Hi folks!
I'm trying to understand why when using an extension trait to add methods, the "&mut Self" type gains +Sized requirement. My understanding of &mut T and &T is that since they are references whether T is Sized shouldn't matter since the size of the pointer is known at compile time.
trait ParserExt<E> : Parser<E> {
fn parse_field_FAILS<I,O:ParseFrom<I,E>>(&mut self, field: String, from: I) -> Result<O,E> {
self.begin_field();
let retv = O::parse_from(self, from);
self.end_field();
retv
}
}
Error:
error[E0277]: the size for values of type `Self` cannot be known at compilation time
--> src/main.rs:13:34
|
13 | let retv = O::parse_from(self, from);
| ^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `Self`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= help: consider adding a `where Self: std::marker::Sized` bound
= note: required for the cast to the object type `dyn Parser<E>`
If I make this method a normal function, it works fine:
fn parse_field_WORKS<E,I,O:ParseFrom<I,E>>(parser: &mut Parser<E>, field: String, from: I) -> Result<O,E> {
parser.begin_field();
let retv = O::parse_from(parser, from);
parser.end_field();
retv
}
Playground:
Thanks!