generational_buffer/lib.rs
1//! A ring buffer returning generational handles on insertion, so that
2//! you can check if an item has been replaced since you got the handle.
3//!
4//! This is safe and efficient, the storage is a simple vector not taking
5//! more space than a minimal ring buffer.
6//!
7//! ```
8//! let mut buffer = generational_buffer::GenerationalBuffer::new(2);
9//!
10//! // Fill the buffer
11//! let h1 = buffer.push(10);
12//! let h2 = buffer.push(20);
13//!
14//! // Wrap around - this should invalidate h1
15//! let h3 = buffer.push(30);
16//!
17//! // h1 should now be invalid due to generation mismatch
18//! assert_eq!(buffer.get(h1), None);
19//! assert_eq!(buffer.get(h2), Some(&20));
20//! assert_eq!(buffer.get(h3), Some(&30));
21//! assert!(!buffer.is_valid(h1));
22//! assert!(buffer.is_valid(h2));
23//! assert!(buffer.is_valid(h3));
24//! assert_eq!(buffer.len(), 2);
25//!
26//! // let's do one more turn
27//! let h4 = buffer.push(40); // This should overwrite h2
28//! let h5 = buffer.push(50); // This should overwrite h3
29//! assert_eq!(buffer.get(h4), Some(&40));
30//! assert_eq!(buffer.get(h5), Some(&50));
31//! assert!(!buffer.is_valid(h2)); // h2 should be invalid now
32//! assert!(!buffer.is_valid(h3)); // h3 should be invalid now
33//! ```
34
35mod generational_buffer;
36
37pub use generational_buffer::*;