Expand description
Provides a wrapper around native shared memory for Linux and Windows.
This crate is ideal if you need to share large amounts of data with another process purely through memory.
§Examples
Creator based on examples/create.rs
//Create a MemFile at `pwd`\test.txt of size 4096
let mut mem_file: MemFile = match MemFile::create(PathBuf::from("test.txt"), 4096) {<...>};
//Set explicit scope for the lock (no need to call drop(shared_data))
{
//Acquire write lock
let mut shared_data = match mem_file.wlock_as_slice::<u8>() {<...>};
let src = b"Some string you want to share\x00";
//Write to the shared memory
shared_data[0..src.len()].copy_from_slice(src);
}
Slave based on examples/open.rs
let mut mem_file: MemFile = match MemFile::open(PathBuf::from("test.txt")) {<...>};
//Set explicit scope for the lock (no need to call drop(shared_data))
{
//Acquire read lock
let mut shared_data = match mem_file.rlock_as_slice::<u8>() {<...>};
//Print the content of the shared memory as chars
for byte in &shared_data[0..256] {
if *byte == 0 { break; }
print!("{}", *byte as char);
}
}
Structs§
- MemFile
- Struct used to manipulate the shared memory
- MemFileR
Lock - Non-exclusive read lock holding a reference to shared memory
- MemFileR
Lock Slice - Non-exclusive read lock holding a reference to a slice of shared memory
- MemFileW
Lock - Exclusive write lock holding a reference to shared memory
- MemFileW
Lock Slice - Exclusive write lock holding a reference to a slice of shared memory
Traits§
- MemFile
Cast - Read WARNING before use