diff --git a/Cargo.toml b/Cargo.toml index a652a4b..3ea4fcb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,8 @@ categories = ["memory-management", "caching"] [dependencies] get-size-derive = { version = "^0.1.3", optional = true } +tokio = { version = "^1", optional = true , features = ["sync", "time", "io-util"]} +serde_json = { version = "^1", optional = true} [features] default = [] diff --git a/src/lib.rs b/src/lib.rs index 65c3057..9d5e4f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,8 +55,6 @@ use std::time::{Instant, Duration, SystemTime}; pub use get_size_derive::*; - - #[cfg(test)] mod tests; @@ -140,9 +138,12 @@ impl GetSize for PhantomData {} impl GetSize for PhantomPinned {} impl GetSize for Instant {} -impl GetSize for Duration {} impl GetSize for SystemTime {} - +impl GetSize for Duration { + fn get_stack_size() -> usize { + 12 + } +} impl<'a, T> GetSize for Cow<'a, T> @@ -334,6 +335,18 @@ impl GetSize for Arc where T: GetSize { } } +impl GetSize for Arc { + fn get_size(&self) -> usize { + std::mem::size_of::() + (&**self).get_size() + } +} + +impl GetSize for Box { + fn get_size(&self) -> usize { + std::mem::size_of::() + (&**self).get_size() + } +} + impl GetSize for Option where T: GetSize { fn get_heap_size(&self) -> usize { match self { @@ -375,7 +388,11 @@ impl GetSize for String { } } -impl GetSize for &str {} +impl GetSize for &str { + fn get_size(&self) -> usize { + std::mem::size_of_val(*self) + } +} impl GetSize for std::ffi::CString { fn get_heap_size(&self) -> usize { @@ -448,3 +465,39 @@ impl GetSize for Box<[T]> { total } } + +#[cfg(feature = "tokio")] +impl GetSize for tokio::sync::mpsc::UnboundedSender {} + +#[cfg(feature = "tokio")] +impl GetSize for tokio::sync::mpsc::WeakUnboundedSender {} + +#[cfg(feature = "tokio")] +impl GetSize for tokio::time::Instant {} + +#[cfg(feature = "tokio")] +impl GetSize for tokio::time::Interval { + fn get_size(&self) -> usize { + std::mem::size_of_val(self) + } +} + +#[cfg(feature = "tokio")] +impl GetSize for tokio::io::BufReader { + fn get_size(&self) -> usize { + let inner = std::mem::size_of_val(self.get_ref()); + let buf = self.buffer().len(); + let pos = 1_usize; + let cap = 1_usize; + let seek_state = 9_usize; + + inner + buf + pos + cap + seek_state + } +} + +#[cfg(feature = "serde_json")] +impl GetSize for serde_json::Value { + fn get_size(&self) -> usize { + std::mem::size_of_val(self) + } +} \ No newline at end of file diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 3da0abd..a047709 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -244,3 +244,17 @@ fn derive_newtype() { let test = TestNewType(0); assert_eq!(u64::get_stack_size(), test.get_size()); } + +#[test] +fn test_arc_str_size(){ + let str_text = "a"; + let arc: Arc = Arc::from(str_text); + assert_eq!(arc.get_size(), std::mem::size_of::() + std::mem::size_of_val(str_text)); +} + +#[test] +fn test_boxed_str_size() { + let str_text = "a"; + let boxed: Box = Box::from(str_text); + assert_eq!(boxed.get_size(), std::mem::size_of::() + std::mem::size_of_val(str_text)); +} \ No newline at end of file