#region #object #different #structure #memory #data #type

yanked allocal

A simple crate that allows storing objects of different types into a single region

0.1.1 Dec 21, 2020
0.1.0 Dec 21, 2020

#75 in #region

MIT license

12KB
217 lines

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.

Example

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);

No runtime deps