slice_pool2/lib.rs
1//! This crate provides functionality for using a sliceable type as the
2//! underlying memory for a pool.
3//!
4//! The allocated memory can be a mutable slice of any type.
5//!
6//! ```
7//! use slice_pool::sync::SlicePool;
8//!
9//! let values = vec![10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
10//! let mut memory = SlicePool::new(values);
11//! assert_eq!(memory.len(), 10);
12//!
13//! // Not enough memory available (only 10 elements)
14//! assert!(memory.alloc(11).is_none());
15//!
16//! let mut first = memory.alloc(2).unwrap();
17//! assert_eq!(*first, [10, 20]);
18//! first[1] = 15;
19//! assert_eq!(*first, [10, 15]);
20//!
21//! let mem2 = memory.alloc(5).unwrap();
22//! assert_eq!(*mem2, [30, 40, 50, 60, 70]);
23//! ```
24
25pub mod sync;
26pub mod unsync;
27
28/// A chunk of memory inside a slice.
29#[derive(Debug, Copy, Clone)]
30struct Chunk {
31 offset: usize,
32 size: usize,
33 free: bool,
34}
35
36impl Chunk {
37 pub fn new(size: usize) -> Self {
38 Self::with_offset(size, 0)
39 }
40
41 pub fn with_offset(size: usize, offset: usize) -> Self {
42 Chunk {
43 size,
44 offset,
45 free: true,
46 }
47 }
48}