diff --git a/Cargo.lock b/Cargo.lock index 3500492..68f0d23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ name = "containers" version = "0.1.0" dependencies = [ "elementary", + "score_log", ] [[package]] diff --git a/src/containers/BUILD b/src/containers/BUILD index af9cb47..9c54466 100644 --- a/src/containers/BUILD +++ b/src/containers/BUILD @@ -18,7 +18,10 @@ rust_library( srcs = glob(["**/*.rs"]), edition = "2021", visibility = ["//visibility:public"], - deps = ["//src/elementary"], + deps = [ + "//src/elementary", + "//src/log/score_log", + ], ) rust_test( diff --git a/src/containers/Cargo.toml b/src/containers/Cargo.toml index 18a071b..82a468e 100644 --- a/src/containers/Cargo.toml +++ b/src/containers/Cargo.toml @@ -24,3 +24,4 @@ path = "lib.rs" [dependencies] elementary.workspace = true +score_log.workspace = true diff --git a/src/containers/fixed_capacity/queue.rs b/src/containers/fixed_capacity/queue.rs index 6cd3d59..92a3a33 100644 --- a/src/containers/fixed_capacity/queue.rs +++ b/src/containers/fixed_capacity/queue.rs @@ -13,8 +13,10 @@ use crate::generic::queue::GenericQueue; use crate::storage::Heap; +use core::fmt; use core::ops; use elementary::{BasicAllocator, HeapAllocator, GLOBAL_ALLOCATOR}; +use score_log::fmt::{FormatSpec, Result as ScoreLogResult, ScoreDebug, Writer}; /// A fixed-capacity queue, using provided allocator. /// @@ -64,6 +66,18 @@ impl ops::DerefMut for FixedCapacityQueueIn<'_, T, A> { } } +impl fmt::Debug for FixedCapacityQueueIn<'_, T, A> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&self.inner, f) + } +} + +impl ScoreDebug for FixedCapacityQueueIn<'_, T, A> { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + ScoreDebug::fmt(&self.inner, f, spec) + } +} + /// A fixed-capacity queue, using global allocator. /// Refer to [`FixedCapacityQueueIn`] for more information. pub struct FixedCapacityQueue(FixedCapacityQueueIn<'static, T, HeapAllocator>); @@ -95,6 +109,18 @@ impl ops::DerefMut for FixedCapacityQueue { } } +impl fmt::Debug for FixedCapacityQueue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&self.0, f) + } +} + +impl ScoreDebug for FixedCapacityQueue { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + ScoreDebug::fmt(&self.0, f, spec) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/containers/fixed_capacity/string.rs b/src/containers/fixed_capacity/string.rs index 7fba594..2929763 100644 --- a/src/containers/fixed_capacity/string.rs +++ b/src/containers/fixed_capacity/string.rs @@ -15,8 +15,8 @@ use crate::generic::string::GenericString; use crate::storage::Heap; use core::fmt; use core::ops; -use elementary::GLOBAL_ALLOCATOR; -use elementary::{BasicAllocator, HeapAllocator}; +use elementary::{BasicAllocator, HeapAllocator, GLOBAL_ALLOCATOR}; +use score_log::fmt::{FormatSpec, Result as ScoreLogResult, ScoreDebug, Writer}; /// A fixed-capacity Unicode string, using provided allocator.. /// @@ -98,6 +98,12 @@ impl fmt::Debug for FixedCapacityStringIn<'_, A> { } } +impl ScoreDebug for FixedCapacityStringIn<'_, A> { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + ScoreDebug::fmt(self.as_str(), f, spec) + } +} + /// A fixed-capacity Unicode string, using global allocator. /// Refer to [`FixedCapacityStringIn`] for more information. pub struct FixedCapacityString(FixedCapacityStringIn<'static, HeapAllocator>); @@ -150,7 +156,13 @@ impl fmt::Display for FixedCapacityString { impl fmt::Debug for FixedCapacityString { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(self.0.as_str(), f) + fmt::Debug::fmt(&self.0, f) + } +} + +impl ScoreDebug for FixedCapacityString { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + ScoreDebug::fmt(&self.0, f, spec) } } diff --git a/src/containers/fixed_capacity/vec.rs b/src/containers/fixed_capacity/vec.rs index 5679c16..09ee379 100644 --- a/src/containers/fixed_capacity/vec.rs +++ b/src/containers/fixed_capacity/vec.rs @@ -16,6 +16,7 @@ use crate::storage::Heap; use core::fmt; use core::ops; use elementary::{BasicAllocator, HeapAllocator, GLOBAL_ALLOCATOR}; +use score_log::fmt::{FormatSpec, Result as ScoreLogResult, ScoreDebug, Writer}; /// A fixed-capacity vector, using provided allocator. /// @@ -85,6 +86,12 @@ impl fmt::Debug for FixedCapacityVecIn<'_, T, } } +impl ScoreDebug for FixedCapacityVecIn<'_, T, A> { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + ScoreDebug::fmt(self.as_slice(), f, spec) + } +} + /// A fixed-capacity vector, using global allocator. /// Refer to [`FixedCapacityVecIn`] for more information. pub struct FixedCapacityVec(FixedCapacityVecIn<'static, T, HeapAllocator>); @@ -131,6 +138,12 @@ impl fmt::Debug for FixedCapacityVec { } } +impl ScoreDebug for FixedCapacityVec { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + ScoreDebug::fmt(&self.0, f, spec) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/containers/generic/queue.rs b/src/containers/generic/queue.rs index 9cfdcca..24a184f 100644 --- a/src/containers/generic/queue.rs +++ b/src/containers/generic/queue.rs @@ -11,12 +11,14 @@ // SPDX-License-Identifier: Apache-2.0 // ******************************************************************************* +use core::fmt; use core::iter::FusedIterator; use core::marker::PhantomData; use core::mem::needs_drop; use core::ops::Range; use core::ptr; use core::slice; +use score_log::fmt::{DebugList, FormatSpec, Result as ScoreLogResult, ScoreDebug, Writer}; use crate::storage::Storage; use crate::InsufficientCapacity; @@ -278,6 +280,18 @@ impl> GenericQueue { } } +impl> fmt::Debug for GenericQueue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_list().entries(self.iter()).finish() + } +} + +impl> ScoreDebug for GenericQueue { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + DebugList::new(f, spec).entries(self.iter()).finish() + } +} + pub struct Iter<'a, T> { first: slice::Iter<'a, T>, second: slice::Iter<'a, T>, diff --git a/src/containers/generic/string.rs b/src/containers/generic/string.rs index a567d51..e42f7bf 100644 --- a/src/containers/generic/string.rs +++ b/src/containers/generic/string.rs @@ -14,6 +14,7 @@ use core::fmt; use core::ops; use core::str; +use score_log::fmt::{FormatSpec, Result as ScoreLogResult, ScoreDebug, Writer}; use super::vec::GenericVec; use crate::storage::Storage; @@ -137,6 +138,12 @@ impl> fmt::Debug for GenericString { } } +impl> ScoreDebug for GenericString { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + ScoreDebug::fmt(self.as_str(), f, spec) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/containers/generic/vec.rs b/src/containers/generic/vec.rs index 13345ba..2d91566 100644 --- a/src/containers/generic/vec.rs +++ b/src/containers/generic/vec.rs @@ -16,6 +16,7 @@ use core::marker::PhantomData; use core::mem::needs_drop; use core::ops; use core::ptr; +use score_log::fmt::{FormatSpec, Result as ScoreLogResult, ScoreDebug, Writer}; use crate::storage::Storage; use crate::InsufficientCapacity; @@ -175,6 +176,12 @@ impl> fmt::Debug for GenericVec { } } +impl> ScoreDebug for GenericVec { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + ScoreDebug::fmt(self.as_slice(), f, spec) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/containers/inline/option.rs b/src/containers/inline/option.rs index 196668e..ef9805e 100644 --- a/src/containers/inline/option.rs +++ b/src/containers/inline/option.rs @@ -14,6 +14,7 @@ use core::cmp; use core::fmt; use core::mem::MaybeUninit; +use score_log::fmt::{FormatSpec, Result as ScoreLogResult, ScoreDebug, Writer}; /// An optional value, similar to [`Option`] in the Rust standard library. /// @@ -174,6 +175,15 @@ where } } +impl ScoreDebug for InlineOption +where + for<'a> Option<&'a T>: ScoreDebug, +{ + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + ScoreDebug::fmt(&self.as_ref(), f, spec) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/containers/inline/queue.rs b/src/containers/inline/queue.rs index 2c50294..ea934fd 100644 --- a/src/containers/inline/queue.rs +++ b/src/containers/inline/queue.rs @@ -11,7 +11,9 @@ // SPDX-License-Identifier: Apache-2.0 // ******************************************************************************* +use core::fmt; use core::ops; +use score_log::fmt::{FormatSpec, Result as ScoreLogResult, ScoreDebug, Writer}; use crate::generic::queue::GenericQueue; use crate::storage::Inline; @@ -73,6 +75,18 @@ impl ops::DerefMut for InlineQueue } } +impl fmt::Debug for InlineQueue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&self.inner, f) + } +} + +impl ScoreDebug for InlineQueue { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + ScoreDebug::fmt(&self.inner, f, spec) + } +} + #[cfg(test)] mod tests { use std::collections::VecDeque; diff --git a/src/containers/inline/result.rs b/src/containers/inline/result.rs index 4286eaa..8506d6e 100644 --- a/src/containers/inline/result.rs +++ b/src/containers/inline/result.rs @@ -14,6 +14,7 @@ use core::cmp; use core::fmt; use core::mem::ManuallyDrop; +use score_log::fmt::{FormatSpec, Result as ScoreLogResult, ScoreDebug, Writer}; /// An result value for error handling, similar to [`Result`] in the Rust standard library. /// @@ -188,6 +189,15 @@ where } } +impl ScoreDebug for InlineResult +where + for<'a> Result<&'a T, &'a E>: ScoreDebug, +{ + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + ScoreDebug::fmt(&self.as_ref(), f, spec) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/containers/inline/string.rs b/src/containers/inline/string.rs index f1e3b40..1730832 100644 --- a/src/containers/inline/string.rs +++ b/src/containers/inline/string.rs @@ -13,6 +13,7 @@ use core::fmt; use core::ops; +use score_log::fmt::{FormatSpec, Result as ScoreLogResult, ScoreDebug, Writer}; use crate::generic::string::GenericString; use crate::storage::Inline; @@ -81,6 +82,12 @@ impl fmt::Debug for InlineString { } } +impl ScoreDebug for InlineString { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + ScoreDebug::fmt(self.as_str(), f, spec) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/containers/inline/vec.rs b/src/containers/inline/vec.rs index 2dcefff..9f45364 100644 --- a/src/containers/inline/vec.rs +++ b/src/containers/inline/vec.rs @@ -13,6 +13,7 @@ use core::fmt; use core::ops; +use score_log::fmt::{FormatSpec, Result as ScoreLogResult, ScoreDebug, Writer}; use crate::generic::vec::GenericVec; use crate::storage::Inline; @@ -79,6 +80,12 @@ impl fmt::Debug for InlineVec ScoreDebug for InlineVec { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> ScoreLogResult { + ScoreDebug::fmt(self.as_slice(), f, spec) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/containers/lib.rs b/src/containers/lib.rs index 9b69338..5444c96 100644 --- a/src/containers/lib.rs +++ b/src/containers/lib.rs @@ -11,8 +11,6 @@ // SPDX-License-Identifier: Apache-2.0 // ******************************************************************************* -#![cfg_attr(not(test), no_std)] - extern crate alloc; pub mod fixed_capacity; diff --git a/src/log/score_log_fmt/fmt_impl.rs b/src/log/score_log_fmt/fmt_impl.rs index d3b8317..d516f8e 100644 --- a/src/log/score_log_fmt/fmt_impl.rs +++ b/src/log/score_log_fmt/fmt_impl.rs @@ -14,14 +14,14 @@ //! `ScoreDebug` implementations for common types. use crate::builders::{DebugList, DebugStruct, DebugTuple}; -use crate::fmt::{Error, Result, ScoreDebug, Writer}; +use crate::fmt::{Error, Result as FmtResult, ScoreDebug, Writer}; use crate::fmt_spec::{DisplayHint, FormatSpec}; use crate::DebugMap; macro_rules! impl_debug_for_t { ($t:ty, $fn:ident) => { impl ScoreDebug for $t { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { f.$fn(self, spec) } } @@ -41,13 +41,13 @@ impl_debug_for_t!(u32, write_u32); impl_debug_for_t!(u64, write_u64); impl ScoreDebug for () { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { f.write_str("()", spec) } } impl ScoreDebug for str { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { match spec.get_display_hint() { DisplayHint::Debug => { let queue_spec = FormatSpec::new(); @@ -61,13 +61,13 @@ impl ScoreDebug for str { } impl ScoreDebug for String { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { ScoreDebug::fmt(&self.as_str(), f, spec) } } impl ScoreDebug for core::str::Utf8Error { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { let mut debug_struct = DebugStruct::new(f, spec, "Utf8Error"); debug_struct .field("valid_up_to", &self.valid_up_to()) @@ -77,7 +77,7 @@ impl ScoreDebug for core::str::Utf8Error { } impl ScoreDebug for std::string::FromUtf8Error { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { let mut debug_struct = DebugStruct::new(f, spec, "FromUtf8Error"); debug_struct .field("bytes", &self.as_bytes()) @@ -87,7 +87,7 @@ impl ScoreDebug for std::string::FromUtf8Error { } impl ScoreDebug for core::time::Duration { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { f.write_f64(&self.as_secs_f64(), spec)?; f.write_str("s", spec) } @@ -96,7 +96,7 @@ impl ScoreDebug for core::time::Duration { macro_rules! impl_debug_for_t_casted { ($ti:ty, $to:ty, $fn:ident) => { impl ScoreDebug for $ti { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { let casted = <$to>::try_from(*self).map_err(|_| Error)?; f.$fn(&casted, spec) } @@ -114,57 +114,57 @@ impl_debug_for_t_casted!(usize, u32, write_u32); impl_debug_for_t_casted!(usize, u64, write_u64); impl ScoreDebug for &T { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { ScoreDebug::fmt(&**self, f, spec) } } impl ScoreDebug for &mut T { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { ScoreDebug::fmt(&**self, f, spec) } } impl ScoreDebug for [T] { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { let mut debug_list = DebugList::new(f, spec); debug_list.entries(self.iter()).finish() } } impl ScoreDebug for [T; N] { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { ScoreDebug::fmt(&&self[..], f, spec) } } impl ScoreDebug for core::array::TryFromSliceError { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { let mut debug_tuple = DebugTuple::new(f, spec, "TryFromSliceError"); debug_tuple.field(&()).finish() } } impl ScoreDebug for Vec { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { ScoreDebug::fmt(&**self, f, spec) } } impl ScoreDebug for std::rc::Rc { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { ScoreDebug::fmt(&**self, f, spec) } } impl ScoreDebug for std::sync::Arc { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { ScoreDebug::fmt(&**self, f, spec) } } impl ScoreDebug for Option { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { match self { Some(v) => { let outer_spec = FormatSpec::new(); @@ -177,8 +177,27 @@ impl ScoreDebug for Option { } } +impl ScoreDebug for Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { + match self { + Ok(v) => { + let outer_spec = FormatSpec::new(); + f.write_str("Ok(", &outer_spec)?; + ScoreDebug::fmt(v, f, spec)?; + f.write_str(")", &outer_spec) + }, + Err(e) => { + let outer_spec = FormatSpec::new(); + f.write_str("Err(", &outer_spec)?; + ScoreDebug::fmt(e, f, spec)?; + f.write_str(")", &outer_spec) + }, + } + } +} + impl ScoreDebug for Box { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { ScoreDebug::fmt(&**self, f, spec) } } @@ -188,27 +207,27 @@ where K: ScoreDebug, V: ScoreDebug, { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { let mut debug_map = DebugMap::new(f, spec); debug_map.entries(self.iter()).finish() } } impl ScoreDebug for std::sync::PoisonError { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { let mut debug_struct = DebugStruct::new(f, spec, "PoisonError"); debug_struct.finish_non_exhaustive() } } impl ScoreDebug for (A, B) { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { DebugTuple::new(f, spec, "").field(&self.0).field(&self.1).finish() } } impl ScoreDebug for (A, B, C) { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { DebugTuple::new(f, spec, "") .field(&self.0) .field(&self.1) @@ -218,7 +237,7 @@ impl ScoreDebug for (A, B, C) { } impl ScoreDebug for (A, B, C, D) { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { DebugTuple::new(f, spec, "") .field(&self.0) .field(&self.1) @@ -229,7 +248,7 @@ impl ScoreDebug for } impl ScoreDebug for (A, B, C, D, E) { - fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result { + fn fmt(&self, f: Writer, spec: &FormatSpec) -> FmtResult { DebugTuple::new(f, spec, "") .field(&self.0) .field(&self.1) @@ -378,6 +397,14 @@ mod tests { common_test_debug(Option::::None); } + #[test] + fn test_result_debug() { + let r1: Result = Ok(123); + common_test_debug(r1); + let r2: Result = Err("fail"); + common_test_debug(r2); + } + #[test] fn test_box_debug() { common_test_debug(Box::new(432.1));