From 55fc2d4e42868832842b932a41fb5add36b98492 Mon Sep 17 00:00:00 2001 From: Matt Wilson Date: Tue, 7 Jul 2026 12:23:40 +1000 Subject: [PATCH] feature: implement `Len` for `NanosecondsStr` --- CHANGES.md | 5 + Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 5 +- .../time_format/nanoseconds_str.rs | 122 +++++++++++++++++- 5 files changed, 132 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 1f3e485..8f04b4a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,11 @@ # Diagnosticism.Rust - CHANGES +## 0.4.1 - 7th July 2026 + +* implement `Len` for `NanosecondsStr`; + + ## 0.4.0 - 30th June 2026 * added `DoomGram::to_mmm()` and `DoomGram::to_nmmm()` — compact min/mean/max duration summaries using `nanoseconds_to_string()`; diff --git a/Cargo.lock b/Cargo.lock index 6dd1eac..a9302dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -212,7 +212,7 @@ checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" [[package]] name = "diagnosticism" -version = "0.4.0" +version = "0.4.1" dependencies = [ "base-traits", "criterion", diff --git a/Cargo.toml b/Cargo.toml index 9a3c442..52fa33f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ name = "diagnosticism" readme = "README.md" repository = "https://github.com/synesissoftware/Diagnosticism.Rust" rust-version = "1.74" -version = "0.4.0" +version = "0.4.1" # ########################################################## diff --git a/README.md b/README.md index 3722325..b53785c 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ diagnosticism = { version = "0" } ## Components + ### Constants No public constants are defined at this time. @@ -320,6 +321,7 @@ Terse `Debug` form of `thing2`: Thing2 { name: "i-am-a-public-thing", internals: ## Project Information + ### Where to get help [GitHub Page](https://github.com/synesissoftware/Diagnosticism.Rust "GitHub Page") @@ -332,7 +334,8 @@ Defect reports, feature requests, and pull requests are welcome on https://githu ### Dependencies -**Diagnosticism.Rust** has no (non-development) dependencies. +* [**base-traits**](https://github.com/synesissoftware/base-traits.rs); + #### Dev Dependencies diff --git a/src/diagnostics/time_format/nanoseconds_str.rs b/src/diagnostics/time_format/nanoseconds_str.rs index 17590db..4dd6e72 100644 --- a/src/diagnostics/time_format/nanoseconds_str.rs +++ b/src/diagnostics/time_format/nanoseconds_str.rs @@ -1,6 +1,9 @@ // src/diagnostics/time_format/nanoseconds_str.rs : `NanosecondsStr` -use base_traits::AsStr; +use base_traits::{ + AsStr, + Len, +}; use std::{ borrow::Borrow, @@ -160,6 +163,14 @@ impl From for String { } } +impl Len for NanosecondsStr { + fn len(&self) -> usize { + match &self.inner { + NanosecondsStrInner::Heap(s) => s.len(), + NanosecondsStrInner::Inline { len, .. } => *len as usize, + } + } +} impl PartialEq for &str { fn eq( @@ -360,6 +371,115 @@ mod tests { assert!(s.as_str().len() <= INLINE_CAP); } } + + + mod TEST_AsStr { + #![allow(non_snake_case)] + + use super::*; + + use base_traits::AsStr; + + + #[allow(unused)] + fn as_AsStr(t : &T) -> &impl AsStr { + t + } + + + #[test] + fn TEST_INLINE_STORAGE() { + let s = nanoseconds_to_string(123_456_789, ""); + + assert!(!s.is_heap()); + assert_eq!("123.4ms", AsStr::as_str(&s)); + + let as_trait = as_AsStr(&s); + + assert_eq!("123.4ms", as_trait.as_str()); + + let s = &s; + + assert_eq!("123.4ms", AsStr::as_str(s)); + } + + #[test] + fn TEST_INLINE_AT_INLINE_CAP() { + let expected = "a".repeat(INLINE_CAP); + let s = heap_from_bytes(expected.as_bytes()); + + assert!(!s.is_heap()); + assert_eq!(expected.as_str(), AsStr::as_str(&s)); + } + + #[test] + fn TEST_HEAP_STORAGE() { + let expected = "x".repeat(INLINE_CAP + 4); + let s = heap_from_bytes(expected.as_bytes()); + + assert!(s.is_heap()); + assert_eq!(expected, AsStr::as_str(&s)); + } + } + + + mod TEST_Len { + #![allow(non_snake_case)] + + use super::*; + + use base_traits::Len; + + + #[allow(unused)] + fn as_Len(t : &T) -> &impl Len { + t + } + + + #[test] + fn TEST_INLINE_STORAGE() { + let s = nanoseconds_to_string(123_456_789, ""); + + assert!(!s.is_heap()); + assert_eq!(s.as_str().len(), Len::len(&s)); + + let as_trait = as_Len(&s); + + assert_eq!(s.as_str().len(), as_trait.len()); + + let s = &s; + + assert_eq!(s.as_str().len(), Len::len(s)); + } + + #[test] + fn TEST_INLINE_AT_INLINE_CAP() { + let expected = "a".repeat(INLINE_CAP); + let s = heap_from_bytes(expected.as_bytes()); + + assert!(!s.is_heap()); + assert_eq!(INLINE_CAP, Len::len(&s)); + } + + #[test] + fn TEST_HEAP_AT_INLINE_CAP_PLUS_ONE() { + let expected = "b".repeat(INLINE_CAP + 1); + let s = heap_from_bytes(expected.as_bytes()); + + assert!(s.is_heap()); + assert_eq!(INLINE_CAP + 1, Len::len(&s)); + } + + #[test] + fn TEST_HEAP_STORAGE() { + let expected = "x".repeat(INLINE_CAP + 4); + let s = heap_from_bytes(expected.as_bytes()); + + assert!(s.is_heap()); + assert_eq!(expected.len(), Len::len(&s)); + } + } }