1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
//! CircularBuffer use std::collections::VecDeque; /// A CircularBuffer that drops the oldest element inserted when full. /// # Example /// /// ```rust /// # use amethyst_utils::circular_buffer::CircularBuffer; /// # use std::collections::VecDeque; /// let mut buf = CircularBuffer::<u32>::new(2); /// assert_eq!(*buf.queue(), VecDeque::<u32>::from(vec![])); /// assert!(buf.push(1).is_none()); /// assert_eq!(*buf.queue(), VecDeque::<u32>::from(vec![1])); /// assert!(buf.push(2).is_none()); /// assert_eq!(*buf.queue(), VecDeque::<u32>::from(vec![1, 2])); /// assert!(buf.push(3).is_some()); /// assert_eq!(*buf.queue(), VecDeque::<u32>::from(vec![2, 3])); /// assert_eq!(buf.capacity(), 2); /// ``` #[derive(Debug)] pub struct CircularBuffer<A> { queue: VecDeque<A>, cap: usize, } impl<A> CircularBuffer<A> { ///Creates a new CircularBuffer with fixed size pub fn new(size: usize) -> Self { CircularBuffer { queue: VecDeque::with_capacity(size), cap: size, } } ///Add a value to the CircularBuffer ///Returns the popped value if the buffer is full pub fn push(&mut self, elem: A) -> Option<A> { let out = if self.queue.len() == self.cap { //front to back <-> oldest to newest self.queue.pop_front() } else { None }; self.queue.push_back(elem); out } ///Get an immutable reference to the values inside the CircularBuffer pub fn queue(&self) -> &VecDeque<A> { &self.queue } /// Returns the capacity of the circular buffer. pub fn capacity(&self) -> usize { self.cap } }