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.
Creator based on examples/create.rs
rust
//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
rust
// Open an existing MemFile from `pwd`\test.txt
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);
}
}
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.