You still haven't supplied new_from_generic_with_options
but from the errors, I'm going to guess it's something like:
impl <'a, R: 'a + Read> Reader<'a, R> {
pub fn new_from_generic_with_options(_: R, _: ReaderOptions) -> Self {
todo!()
}
}
In which case, the first error is because in this context:
impl <'a> Reader<'a, &'a [u8]> {
pub fn new_with_options(buffer: &'a [u8], options: ReaderOptions) -> Self
Self
is the thing you are implementing -- a Reader<'a, &'a [u8]>
. But you're not returning that, you're returning a Reader<'a, Cursor<&'a [u8]>>
. You could change the signature, but since returning Self
is the point of these methods, you probably want to change what you are implementing instead. Here's a playground that does that. (If you wanted to see what just changing the signature looks like, you can see that in this playground.)
In the second version, you're implementing on Reader<'a, Cursor<T>>
where T
is unconstrained, but you're still returning a Reader<'a, Cursor<&'a [u8]>>
. You could constrain T
with T: AsRef<[u8]>
to change your trait bound error into a type mismatch error, but it's still an error, because you're still returning a Reader<'a, Cursor<&'a [u8]>>
. T
might be something that's not &[u8]
.
So perhaps the confusion is coming in with how generic work. In the second version, when you said:
impl <'a, T> Reader<'a, Cursor<T>> {
You're basically saying, this is an implementation for any type T
. The users of this implementation can pick any T
they want. You can constrain their choices with trait bounds, like T: AsRef<[u8]>
, but they still get to choose the T
. So when you say that you're going to return Self
, it has to be a Reader<'a, Cursor<T>>
-- and that T
has to be the one they chose, not a specific type like &[u8]
.
Here's a playground for that second version. The changes are:
- I added a bound for
T: AsRef<[u8]> + 'a
so you can create the Cursor
- I changed
new
and new_with_options
to take T
and not a &[u8]
And now it creates a Cursor<T>
and returns a Reader<'a, Cursor<T>>
, which matches Self
, as required.