fn unsafe_load_vec<T: Copy>(
    expected_relation_hash: u64,
    mut path: std::path::PathBuf,
) -> Result<Vec<T>, Error> {
    path.set_extension("rc");
    trace!("[enter] load({:?})", path);
    let file = std::fs::File::open(&path).map_err(|e| LoadError::OpenFileError {
        path: path.to_path_buf(),
        error: e.to_string(),
    })?;
    let mut buf_reader = BufReader::new(file);
    if cfg!(target_endian = "big") {
        unreachable!("We assume little endian machines");
    }
    let mut buf: [u8; 8] = [0u8; 8];
    assert_eq!(buf_reader.read(&mut buf)?, 8);
    let actual_relation_hash = u64::from_le_bytes(buf);
    assert_eq!(actual_relation_hash, expected_relation_hash);
    let expected_fact_size = std::mem::size_of::<T>();
    assert_eq!(buf_reader.read(&mut buf)?, 8);
    let actual_fact_size = u64::from_le_bytes(buf);
    assert_eq!(expected_fact_size, (actual_fact_size as usize));
    assert_eq!(buf_reader.read(&mut buf)?, 8);
    let len = u64::from_le_bytes(buf) as usize;
    let mut vec: Vec<T> = Vec::with_capacity(len);
    assert_eq!(vec.capacity(), len);
    let begin_ptr = vec.as_ptr() as *mut u8;
    // TODO: Is there a safe and equally fast way of doing this?
    let data = unsafe { std::slice::from_raw_parts_mut(begin_ptr, expected_fact_size * len) };
    let mut total_bytes_read = 0;
    loop {
        let mut remainder = &mut data[total_bytes_read..];
        let bytes_read = buf_reader.read(&mut remainder)?;
        if bytes_read == 0 {
            break;
        }
        total_bytes_read += bytes_read;
    }
    assert_eq!(total_bytes_read, expected_fact_size * len);
    unsafe {
        vec.set_len(len);
    }
    trace!("[exit] load({:?})", path);
    Ok(vec)
}
/// **Note:** this function is not marked unsafe just so that we could use unsafe blocks
/// to mark precisely where we are performing unsafe operations.
this function is not marked unsafe just so that we could use unsafe blocks
to mark precisely where we are performing unsafe operations.
this function is not marked unsafe just so that we could use unsafe blocks to mark precisely where we are performing unsafe operations .
fn unsafe_load_vec<T: Copy>(
    expected_relation_hash: u64,
    mut path: std::path::PathBuf,
) -> Result<Vec<T>, Error> {
    path.set_extension("rc");
    trace!("[enter] load({:?})", path);
    let file = std::fs::File::open(&path).map_err(|e| LoadError::OpenFileError {
        path: path.to_path_buf(),
        error: e.to_string(),
    })?;
    let mut buf_reader = BufReader::new(file);
    if cfg!(target_endian = "big") {
        unreachable!("We assume little endian machines");
    }
    let mut buf: [u8; 8] = [0u8; 8];
    assert_eq!(buf_reader.read(&mut buf)?, 8);
    let actual_relation_hash = u64::from_le_bytes(buf);
    assert_eq!(actual_relation_hash, expected_relation_hash);
    let expected_fact_size = std::mem::size_of::<T>();
    assert_eq!(buf_reader.read(&mut buf)?, 8);
    let actual_fact_size = u64::from_le_bytes(buf);
    assert_eq!(expected_fact_size, (actual_fact_size as usize));
    assert_eq!(buf_reader.read(&mut buf)?, 8);
    let len = u64::from_le_bytes(buf) as usize;
    let mut vec: Vec<T> = Vec::with_capacity(len);
    assert_eq!(vec.capacity(), len);
    let begin_ptr = vec.as_ptr() as *mut u8;
    // TODO: Is there a safe and equally fast way of doing this?
    let data = unsafe { std::slice::from_raw_parts_mut(begin_ptr, expected_fact_size * len) };
    let mut total_bytes_read = 0;
    loop {
        let mut remainder = &mut data[total_bytes_read..];
        let bytes_read = buf_reader.read(&mut remainder)?;
        if bytes_read == 0 {
            break;
        }
        total_bytes_read += bytes_read;
    }
    assert_eq!(total_bytes_read, expected_fact_size * len);
    unsafe {
        vec.set_len(len);
    }
    trace!("[exit] load({:?})", path);
    Ok(vec)
}
fn unsafe_load_vec < T : Copy > ( expected_relation_hash : u64 , mut path : std :: path :: PathBuf , ) -> Result < Vec < T > , Error > { path . set_extension ( " " ) ; trace ! ( " " path ) ; let file = std :: fs :: File :: open ( & path ) . map_err ( | e | LoadError :: OpenFileError { path : path . to_path_buf ( ) , error : e . to_string ( ) , } ) ? ; let mut buf_reader = BufReader :: new ( file ) ; if cfg ! ( target_endian " " ) { unreachable ! ( " " ) ; } let mut buf : [ u8 ; 8 ] = [ 0u8 ; 8 ] ; assert_eq ! ( buf_reader read ( mut buf ) 8 ) ; let actual_relation_hash = u64 :: from_le_bytes ( buf ) ; assert_eq ! ( actual_relation_hash expected_relation_hash ) ; let expected_fact_size = std :: mem :: size_of :: < T > ( ) ; assert_eq ! ( buf_reader read ( mut buf ) 8 ) ; let actual_fact_size = u64 :: from_le_bytes ( buf ) ; assert_eq ! ( expected_fact_size ( actual_fact_size as usize ) ) ; assert_eq ! ( buf_reader read ( mut buf ) 8 ) ; let len = u64 :: from_le_bytes ( buf ) as usize ; let mut vec : Vec < T > = Vec :: with_capacity ( len ) ; assert_eq ! ( vec capacity ( ) len ) ; let begin_ptr = vec . as_ptr ( ) as * mut u8 ; // TODO: Is there a safe and equally fast way of doing this? let data = unsafe { std :: slice :: from_raw_parts_mut ( begin_ptr , expected_fact_size * len ) } ; let mut total_bytes_read = 0 ; loop { let mut remainder = & mut data [ total_bytes_read .. ] ; let bytes_read = buf_reader . read ( & mut remainder ) ? ; if bytes_read == 0 { break ; } total_bytes_read += bytes_read ; } assert_eq ! ( total_bytes_read expected_fact_size len ) ; unsafe { vec . set_len ( len ) ; } trace ! ( " " path ) ; Ok ( vec ) }
Note:** this function is not marked unsafe just so that we could use unsafe blocks
to mark precisely where we are performing unsafe operations.
Note : ** this function is not marked unsafe just so that we could use unsafe blocks to mark precisely where we are performing unsafe operations .
expected_relation_hash
u64
path
std::path::PathBuf