I am porting some Python to Rust as a learning exercise and need to take input from either a file or stdin. I keep a handle to my input in a struct so I thought I'd just make a Box<io::Read>
but I ran into a situation where I need to seek on the input, and seek
isn't part of the Read
trait. I know you can't seek in pipes, so I'm going ahead and assuming for now that this method only gets called when the input is a file, but my problem is that I can't check that and downcast in Rust.
I know that I could use an enum for the two input types, but it seems like there should be a more elegant way to do this. And that's my question, how do you do this and not make a mess?
Is it possible to wrap stdin or a file in the same sort of buffer so I could just use that type and not worry about the type of IO?