Mercurial > lbo > hg > localmr
changeset 8:bb148e9edb78
Implement IntoIterator<IntoIter=RecordIterator> for WLR
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 30 Jan 2016 21:57:26 +0000 |
parents | f68ce3a86754 |
children | 020bfec8699f |
files | src/formats/writelog.rs src/util.rs |
diffstat | 2 files changed, 16 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/src/formats/writelog.rs Sat Jan 30 21:49:57 2016 +0000 +++ b/src/formats/writelog.rs Sat Jan 30 21:57:26 2016 +0000 @@ -7,6 +7,9 @@ use std::vec; use std::string; +use mapreducer::{Record}; +use util::{RecordIterator}; + /// A length-prefixed record stream named for the original use case, /// which was to write a log of all write operations to a database. /// @@ -201,10 +204,15 @@ } } -impl Iterator for WriteLogReader { +/// Iterator type for WriteLogReader; used to implement IntoIterator +struct WLRIteratorAdapter { + wlr: WriteLogReader, +} + +impl Iterator for WLRIteratorAdapter { type Item = String; fn next(&mut self) -> Option<String> { - let result = self.read_vec(); + let result = self.wlr.read_vec(); let convert_result; match result { @@ -219,19 +227,13 @@ } } -// Byte string implementation. -/* -impl Iterator for WriteLogReader { - type Item = vec::Vec<u8>; - fn next(&mut self) -> Option<vec::Vec<u8>> { - let result = self.read_vec(); - match result { - Err(_) => None, - Ok(v) => Some(v) - } +impl IntoIterator for WriteLogReader { + type Item = Record; + type IntoIter = RecordIterator; + fn into_iter(self) -> RecordIterator { + RecordIterator::new(Box::new(WLRIteratorAdapter{ wlr: self })) } } -*/ impl Read for WriteLogReader { fn read(&mut self, dst: &mut [u8]) -> io::Result<usize> {
--- a/src/util.rs Sat Jan 30 21:49:57 2016 +0000 +++ b/src/util.rs Sat Jan 30 21:57:26 2016 +0000 @@ -10,7 +10,7 @@ } impl RecordIterator { - fn new(it: Box<Iterator<Item=String>>) -> RecordIterator { + pub fn new(it: Box<Iterator<Item=String>>) -> RecordIterator { RecordIterator { i: it, counter: 0 } } }