pub trait Stream<'a> {
type Item;
// Required methods
fn token(&mut self, x: usize) -> StreamResult<Self::Item>;
fn junk(&mut self, x: usize);
fn pos(&self) -> usize;
}Expand description
Suppose you need to process each line of a text file. One way to do this is to read the file in as a single large string and use something
like split to turn it into a list. This works when the file is small, but because the entire file is loaded into memory, it does not
scale well when the file is large.
More commonly, the read_line function can be used to read one line at a time from a channel. This typically looks like:
// LineReadStream is just example, you can implement your own stream for reading files.
use streams_rs::*;
use io_stream::*;
let mut c = std::io::Cursor::new(b"a\n b\n c");
let mut stream = LineReadStream::new(&mut c);
let _ = smatch!(match (stream) {
[a=> b=> c =>] => {
println!("{:?} {:?} {:?}",a,b,c);
}
}); Required Associated Types§
Required Methods§
Sourcefn token(&mut self, x: usize) -> StreamResult<Self::Item>
fn token(&mut self, x: usize) -> StreamResult<Self::Item>
Returns token at x. It’s possible to cache values. FnStream for example caches all tokens returned from getter.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".