fitzgen / bumpalo

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

Continuous address allocated #116

Closed sundy-li closed 2 years ago

sundy-li commented 2 years ago

Hello, can this crate support Continuous address allocated? The function returns the first allocated reference. And we can use offset to access b and c.

pub fn alloc_with<F,  A, B, C>(&self, a: A, b: B, c: B) -> &mut A
fitzgen commented 2 years ago

You can use a #[repr(C)] struct to do this:

#[repr(C)]
struct Abc {
    a: A,
    b: B,
    c: C,
}

let bump = Bump::new();
let abc = bump.alloc_with(|| Abc { a: foo(), b: bar(), c: baz() });
do_stuff(&mut abc.a);

I don't think this is a common enough usage pattern to warrant adding sugar to bumpalo itself, however.