diff --git a/parity-util-mem/src/lib.rs b/parity-util-mem/src/lib.rs index 23c07e27b..2d45cf9b4 100644 --- a/parity-util-mem/src/lib.rs +++ b/parity-util-mem/src/lib.rs @@ -73,10 +73,17 @@ pub use malloc_size::{MallocSizeOf, MallocSizeOfOps}; pub use parity_util_mem_derive::*; +/// Heap size of structure. +/// +/// Structure can be anything that implements MallocSizeOf. +pub fn malloc_size(t: &T) -> usize { + MallocSizeOf::size_of(t, &mut allocators::new_malloc_size_ops()) +} + #[cfg(feature = "std")] #[cfg(test)] mod test { - use super::MallocSizeOfExt; + use super::{malloc_size, MallocSizeOf, MallocSizeOfExt}; use std::sync::Arc; #[test] @@ -85,4 +92,12 @@ mod test { let s = val.malloc_size_of(); assert!(s > 0); } + + #[test] + fn test_dyn() { + trait Augmented: MallocSizeOf {} + impl Augmented for Vec {} + let val: Arc = Arc::new(vec![0u8; 1024]); + assert!(malloc_size(&*val) > 1000); + } }