Expand description
§typed_shmem
Provides the ShMem and ShMemCfg types for creating a shared memory region.
In order of a type T
to be compatible with the shared memory implementation here present it must be
T: zerocopy::AsBytes + zerocopy::FromBytes + Default
.
Since there is no synchronization when reading/mutating the shared data, the programmer has to be responsible of how to do so in order to not corrupt said data.
§Example
§Owner process
use typed_shmem as sh;
use typed_shmem::error::ShMemErr;
use typed_shmem::common::ShMemOps;
fn main() -> Result<(), ShMemErr> {
let mut mem = sh::ShMemCfg::<u32>::default()
.set_owner()
.on_file("test_program")
.build()?;
//Writing
unsafe { *mem.get_t_mut() = 10; }
//Reading
let val = unsafe { mem.get_t() };
assert_eq!(*val, 10);
loop {} //Used to keep the process alive, thus the allocated shared memory too.
Ok(())
}
§Any other process
use typed_shmem as sh;
use typed_shmem::error::ShMemErr;
use typed_shmem::common::ShMemOps;
fn main() -> Result<(), ShMemErr> {
let mut mem = sh::ShMemCfg::<u32>::default()
.on_file("test_program")
.build()?;
let val = unsafe { mem.get_t() };
assert_eq!(*val, 10);
Ok(())
}
§Panics
If the platform on which this crate is compiled does not comply with cfg(unix) nor with cfg(windows), the program will panic.