Given a Box<str>
I would like to produce a Box<dyn _>
for it. Concretely:
trait StringLike {
fn length(&self) -> usize;
}
impl StringLike for str {
fn length(&self) -> usize {
self.len()
}
}
fn main() {
let x1: String = "test".to_owned();
let x2: Box<str> = x1.into_boxed_str();
let x3: Box<dyn StringLike> = x2;
}
This gives an error on x3
stating that:
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/main.rs:15:35
|
15 | let x3: Box<dyn StringLike> = x2;
| ^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `str`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: required for the cast to the object type `dyn StringLike`
The second link explains that:
Due to Rust’s need to know certain details, such as how much space to allocate for a value of a particular type, there is a corner of its type system that can be confusing: the concept of dynamically sized types.
I understand that, but in this instance I want to go from a Box<str>
(which I believe internally looks like *str
) to a Box<dyn StringLike>
(which I believe internally looks like (*str, *vtable_Stringlike)
), so I don't see the conceptual requirement to know the type of str
at compile-time.
Is it possible to do the conversion? If not, is that for conceptual reasons or just currently unavailable?