Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2018"
name = "thin-vec"
version = "0.2.13"
version = "0.2.14"
authors = ["Aria Beingessner <[email protected]>"]
description = "A vec that takes up less space on the stack"
license = "MIT/Apache-2.0"
Expand All @@ -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)'] }
3 changes: 3 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down Expand Up @@ -1873,6 +1876,33 @@ impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for ThinVec<T> {
}
}

#[cfg(feature = "malloc_size_of")]
impl<T> MallocShallowSizeOf for ThinVec<T> {
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::<Self>(),
std::mem::size_of::<*const ()>()
);
unsafe { ops.malloc_size_of(*(self as *const Self as *const *const ())) }
}
}

#[cfg(feature = "malloc_size_of")]
impl<T: MallocSizeOf> MallocSizeOf for ThinVec<T> {
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<A, B> PartialEq<[B; $N]> for ThinVec<A> where A: PartialEq<B> {
Expand Down
Loading