sequential-id-alloc

Crates.iosequential-id-alloc
lib.rssequential-id-alloc
version0.1.0
created_at2025-09-25 14:05:22.877648+00
updated_at2025-09-25 14:05:22.877648+00
descriptionA simple sequential ID allocator that guarantees sequential allocation
homepagehttps://github.com/hypervideo/sequential-id-alloc
repositoryhttps://github.com/hypervideo/sequential-id-alloc
max_upload_size
id1854670
size48,300
Robert Krahn (rksm)

documentation

README

sequential-id-alloc

Crates.io License

A simple sequential ID allocator that guarantees sequential allocation.

This crate provides a macro to generate sequential ID allocators that differ from traditional bitmap/slab allocators by not immediately reusing freed IDs. Instead, freed IDs are only reused when the allocation pointer wraps around to them again. This ensures IDs are allocated sequentially and predictably.

Example

use sequential_id_alloc::sequential_id_alloc;

// Create an allocator for u8 IDs (0-255)
sequential_id_alloc!(MyIdAllocator, u8, 256, u8);

let mut allocator = MyIdAllocator::default();

// Allocate IDs sequentially
assert_eq!(allocator.alloc(), Some(0u8));
assert_eq!(allocator.alloc(), Some(1u8));
assert_eq!(allocator.alloc(), Some(2u8));

// Free an ID - it won't be reused immediately
allocator.dealloc(1u8);
assert_eq!(allocator.alloc(), Some(3u8)); // Gets 3, not 1

// Check if an ID is allocated
assert!(allocator.contains(0u8));
assert!(!allocator.contains(1u8));

// Get allocation statistics
assert_eq!(allocator.size(), 3); // Currently 3 IDs allocated
assert!(!allocator.is_full());   // Not all IDs are allocated
Commit count: 8

cargo fmt