diff --git a/parity-util-mem/CHANGELOG.md b/parity-util-mem/CHANGELOG.md index b6f2dba48..855ea42fe 100644 --- a/parity-util-mem/CHANGELOG.md +++ b/parity-util-mem/CHANGELOG.md @@ -6,8 +6,11 @@ The format is based on [Keep a Changelog]. ## [Unreleased] +## [0.5.1] - 2019-02-05 +- Add different mode for malloc_size_of_is_0 macro dealing with generics #334. [#332](https://github.com/paritytech/parity-common/pull/334) + ## [0.5.0] - 2019-02-05 -- Bump parking_lot to 0.10. [#332](https://github.com/paritytech/parity-common/pull/332 +- Bump parking_lot to 0.10. [#332](https://github.com/paritytech/parity-common/pull/332) ## [0.4.2] - 2020-02-04 - Implementation of `MallocSizeOf` for `BTreeSet`. [#325](https://github.com/paritytech/parity-common/pull/325) diff --git a/parity-util-mem/Cargo.toml b/parity-util-mem/Cargo.toml index 02e0e222a..d9024ea1c 100644 --- a/parity-util-mem/Cargo.toml +++ b/parity-util-mem/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "parity-util-mem" -version = "0.5.0" +version = "0.5.1" authors = ["Parity Technologies "] repository = "https://github.com/paritytech/parity-common" description = "Collection of memory related utilities" diff --git a/parity-util-mem/src/malloc_size.rs b/parity-util-mem/src/malloc_size.rs index 864c94abd..fccdb9bcd 100644 --- a/parity-util-mem/src/malloc_size.rs +++ b/parity-util-mem/src/malloc_size.rs @@ -564,28 +564,68 @@ impl MallocSizeOf for parking_lot::RwLock { } } +/// Implement notion of 0 allocation size for some type(s). +/// +/// if used for generics, by default it will require that generaic arguments +/// should implement `MallocSizeOf`. This can be avoided with passing "any: " +/// in front of type list. +/// +/// ```rust +/// use parity_util_mem::{malloc_size, malloc_size_of_is_0}; +/// +/// struct Data

{ +/// phantom: std::marker::PhantomData

, +/// } +/// +/// malloc_size_of_is_0!(any: Data

); +/// +/// // MallocSizeOf is NOT implemented for [u8; 333] +/// assert_eq!(malloc_size(&Data::<[u8; 333]> { phantom: std::marker::PhantomData }), 0); +/// ``` +/// +/// and when no "any: " +/// +/// use parity_util_mem::{malloc_size, malloc_size_of_is_0}; +/// +/// struct Data { pub T } +/// +/// // generic argument (`T`) must be `impl MallocSizeOf` +/// malloc_size_of_is_0!(Data); +/// +/// assert_eq!(malloc_size(&Data(0u8), 0); +/// ``` #[macro_export] macro_rules! malloc_size_of_is_0( - ($($ty:ty),+) => ( - $( - impl $crate::MallocSizeOf for $ty { - #[inline(always)] - fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize { - 0 - } - } - )+ - ); - ($($ty:ident<$($gen:ident),+>),+) => ( - $( - impl<$($gen: $crate::MallocSizeOf),+> $crate::MallocSizeOf for $ty<$($gen),+> { - #[inline(always)] - fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize { - 0 - } - } - )+ - ); + ($($ty:ty),+) => ( + $( + impl $crate::MallocSizeOf for $ty { + #[inline(always)] + fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize { + 0 + } + } + )+ + ); + (any: $($ty:ident<$($gen:ident),+>),+) => ( + $( + impl<$($gen),+> $crate::MallocSizeOf for $ty<$($gen),+> { + #[inline(always)] + fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize { + 0 + } + } + )+ + ); + ($($ty:ident<$($gen:ident),+>),+) => ( + $( + impl<$($gen: $crate::MallocSizeOf),+> $crate::MallocSizeOf for $ty<$($gen),+> { + #[inline(always)] + fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize { + 0 + } + } + )+ + ); ); malloc_size_of_is_0!(bool, char, str); @@ -763,4 +803,16 @@ mod tests { // ~36 per value assert!(crate::malloc_size(&set) > 3000); } + + #[test] + fn special_malloc_size_of_0() { + struct Data

{ + phantom: std::marker::PhantomData

, + } + + malloc_size_of_is_0!(any: Data

); + + // MallocSizeOf is not implemented for [u8; 333] + assert_eq!(crate::malloc_size(&Data::<[u8; 333]> { phantom: std::marker::PhantomData }), 0); + } }