|
| 1 | +//! A simple sequential ID allocator that guarantees sequential allocation. |
| 2 | +//! |
| 3 | +//! This crate provides a macro to generate sequential ID allocators that differ from |
| 4 | +//! traditional bitmap/slab allocators by not immediately reusing freed IDs. Instead, |
| 5 | +//! freed IDs are only reused when the allocation pointer wraps around to them again. |
| 6 | +//! This ensures IDs are allocated sequentially and predictably. |
| 7 | +//! |
| 8 | +//! # Example |
| 9 | +//! |
| 10 | +//! ``` |
| 11 | +//! use sequential_id_alloc::sequential_id_alloc; |
| 12 | +//! |
| 13 | +//! // Create an allocator for u8 IDs (0-255) |
| 14 | +//! sequential_id_alloc!(MyIdAllocator, u8, 256, u8); |
| 15 | +//! |
| 16 | +//! let mut allocator = MyIdAllocator::default(); |
| 17 | +//! |
| 18 | +//! // Allocate IDs sequentially |
| 19 | +//! assert_eq!(allocator.alloc(), Some(0u8)); |
| 20 | +//! assert_eq!(allocator.alloc(), Some(1u8)); |
| 21 | +//! assert_eq!(allocator.alloc(), Some(2u8)); |
| 22 | +//! |
| 23 | +//! // Free an ID - it won't be reused immediately |
| 24 | +//! allocator.dealloc(1u8); |
| 25 | +//! assert_eq!(allocator.alloc(), Some(3u8)); // Gets 3, not 1 |
| 26 | +//! |
| 27 | +//! // Check if an ID is allocated |
| 28 | +//! assert!(allocator.contains(0u8)); |
| 29 | +//! assert!(!allocator.contains(1u8)); |
| 30 | +//! |
| 31 | +//! // Get allocation statistics |
| 32 | +//! assert_eq!(allocator.size(), 3); // Currently 3 IDs allocated |
| 33 | +//! assert!(!allocator.is_full()); // Not all IDs are allocated |
| 34 | +//! ``` |
| 35 | +
|
1 | 36 | pub use bitvec::BitArr; |
2 | 37 |
|
3 | 38 | /// A simple sequential id allocator. |
|
0 commit comments