fitzgen / bumpalo

A fast bump allocation arena for Rust
https://docs.rs/bumpalo
Apache License 2.0
1.36k stars 111 forks source link

Vec<'bump, T> does not call Drop for its items #133

Closed wada314 closed 2 years ago

wada314 commented 2 years ago

Hello, I'm not sure if this is an intentional design decision or just a bug though, Vec<'bump, T> does not call the owning items' Drop when itself is dropped. I'll send a PR to add the Drop impl for Vec later so please utilize it if it's not an intended behavior, thanks! 😃

#[cfg(test)]
use bumpalo::{boxed::Box, collections::Vec, Bump};
use std::cell::RefCell;
#[cfg(test)]
use std::ops::Deref;

struct Foo<'a>(&'a RefCell<String>);
impl<'a> Drop for Foo<'a> {
    fn drop(&mut self) {
        self.0.borrow_mut().push_str("Dropped!");
    }
}

#[test]
fn test_bare() {
    let buffer = RefCell::new(String::new());
    {
        let _bare_foo = Foo(&buffer);
    }
    // OK
    assert_eq!("Dropped!", buffer.borrow().deref());
}

#[test]
fn test_boxed() {
    let buffer = RefCell::new(String::new());
    let bump = Bump::new();
    {
        let _boxed_foo = Box::new_in(Foo(&buffer), &bump);
    }
    // OK
    assert_eq!("Dropped!", buffer.borrow().deref());
}

#[test]
fn test_vec() {
    let buffer = RefCell::new(String::new());
    let bump = Bump::new();
    {
        let mut vec_foo = Vec::new_in(&bump);
        vec_foo.push(Foo(&buffer));
    }
    // Fails!!
    assert_eq!("Dropped!", buffer.borrow().deref());
}
wada314 commented 2 years ago

Thank you for merging it! 🙇