LICENSE LICENSE Documentation Crates.io Version

Memory-Slice

memory_slice that can alias any kind of data, and provides an API that allow user to read, write and borrow data of any type.

Features

Example

```rust use memory_slice::buffer;

//create an uninitialized buffer of 64 bytes aligned as 8. let mut buff = buffer!(64, 8);

//the create an int inside the buffer and get a reference to it let (padding, v1, remainingbuffer) = buff.write(42 as i32); assert!(padding.isempty()); //unsafe{buff.read::()}; //error => cannot borrow buff as immutable

//use the remaining unitialized buffer to write an u64 in it: let (padding, v2, remainingbuffer2) = remainingbuffer.write(42 as u64); asserteq!(padding.len(), 4); //unsafe{remaingbuffer.read::()}; //error => cannot borrow remaining_buffer

//v1 and v2 are reference to the i32 and u64 created inside buff assert_eq!(*v1 as u64, *v2);

{ extern crate alloc; use std::borrow::ToOwned;

//In what remains of the buffer, let's create a value that needs to be dropped:
let (_padding, v3, _remaining) = remaining_buffer2.emplace("42".to_owned());

//v3 is a smart pointer to the String created in the buffer that will drop
//this string when it goes out of scope
assert_eq!(*v1, v3.parse::<i32>().unwrap());

} //string refered by v3 is dropped

//buff is not anymore borrowed, so it is accessible: assert_eq!(unsafe { buff.read::() }, 42); ```