Index: Cargo.toml ================================================================== --- Cargo.toml +++ Cargo.toml @@ -1,8 +1,8 @@ [package] name = "limq" -version = "0.1.2" +version = "0.1.3" edition = "2021" license = "0BSD" # https://crates.io/category_slugs categories = [ "data-structures" ] keywords = [ "queue", "bounded" ] Index: src/lib.rs ================================================================== --- src/lib.rs +++ src/lib.rs @@ -230,10 +230,52 @@ let _ = self.q.pop_front(); } } self.q.push_back(n); } + + /// Attempt to push node onto queue so it will be the next in line. + /// + /// 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. + /// + /// ``` + /// use limq::LimQ; + /// + /// // Construct a queue with a maximum 2 element length limit + /// let mut q: LimQ = LimQ::new(Some(2)); + /// + /// // Fill queue up + /// q.force_push(1); + /// q.force_push(2); + /// + /// // Take node off queue + /// let n = q.pop().unwrap(); + /// assert_eq!(n, 1); + /// assert_eq!(q.len(), 1); + /// + /// // Return node to queue + /// q.try_return(n); + /// assert_eq!(q.len(), 2); + /// + /// // Should be the same node again + /// let n = q.pop().unwrap(); + /// assert_eq!(n, 1); + /// ``` + /// + /// # Errors + /// If the queue is full, return the node back to caller. + #[inline] + pub fn try_return(&mut self, n: T) -> Result<(), T> { + if let Some(max_len) = self.max_len { + if self.q.len() > (max_len - 1) { + return Err(n); + } + } + self.q.push_front(n); + Ok(()) + } /// Take node off queue. /// /// Returns `None` if the queue is empty. #[inline] Index: www/changelog.md ================================================================== --- www/changelog.md +++ www/changelog.md @@ -1,17 +1,28 @@ # Change Log ## [Unreleased] -[Details](/vdiff?from=limq-0.1.2&to=trunk) +[Details](/vdiff?from=limq-0.1.3&to=trunk) ### Added ### Changed ### Removed +--- + +## [0.1.3] - 2024-10-05 + +[Details](/vdiff?from=limq-0.1.2&to=limq-0.1.3) + +### Added + +- `LimQ::try_return()` can be used to add an element to the front of the queue. + Intended to be used to add an element back that was just taken off. + --- ## [0.1.2] - 2024-09-13 [Details](/vdiff?from=limq-0.1.1&to=limq-0.1.2)