Crates.io | memvec |
lib.rs | memvec |
version | 0.1.0 |
source | src |
created_at | 2022-05-02 16:22:07.877155 |
updated_at | 2022-08-22 13:15:24.782971 |
description | Memory-backed vector, not buffer. Designed for for mmap. Not MemMap, but MemVec! |
homepage | https://github.com/youknowone/memvec |
repository | https://github.com/youknowone/memvec.git |
max_upload_size | |
id | 579200 |
size | 45,042 |
MemVec
is a Vec-like interface for continuous memory buffer. Mostly designed to be used with mmap
.
VecFile
is a simple linear database. For the database, a file is a collection of records. VecFile
is the Vec interface of a file of records without read, write, and seek but using mmap.
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:?}");
}
}