Skip to content

Commit 30c30a6

Browse files
authored
Implement MallocSizeOf for SmallVec (v2) (#369)
* Implement MallocSizeOf for SmallVec (v2) Signed-off-by: Nico Burns <nico@nicoburns.com> * Bump malloc_size_of crate to version with lower MSRV Signed-off-by: Nico Burns <nico@nicoburns.com> --------- Signed-off-by: Nico Burns <nico@nicoburns.com>
1 parent 9a23ebf commit 30c30a6

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

.github/workflows/main.yml

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ jobs:
3939
- name: Cargo test w/ serde
4040
run: cargo test --verbose --features serde
4141

42+
- name: Cargo test w/ malloc_size_of
43+
run: cargo test --verbose --features malloc_size_of
44+
4245
- name: Cargo check w/o default features
4346
if: matrix.toolchain == 'nightly'
4447
run: cargo check --verbose --no-default-features

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extract_if = []
2020

2121
[dependencies]
2222
serde = { version = "1", optional = true, default-features = false }
23+
malloc_size_of = { version = "0.1.1", optional = true, default-features = false }
2324

2425
[dev-dependencies]
2526
bincode = "1.0.1"

src/lib.rs

+24
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ use core::ptr::copy;
9292
use core::ptr::copy_nonoverlapping;
9393
use core::ptr::NonNull;
9494

95+
#[cfg(feature = "malloc_size_of")]
96+
use malloc_size_of::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps};
9597
#[cfg(feature = "serde")]
9698
use serde::{
9799
de::{Deserialize, Deserializer, SeqAccess, Visitor},
@@ -2176,6 +2178,28 @@ where
21762178
}
21772179
}
21782180

2181+
#[cfg(feature = "malloc_size_of")]
2182+
impl<T, const N: usize> MallocShallowSizeOf for SmallVec<T, N> {
2183+
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
2184+
if self.spilled() {
2185+
unsafe { ops.malloc_size_of(self.as_ptr()) }
2186+
} else {
2187+
0
2188+
}
2189+
}
2190+
}
2191+
2192+
#[cfg(feature = "malloc_size_of")]
2193+
impl<T: MallocSizeOf, const N: usize> MallocSizeOf for SmallVec<T, N> {
2194+
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
2195+
let mut n = self.shallow_size_of(ops);
2196+
for elem in self.iter() {
2197+
n += elem.size_of(ops);
2198+
}
2199+
n
2200+
}
2201+
}
2202+
21792203
#[cfg(feature = "std")]
21802204
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
21812205
impl<const N: usize> io::Write for SmallVec<u8, N> {

0 commit comments

Comments
 (0)