3

What type should I use to store a BufferedReader line iterator in a structure? Here's what I tried:

struct S<'a, R: 'a> {
    iter: std::io::Lines<'a, std::io::buffered::BufferedReader<R>>
}

pub fn read<'a, A, R: std::io::Reader>(reader: R) -> S<'a, R> {
    let mut br = std::io::BufferedReader::new(reader);
    S { iter: br.lines() }
}

#[test]
fn test() {
    let mut reader = std::io::BufReader::new("test".as_bytes());
    read(reader);
}

Compile fails with this output:

/home/nicholasbishop/rust-so-test-00/src/lib.rs:11:30: 11:66 error: struct `BufferedReader` is private
/home/nicholasbishop/rust-so-test-00/src/lib.rs:11     iter: std::io::Lines<'a, std::io::buffered::BufferedReader<R>>
                                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Build failed, waiting for other jobs to finish...
/home/nicholasbishop/rust-so-test-00/src/lib.rs:22:5: 22:9 error: unable to infer enough type information about `_`; type annotations required
/home/nicholasbishop/rust-so-test-00/src/lib.rs:22     read(reader);
                                                   ^~~~

rustc version: rustc 0.13.0-nightly (eedfc0779 2014-11-25 22:36:59 +0000)

Nicholas Bishop
  • 1,141
  • 9
  • 21

2 Answers2

1

Yes, the original path of that data type is std::io::buffered::BufferedReader. But it’s not public there, for the entire buffered module is private. It’s exported as std::io::BufferedReader and this is the path you should use it from, exclusively.

Also, if you’re wanting to just use it, returning the type Lines<'a, BufferedReader<R>> (or even giving an alias to it, type S<'a, R: 'a> = Lines<'a, BufferedReader<R>>) is perfectly fine.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
  • Thanks, that helped. Fixing that error lead me into all sorts of other errors, but I think I've got a somewhat working solution now that I will post as a separate answer. – Nicholas Bishop Nov 27 '14 at 04:50
0

Here's a solution that seems to work:

// Tested with: rustc 0.13.0-nightly (eedfc0779 2014-11-25 22:36:59 +0000)

use std::io::BufferedReader;

pub struct S<'a, R: Reader + 'a> {
    reader: BufferedReader<R>,
    iter: Option<std::io::Lines<'a, BufferedReader<R>>>
}

impl<'a, R: Reader> S<'a, R> {
    pub fn new<R: Reader>(reader: R) -> S<'a, R> {
        S { reader: BufferedReader::new(reader), iter: None }
    }

    // It seems like this code could live in Iterator::next, but I get
    // an error because the explicit lifetime for the self parameter
    // seems to be necessary for this code, but the Iterator trait
    // doesn't expect that and fails to compile. Having it call this
    // helper method seems to infer the correct thing.
    fn init_iter(&'a mut self) {
        match self.iter {
            None => {
                self.iter = Some(self.reader.lines());
            }
            _ => {
            }    
        }
    }
}

impl<'a, R: Reader> Iterator<String> for S<'a, R> {
    fn next(&mut self) -> Option<String> {
        self.init_iter();

        match self.iter {
            Some(ref mut iter) => {
                match iter.next() {
                    Some(line) => {
                        Some(line.unwrap())
                    }
                    None => {
                        None
                    }
                }
            }
            None => {
                None
            }
        }
    }
}

pub fn read<'a, R: Reader>(reader: R) -> S<'a, R> {
    S::<R>::new(reader)
}

#[test]
fn test1() {
    let reader = std::io::BufReader::new("a\nb\n".as_bytes());
    let mut iter = read(reader);
    assert!(iter.next().unwrap().as_slice() == "a\n");
    assert!(iter.next().unwrap().as_slice() == "b\n");
    assert!(iter.next() == None);
}
Nicholas Bishop
  • 1,141
  • 9
  • 21