Index: Cargo.toml ================================================================== --- Cargo.toml +++ Cargo.toml @@ -1,8 +1,8 @@ [package] name = "bndpresbufq" -version = "0.1.1" +version = "0.1.2" edition = "2021" license = "0BSD" # https://crates.io/category_slugs categories = [ "data-structures" ] keywords = [ "buffer", "queue", "bounded", "bounds-preserving" ] @@ -21,11 +21,11 @@ # https://doc.rust-lang.org/cargo/reference/manifest.html#the-badges-section [badges] maintenance = { status = "passively-maintained" } [dependencies] -limq = { version = "0.1.1" } +limq = { version = "0.1.2" } [package.metadata.docs.rs] rustdoc-args = ["--generate-link-to-definition"] [lints.clippy] Index: src/lib.rs ================================================================== --- src/lib.rs +++ src/lib.rs @@ -56,10 +56,17 @@ q: LimQ::new(max_len), max_size, size: 0 } } + + /// Clear all buffers from queue. + #[inline] + pub fn clear(&mut self) { + self.q.clear(); + self.size = 0; + } /// Change the length limit of the queue. /// /// # Panics /// A zero-limit will cause a panic. @@ -109,10 +116,22 @@ #[inline] #[must_use] pub fn is_empty(&self) -> bool { self.q.is_empty() } + + /// Return boolean indicating whether the queue has reached its configured + /// limits. + /// + /// Returns `false` is no limits have been configured. + #[must_use] + pub fn is_full(&self) -> bool { + self.q.is_full() + || self + .max_size + .map_or(false, |max_size| self.size >= max_size) + } /// Drop elements that overflow the queue limit (if a limit has been set). #[inline] pub fn purge_overflow(&mut self) { self.q.purge_overflow(); @@ -285,8 +304,32 @@ #[test] fn force_too_big_buf() { let mut q = BndPresLimBufQ::new(None, Some(2)); assert_eq!(q.force_push([1, 2, 3].into()), Err(vec![1, 2, 3])); } + + #[test] + fn is_full_len() { + let mut q = BndPresLimBufQ::new(Some(1), None); + assert!(!q.is_full()); + q.force_push([1].into()).unwrap(); + assert!(q.is_full()); + } + + #[test] + fn is_full_size() { + let mut q = BndPresLimBufQ::new(None, Some(1)); + assert!(!q.is_full()); + q.force_push([1].into()).unwrap(); + assert!(q.is_full()); + } + + #[test] + fn is_full_both() { + let mut q = BndPresLimBufQ::new(Some(1), Some(1)); + assert!(!q.is_full()); + q.force_push([1].into()).unwrap(); + assert!(q.is_full()); + } } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 : Index: www/changelog.md ================================================================== --- www/changelog.md +++ www/changelog.md @@ -1,17 +1,27 @@ # Change Log ## [Unreleased] -[Details](/vdiff?from=bndpresbufq-0.1.1&to=trunk) +[Details](/vdiff?from=bndpresbufq-0.1.2&to=trunk) ### Added ### Changed ### Removed +--- + +## [0.1.2] - 2024-09-13 + +[Details](/vdiff?from=bndpresbufq-0.1.1&to=bndpresbufq-0.1.2) + +### Added + +- Add `clear()` and `is_full()` methods. + --- ## [0.1.1] - 2024-09-13 [Details](/vdiff?from=bndpresbufq-0.1.0&to=bndpresbufq-0.1.1)