diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 54e4fba..796c73e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -31,6 +31,8 @@ jobs: - uses: actions/checkout@v2 - name: Build run: cargo build --verbose + - name: Build (malloc_size_of) + run: cargo build --features=malloc_size_of --verbose - name: Run tests run: cargo test --verbose - name: Run tests diff --git a/Cargo.toml b/Cargo.toml index 0d74561..c5c86d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] edition = "2018" name = "thin-vec" -version = "0.2.13" +version = "0.2.14" authors = ["Aria Beingessner "] description = "A vec that takes up less space on the stack" license = "MIT/Apache-2.0" @@ -20,7 +20,11 @@ std = [] gecko-ffi = [] [dependencies] -serde = {version = "1.0", optional = true} +serde = { version = "1.0", optional = true } +malloc_size_of = { version = "0.1", default-features = false, optional = true } [dev-dependencies] serde_test = "1.0" + +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(no_global_oom_handling)'] } diff --git a/RELEASES.md b/RELEASES.md index 9e398e5..e87ad6f 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,3 +1,6 @@ +# Version 0.2.14 (2025-02-19) +* Add "malloc_size_of" feature for heap size measurement support + # Version 0.2.13 (2023-12-02) * add default-on "std" feature for no_std support diff --git a/src/lib.rs b/src/lib.rs index 6616514..39ef50b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,6 +163,9 @@ use core::{fmt, mem, ptr, slice}; use impl_details::*; +#[cfg(feature = "malloc_size_of")] +use malloc_size_of::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps}; + // modules: a simple way to cfg a whole bunch of impl details at once #[cfg(not(feature = "gecko-ffi"))] @@ -1873,6 +1876,33 @@ impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for ThinVec { } } +#[cfg(feature = "malloc_size_of")] +impl MallocShallowSizeOf for ThinVec { + fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { + if self.capacity() == 0 { + // If it's the singleton we might not be a heap pointer. + return 0; + } + + assert_eq!( + std::mem::size_of::(), + std::mem::size_of::<*const ()>() + ); + unsafe { ops.malloc_size_of(*(self as *const Self as *const *const ())) } + } +} + +#[cfg(feature = "malloc_size_of")] +impl MallocSizeOf for ThinVec { + fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { + let mut n = self.shallow_size_of(ops); + for elem in self.iter() { + n += elem.size_of(ops); + } + n + } +} + macro_rules! array_impls { ($($N:expr)*) => {$( impl PartialEq<[B; $N]> for ThinVec where A: PartialEq {