I know that traits and slices are unsized, i.e. it's not possible to know their size at compile time, e.g. any type may implement a trait, but that type may not be sized.
Nevertheless, doesn't this example code mean that every type which implements trait Foo
needs to implement Sized
too?
trait Foo: Sized {}
struct Bar(i64);
impl Foo for Bar {}
If that's so, why doesn't this work?
impl From<Foo> for Bar {
fn from(foo: Foo) -> Bar {
Bar(64)
}
}
error[E0277]: the trait bound `Foo + 'static: std::marker::Sized` is not satisfied
--> src/main.rs:7:6
|
7 | impl From<Foo> for Bar {
| ^^^^^^^^^ `Foo + 'static` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `Foo + 'static`
I want to provide to the consumer of the library a type (let's name it Bar
) and make it possible to convert to Bar
from any other type which implements a particular trait (let's name it Foo
).
I solved it by passing Foo
by the reference instead of the value, but I'm not sure why the compiler complains if it's required for implementors to be Sized
.