From e7d751999ce5a5258ed3c0942ecfe0518f781e18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D1=96=D1=83=D0=BA?= <> Date: Fri, 9 Feb 2024 03:21:53 +0200 Subject: [PATCH] Implimented GetSize for Arc, tokio sync and fixed size of the &str --- Cargo.toml | 2 ++ src/lib.rs | 58 +++++++++++++++++++++++++++++++++++++++++++----- src/tests/mod.rs | 7 ++++++ 3 files changed, 62 insertions(+), 5 deletions(-) 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..7e8f6d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,8 +53,7 @@ use std::time::{Instant, Duration, SystemTime}; #[cfg(feature = "derive")] #[cfg_attr(docsrs, doc(cfg(feature = "derive")))] pub use get_size_derive::*; - - +use tokio::io::AsyncRead; #[cfg(test)] @@ -140,9 +139,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 +336,12 @@ 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 Option where T: GetSize { fn get_heap_size(&self) -> usize { match self { @@ -375,7 +383,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 +460,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..4ad2af6 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -244,3 +244,10 @@ 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)); +} \ No newline at end of file