Index: src/lib.rs ================================================================== --- src/lib.rs +++ src/lib.rs @@ -231,14 +231,15 @@ } } self.q.push_back(n); } - /// Attempt to push node onto queue so it will be the next in line. + /// Attempt to push node onto queue so it will be the next in line to be + /// taken off it. /// - /// Meant to be used to return an element to the queue that was just taken - /// off it, but is currently not available to be processed. + /// Can be used to return an element to the queue that was just taken off it, + /// but is currently not available to be processed. /// /// ``` /// use limq::LimQ; /// /// // Construct a queue with a maximum 2 element length limit @@ -324,8 +325,46 @@ q.force_push(42); q.force_push(11); assert_eq!(q.pop(), Some(11)); } + + #[test] + fn try_return_success() { + let mut q: LimQ = LimQ::new(Some(2)); + q.try_push(1).unwrap(); + q.try_push(2).unwrap(); + + let n = q.pop().unwrap(); + assert_eq!(n, 1); + + q.try_return(n).unwrap(); + + assert_eq!(q.pop(), Some(1)); + assert_eq!(q.pop(), Some(2)); + assert_eq!(q.pop(), None); + } + + #[test] + fn try_return_fail() { + let mut q: LimQ = LimQ::new(Some(2)); + q.try_push(1).unwrap(); + q.try_push(2).unwrap(); + + let n = q.pop().unwrap(); + assert_eq!(n, 1); + + // fill up queue again + q.try_push(3).unwrap(); + + let Err(n) = q.try_return(n) else { + panic!("Unexpectedly successful"); + }; + assert_eq!(n, 1); + + assert_eq!(q.pop(), Some(2)); + assert_eq!(q.pop(), Some(3)); + assert_eq!(q.pop(), None); + } } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :