fitzgen / bumpalo

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

Missing Clone for Box #204

Open douglas-raillard-arm opened 1 year ago

douglas-raillard-arm commented 1 year ago

Bumpalo's Box is missing a Clone implementation, which is a pretty big departure from std's Box. Bringing it back would require storing a &Bump inside the Box in addition to the &mut T to be able to allocate a new value inside the same arena.

That is also the way std is implemented: https://doc.rust-lang.org/src/alloc/boxed.rs.html#198-201

fitzgen commented 1 year ago

This was intentional to keep bumpalo::boxed::Box a single word.

douglas-raillard-arm commented 1 year ago

Maybe that could become opt-in with a generic parameter, defaulting to current behavior ? Something like:

pub struct Box<'a, T: ?Sized, Alloc=()>(&'a mut T, alloc: Alloc);

type CloneableBox<'a, T> = Box<'a, T, &'a Bump>;

impl<'a, T: ?Sized> Clone for Box<'a, T, &'a Bump> {
      ...
}
rzvxa commented 3 weeks ago

In oxc, we achieve this with a CloneIn trait.

https://github.com/oxc-project/oxc/blob/main/crates/oxc_allocator/src/clone_in.rs something similar might be useful to you.