memory_sclice
that can alias any kind of buffer, and provides an API that allow user to read, write and borrow
data of any type.
In your Cargo.toml
:
Toml
[dependencies]
memory_slice = "0.1"
```rust use memory_slice::AsMutMemory; use core::mem::MaybeUninit;
let mut buf = MaybeUninit::<[i32;3]>::uninit(); let mem = buf.asmutmemory();
//split the memory to place an element in the first 4 byte let (lmem, rmem) = mem.splitatmut(4); //from this point mem and buf are borrowed
let v1 = lmem.write(33 as i32);
//at this point v1 is a &mut i32
that borrowes lmem. It refers
//to a i32 placed in the first 4 bytes of buff.
//split rmem to creates to new integer in the memory let (rmem1, rmem2) = rmem.splitatmut(4);
{ //This time we create to smart pointer that will drop //v2 and v3 when they will get out of scope: let v2 = rmem1.emplace(133); let v3 = rmem2.emplace(1033);
//rmem1.read::
asserteq!(*v2, 133); asserteq!(v3, 1033); //drop v2 and v3 pointees inside the buffer } assert_eq!(v1, 33);
//There are no more reference to the buffer, so now it can be read:
asserteq!(unsafe { buf.asmut_memory().read::