I'm writing a parser. My main parse
function takes a &mut Box<Read>
. I can get this to work with stdin
, but can't get it to work with stdin.lock()
:
use std::io::{self, Read};
fn main() {
let stdin = io::stdin();
let mut handle = stdin.lock();
let mut read_box: Box<Read> = Box::new(handle);
read(&mut read_box);
}
fn parse(source: &mut Box<Read>) {
...
}
The error:
bb.rs:5:22: 5:27 error: `stdin` does not live long enough
bb.rs:5 let mut handle = stdin.lock();
^~~~~
note: reference must be valid for the static lifetime...
bb.rs:4:29: 8:2 note: ...but borrowed value is only valid for the block suffix following statement 0 at 4:28
...
...
Any idea what I'm doing wrong?
Tangent: I'm using Box<Read>
instead of a T:Read
type parameter because I don't want to the parser code to be specialized for each type of input source (stdin, file, etc). Does that make sense or am I misunderstanding things?