#mmap #vec #file #data-structure #memmap2

memvec

Memory-backed vector, not buffer. Designed for for mmap. Not MemMap, but MemVec!

4 releases

Uses new Rust 2021

0.0.5 Jun 18, 2022
0.0.4 Jun 4, 2022
0.0.3 May 5, 2022
0.0.2 May 2, 2022
0.0.1 May 2, 2022

#54 in Memory management

Download history 13/week @ 2022-04-26 96/week @ 2022-05-03 31/week @ 2022-05-10 28/week @ 2022-05-17 10/week @ 2022-05-24 93/week @ 2022-05-31 215/week @ 2022-06-07 590/week @ 2022-06-14 324/week @ 2022-06-21

1,222 downloads per month

Custom license

41KB
932 lines

MemVec

MemVec is a Vec-like interface for continuous memory buffer. Mostly intended to be used with mmap.

VecFile + MemVec

use memvec::{MemVec, VecFile};

#[derive(Copy, Clone)]
#[repr(C, packed)]
struct Record {
    time: std::time::Instant,
    event_id: u32,
    _payload: [u8; 50], // we will not use it
}

fn main() {
    let mut path = std::env::temp_dir();
    path.push("vecfile.memvec");

    let vec_file = VecFile::open(&path).expect("file open failed");
    let mut vec =
        unsafe { MemVec::<Record, _>::try_from_memory(vec_file) }.expect("vec file is corrupted");

    if vec.len() == 0 {
        // creating a new file
        println!("creating a new file: {path:?}");
        for i in 0..10 {
            vec.push(Record {
                time: std::time::Instant::now(),
                event_id: '1' as u32 + (i % 3),
                _payload: unsafe { std::mem::zeroed() },
            })
        }
    } else {
        // found an existing file
        for (i, item) in vec.iter().enumerate() {
            println!(
                "idx: {i} time: {time:?} event_id: {event_id}",
                time = item.time,
                event_id = item.event_id
            );
        }
        vec.clear();
        println!("deleted existing file: {path:?}");
    }
}

Dependencies

~95KB