Skip to content

Commit 68f3529

Browse files
authored
impl MallocSizeOf for BTreeSet. (#11)
1 parent 8568cba commit 68f3529

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

malloc_size_of/src/impls.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use core::sync::atomic::{AtomicU16, AtomicU32, AtomicU64, AtomicU8, AtomicUsize}
2323

2424
use alloc::borrow::{Cow, ToOwned};
2525
use alloc::boxed::Box;
26-
use alloc::collections::{BTreeMap, VecDeque};
26+
use alloc::collections::{BTreeMap, BTreeSet, VecDeque};
2727
use alloc::string::String;
2828
use alloc::vec::Vec;
2929

@@ -318,6 +318,34 @@ where
318318
}
319319
}
320320

321+
impl<T> MallocShallowSizeOf for BTreeSet<T>
322+
where
323+
T: Ord,
324+
{
325+
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
326+
if ops.has_malloc_enclosing_size_of() {
327+
self.iter()
328+
.next()
329+
.map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) })
330+
} else {
331+
self.len() * (size_of::<T>() + size_of::<usize>())
332+
}
333+
}
334+
}
335+
336+
impl<T> MallocSizeOf for BTreeSet<T>
337+
where
338+
T: Ord + MallocSizeOf,
339+
{
340+
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
341+
let mut n = self.shallow_size_of(ops);
342+
for v in self.iter() {
343+
n += v.size_of(ops);
344+
}
345+
n
346+
}
347+
}
348+
321349
#[cfg(feature = "std")]
322350
impl<T, S> MallocShallowSizeOf for HashSet<T, S>
323351
where

0 commit comments

Comments
 (0)