In the given example Bar implements Foo and cannot be instantiated, but Rust requires me to add a Sized constraint to the Foo trait. Why?
I would understand if it would require me to have such a constraint to FooInput because it is actually instantiated in code, but I do not get why it is needed for Foo.
trait FooInput {
fn parse() -> Self;
}
trait Foo /*: Sized*/ { // uncomment `: Sized` to fix compilation error
type Input: FooInput;
fn run() {
let _input = get_input::<Self>();
//the size for values of type `Self` cannot be known at compilation time doesn't have a size known at compile-time
}
}
fn get_input<T: Foo>() -> <T as Foo>::Input {
T::Input::parse()
}
type BarInput = u32;
impl FooInput for BarInput {
fn parse() -> Self {
100
}
}
pub struct Bar;
impl Foo for Bar {
type Input = BarInput;
}
fn main() {
Bar::run()
}
The reason that it comes with a Sized bound by default is that function generics are typically an argument to the function, and function arguments must be Sized, so its convenient to require it by default.