add seeking in consumers

This commit is contained in:
Geoffroy Couprie 2015-02-20 13:29:27 +01:00
parent d6960981eb
commit 1eecaba724
2 changed files with 27 additions and 3 deletions

View File

@ -59,6 +59,8 @@ use internal::Err;
#[derive(Debug,PartialEq,Eq,Copy)]
pub enum ConsumerState {
Await(usize),
Goto(u64),
Offset(i64),
Incomplete,
ConsumerDone,
ConsumerError(Err)
@ -74,7 +76,19 @@ pub trait Consumer {
fn run(&mut self, producer: &mut Producer) {
let mut acc: Vec<u8> = Vec::new();
//let mut v2: Vec<u8> = Vec::new();
let mut isGoto = false;
let mut isOffset = false;
let mut goto:u64 = 0;
let mut offset:i64 = 0;
loop {
if isGoto {
producer.seek(goto);
isGoto = false;
}
if isOffset {
producer.seek_offset(offset);
isOffset = false;
}
let state = producer.produce();
let mut eof = false;
let mut end = false;
@ -117,6 +131,16 @@ pub trait Consumer {
acc = tmp;
println!("acc size: {}", acc.len());
},
Goto(i) => {
goto = i;
isGoto = true;
acc.clear();
},
Offset(i) => {
offset = i;
isOffset = true;
acc.clear();
},
Incomplete => {
println!("incomplete");
}

View File

@ -57,7 +57,7 @@ pub enum ProducerState<O> {
/// A producer implements the produce method, currently working with u8 arrays
pub trait Producer {
fn produce(&mut self) -> ProducerState<&[u8]>;
fn seek_to(&mut self, position:u64) -> Option<u64>;
fn seek(&mut self, position:u64) -> Option<u64>;
fn seek_offset(&mut self, offset:i64) -> Option<u64>;
}
@ -104,7 +104,7 @@ impl Producer for FileProducer {
}
}
fn seek_to(&mut self, position: u64) -> Option<u64> {
fn seek(&mut self, position: u64) -> Option<u64> {
self.file.seek(SeekFrom::Start(position)).ok()
}
@ -158,7 +158,7 @@ impl<'x> Producer for MemProducer<'x> {
}
}
fn seek_to(&mut self, position: u64) -> Option<u64> {
fn seek(&mut self, position: u64) -> Option<u64> {
if position as usize > self.length {
self.index = self.length
} else {