Crate slice_pool [] [src]

This crate provides functionality for wrapping a slice and exposing it as a chunkable interface (i.e acts as a memory pool).

The underlying memory can be a mutable slice of any type.

use slice_pool::SlicePool;

let mut values = vec![10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
let mut memory = SlicePool::new(&mut values);

// Not enough memory available (only 10 elements)
assert!(memory.allocate(11).is_none());

let mut first = memory.allocate(2).unwrap();
assert_eq!(*first, [10, 20]);
first[1] = 15;
assert_eq!(*first, [10, 15]);

// Amount of chunks (i.e the fragmentation)
assert_eq!(memory.len(), 2);

let mem2 = memory.allocate(5).unwrap();
assert_eq!(*mem2, [30, 40, 50, 60, 70]);
assert_eq!(memory.len(), 3);

Structs

ChunkRef

A reference to an allocated chunk.

SlicePool

An interface for allocating chunks in a slice.