Crates.io | allocal |
lib.rs | allocal |
version | 0.1.1 |
source | src |
created_at | 2020-12-21 19:31:06.810099 |
updated_at | 2020-12-21 20:06:23.273044 |
description | A simple crate that allows storing objects of different types into a single region. |
homepage | |
repository | https://github.com/gymore-z/allocal |
max_upload_size | |
id | 325367 |
size | 11,738 |
allocal
is a simple crate that provides the Allocal
structure. This structure works a bit like an allocator but only works on a fixed continuous region of the memory of the heap.
use allocal::Allocal;
// Create a 1024 bytes-long region to store any kind of data
let mut region = Allocal::with_capacity(1024);
// Allocate things into the buffer
// an error is returned if the region is full
let location_i32 = region.allocate(123i32).unwrap();
let location_i64 = region.allocate(123i64).unwrap();
let location_vec = region.allocate(vec![1u8, 2, 3]).unwrap();
// You can then get back references to your items
*region.get_mut(location_i32) = 124;
region.get_mut(location_vec).push(4);
assert_eq!(region.get(location_i64), &123);
// Or deallocate them when you don't needs them anymore to get some space
let vec: Vec<u8> = region.deallocate(location_vec);
assert_eq!(vec.len(), 4);