I want to create MyReader that can open a file from path or from an existing stream, my question is why rust complains about mismatch types? According to the doc, BufReader<File> already implements BufRead trait.
How can I make this code working? Thanks in advance!
impl<R> MyReader<R> means that the code that’s calling any function inside the impl block gets to choose what R is, but your implementation of new_from_path always returns the specific type BufReader<File> in place of R. The caller could request a different type for R and your new_from_path implementation has no way to deal with that, which is why it’s a hard error.
You can fix this by making the impl specific rather than generic[1].
new_from_stream is fine as it is, because the caller passes in the reader which is of type R, so the generic impl block works correctly there.
technically you can leave the impl generic and just change the return type of new_from_stream but then the caller has to specify what R is even though it never gets used, which is annoying ↩︎