use mod_alloc::ModAlloc;
#[global_allocator]
static GLOBAL: ModAlloc = ModAlloc::new();
struct AllocOnDrop {
payload: Vec<u8>,
}
impl Drop for AllocOnDrop {
fn drop(&mut self) {
let scratch: Vec<u8> = Vec::with_capacity(self.payload.len() * 2);
std::hint::black_box(&scratch);
}
}
#[test]
fn allocator_handles_drop_chains_without_recursion() {
let before = GLOBAL.snapshot();
let mut chains: Vec<AllocOnDrop> = Vec::with_capacity(100);
for i in 0..100 {
chains.push(AllocOnDrop {
payload: vec![i as u8; 32],
});
}
let mut acc = String::new();
for i in 0..200 {
acc.push_str(&format!("item-{i:08}-{i:08x}\n"));
}
assert!(!acc.is_empty());
drop(chains);
let after = GLOBAL.snapshot();
assert!(
after.alloc_count > before.alloc_count,
"expected counter movement; got {} -> {}",
before.alloc_count,
after.alloc_count
);
}