Skip to content

Commit

Permalink
Implimented GetSize for Arc<str>, tokio sync and fixed size of the &str
Browse files Browse the repository at this point in the history
  • Loading branch information
іук committed Feb 14, 2024
1 parent 3e22c96 commit e7d7519
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
58 changes: 53 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -140,9 +139,12 @@ impl<T> GetSize for PhantomData<T> {}
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>
Expand Down Expand Up @@ -334,6 +336,12 @@ impl<T> GetSize for Arc<T> where T: GetSize {
}
}

impl GetSize for Arc<str> {
fn get_size(&self) -> usize {
std::mem::size_of::<usize>() + (&**self).get_size()
}
}

impl<T> GetSize for Option<T> where T: GetSize {
fn get_heap_size(&self) -> usize {
match self {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -448,3 +460,39 @@ impl<T> GetSize for Box<[T]> {
total
}
}

#[cfg(feature = "tokio")]
impl<T> GetSize for tokio::sync::mpsc::UnboundedSender<T> {}

#[cfg(feature = "tokio")]
impl<T> GetSize for tokio::sync::mpsc::WeakUnboundedSender<T> {}

#[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 <T: AsyncRead> GetSize for tokio::io::BufReader<T> {
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)
}
}
7 changes: 7 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str> = Arc::from(str_text);
assert_eq!(arc.get_size(), std::mem::size_of::<usize>() + std::mem::size_of_val(str_text));
}

0 comments on commit e7d7519

Please sign in to comment.