Any way to support dyn types in custom Box?

โš“ Rust    ๐Ÿ“… 2026-03-22    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 3      

surdeus

In my implementation of Box, to allow boxing of dyn values, I want to add

use std::ops::CoerceUnsized;
use std::marker::Unsize;
impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {}

but this is not possible under stable, it requires the features unsize and coerce_unsized.

I donโ€™t think there is any way around this, is that correct?

It seems a shame that in 2026 dyn types are not yet fully supported on stable. I am thinking about a dirty work-around, such as temporarily switching the Global allocator to bump allocate for the current thread, but it seems rather nasty.

Or maybe just switch to using nightly until this is stabilised, I guess.

Well, I decided to have a feature that requires nightly, that seems a reasonable way to proceed.

6 posts - 2 participants

Read full topic

๐Ÿท๏ธ Rust_feed