generational-buffer

Crates.iogenerational-buffer
lib.rsgenerational-buffer
version1.0.4
created_at2025-07-10 07:30:40.351675+00
updated_at2025-07-10 13:41:50.580397+00
descriptionGenerational Ring Buffer
homepage
repositoryhttps://github.com/Canop/generational-buffer
max_upload_size
id1745994
size13,952
Denys Séguret (Canop)

documentation

README

MIT Latest Version docs Chat on Miaou

A ring buffer returning generational handles on insertion, so that you can check if an item has been replaced since you got the handle.

This is safe and efficient, the storage is a simple vector not taking more space than a minimal ring buffer.

let mut buffer = generational_buffer::GenerationalBuffer::new(2);

// Fill the buffer
let h1 = buffer.push(10);
let h2 = buffer.push(20);

// Wrap around - this should invalidate h1
let h3 = buffer.push(30);

// h1 should now be invalid due to generation mismatch
assert_eq!(buffer.get(h1), None);
assert_eq!(buffer.get(h2), Some(&20));
assert_eq!(buffer.get(h3), Some(&30));
assert!(!buffer.is_valid(h1));
assert!(buffer.is_valid(h2));
assert!(buffer.is_valid(h3));
assert_eq!(buffer.len(), 2);

// let's do one more turn
let h4 = buffer.push(40); // This should overwrite h2
let h5 = buffer.push(50); // This should overwrite h3
assert_eq!(buffer.get(h4), Some(&40));
assert_eq!(buffer.get(h5), Some(&50));
assert!(!buffer.is_valid(h2)); // h2 should be invalid now
assert!(!buffer.is_valid(h3)); // h3 should be invalid now
Commit count: 0

cargo fmt