Open rw opened 4 years ago
I think that the standard library Box
is special cased by the language here.
You should be able to create a &mut dyn Future
from a &mut MyFuture
that was allocated in a bump.
And if we had a public, unsafe constructor for bumpalo::boxed::Box
that took a &mut
then you could do
MyFuture
in the bump, resulting in a &mut MyFuture
&mut MyFuture
to &mut dyn Future
bumpalo::boxed::Box
from the &mut dyn Future
You would probably want to wrap that up in a method (unfortunately, I don't think the type system can express this generically, so we can't do it in this crate).
I think an unsafe constructor is reasonable to support, so if you want to open a PR, I can review+merge it.
@fitzgen Would the function look like this?
impl<'a, T: ?Sized> Box<'a, T> {
#[inline]
pub unsafe fn from_mut(x: &mut T) -> Self {
Box(x)
}
or
impl<'a, T: ?Sized> Box<'a, T> {
#[inline]
pub unsafe fn from_mut(x: &mut T) -> Self {
Box(&mut *x)
}
It would look like the first, but additionally the parameter would use the 'a
lifetime: x: &'a mut T
std::boxed::Box
implements CoerceUnsized
which is a nightly-only trait for doing this. You can fake it with unsafe { BumpaloBox::from_raw(BumpaloBox::into_raw(node) as *mut dyn Whatever) }
, or by the above additional function...
However it doesn't work too well. The fact that Box
is just a wrapper for a &'a mut T
causes a lot of issues with trait object invariance. You need to use raw pointers like the real Box
to make it function properly.
To reduce memory allocations, I'm trying to use bumpalo to create my own Boxed futures type.
I'm basing this off of an idiom present in the futures crate:
https://docs.rs/futures/0.3.5/futures/future/type.BoxFuture.html
When I try this with bumpalo::boxed::Box, however, it seems that I can't convert an
impl Future
into adyn Future
the same way that I can with std::boxed::Box.Here is some example code demonstrating the issue (I wish the playground had bumpalo!):
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=80eeec8261dc28169cfc968525ed7e43
Is there something I'm missing with how to use this library? Thank you!