使用旧的Rust 2015
0.1.0 |
|
---|
#12 in #shmem
41KB
680 代码行
mem_file
为Linux和Windows提供原生共享内存的包装器。
如果您需要通过内存完全与其他进程共享大量数据,此crate非常理想。
用法
基于示例/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);
}
基于示例/open.rs的从属者
// 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);
}
}
许可证
许可协议为以下之一
任选其一。
贡献
除非您明确声明,否则您有意提交给工作的任何贡献,根据Apache-2.0许可证定义,应双重许可如上所述,不附加任何额外条款或条件。
依赖项
~2MB
~42K SLoC