-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implimented GetSize for Arc<str>, tokio sync and fixed size of the &str #17
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -55,8 +55,6 @@ use std::time::{Instant, Duration, SystemTime}; | |||||||||||||
pub use get_size_derive::*; | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
|
||||||||||||||
|
||||||||||||||
#[cfg(test)] | ||||||||||||||
mod tests; | ||||||||||||||
|
||||||||||||||
|
@@ -140,9 +138,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> | ||||||||||||||
|
@@ -334,6 +335,18 @@ 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() | ||||||||||||||
} | ||||||||||||||
Comment on lines
+339
to
+341
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Shouldn't this be I think your
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
impl GetSize for Box<str> { | ||||||||||||||
fn get_size(&self) -> usize { | ||||||||||||||
std::mem::size_of::<usize>() + (&**self).get_size() | ||||||||||||||
} | ||||||||||||||
Comment on lines
+345
to
+347
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: As above, this should be
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
impl<T> GetSize for Option<T> 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<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: tokio::io::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) | ||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<str> = Arc::from(str_text); | ||
assert_eq!(arc.get_size(), std::mem::size_of::<usize>() + std::mem::size_of_val(str_text)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: This is incorrect. The total size should include the stack size of the |
||
} | ||
|
||
#[test] | ||
fn test_boxed_str_size() { | ||
let str_text = "a"; | ||
let boxed: Box<str> = Box::from(str_text); | ||
assert_eq!(boxed.get_size(), std::mem::size_of::<usize>() + std::mem::size_of_val(str_text)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Why make this change for
Duration
?It seems like the default implementation based on
size_of
should be correct.