-
Notifications
You must be signed in to change notification settings - Fork 120
Can't unsize bumpalo's Box #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You can perhaps do something like let x = arena.alloc(x);
let x = x as &mut dyn Debug;
let x = unsafe { BumpBox::from_raw(x) }; Otherwise, you can use the cargo feature to enable |
I'd say neither is usable in a "production" project that disallows both nightly and unsafe :-(. If I really needed that (instead of preferring not to allocate on the heap), I'd go with creating a separate crate to isolate that unsafe, but that's probably not worth it. Would it be OK to put such code into a method inside bumpalo? Can I send a PR? |
Unfortunately it is impossible to make a generic version of that snippet in today's Rust:
|
Well, the #![feature(unsize)]
use std::fmt::Debug;
use std::marker::Unsize;
use bumpalo::Bump;
use bumpalo::boxed::Box as BumpBox;
fn box_it<'a, T, U>(arena: &'a Bump, x: T) -> BumpBox<'a, U>
where
T: Unsize<U>,
U: ?Sized + 'static,
{
let x = arena.alloc(x);
let x = x as &mut _;
let x = unsafe { BumpBox::from_raw(x) };
x
}
fn main() {
let bump = Bump::new();
let x = box_it::<usize, dyn Debug>(&bump, 42);
} I wonder if there might be some other conversion trait that would also be usable. But it seems I guess there would be a possibility of a macro to hide the unsafety, but that's ugly :-(. |
Is there any good work around for |
I think it is all about variance of type. AFAIU, if |
A related issue is that even if you manage to construct a fn f(b: bumpalo::boxed::Box<'_, dyn FnOnce()>) {
b();
}
and The former works with the stdlib
This doesn't work for my use case as the stdlib box is EDIT: I ended up writing an allocator wrapper with a no-op
Then used that with the stdlib |
Hello
It seems the bumpalo's Box doesn't implement CoerceUnsize, like the usual
Box
does. I'm not sure how one should be able to create upcastable smart pointers, as that's a nightly-only API :-(.This code does not compile (and I can't figure out any equivalent that does):
This fails with
mismatched types
. Tryinginto()
and similar doesn't help.I've looked at this example around
downcast
and I'm pretty sure the example uses the ordinarystd::boxed::Box
, not the bumpalo's one.Is there a way to do the upcast I'm not seeing? Should the example be modified to use that?
The text was updated successfully, but these errors were encountered: