Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement seeking to the end of the content in a MemProducer #73

Merged
merged 2 commits into from
Aug 25, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions src/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,26 @@ impl<'x> Producer for MemProducer<'x> {
}
}
},
SeekFrom::End(_) => {
//FIXME: to implement
panic!("SeekFrom::End not implemented");
SeekFrom::End(i) => {
let next = if i < 0 {
(self.length as u64).checked_sub(-i as u64)
} else {
// std::io::SeekFrom documentation explicitly allows
// seeking beyond the end of the stream, so we seek
// to the end of the content if the offset is 0 or
// greater.
Some(self.length as u64)
};
match next {
// std::io:SeekFrom documentation states that it `is an
// error to seek before byte 0.' So it's the sensible
// thing to refuse to seek on underflow.
None => None,
Some(u) => {
self.index = u as usize;
Some(u)
}
}
}
}
}
Expand Down Expand Up @@ -374,6 +391,7 @@ mod tests {
use super::*;
use internal::{Needed,IResult};
use std::fmt::Debug;
use std::io::SeekFrom;

fn local_print<'a,T: Debug>(input: T) -> IResult<'a,T, ()> {
println!("{:?}", input);
Expand All @@ -400,6 +418,36 @@ mod tests {
//assert_eq!(iterations, 3);
}


#[test]
fn mem_producer_seek_from_end() {
let mut p = MemProducer::new(&b"abcdefg"[..], 1);
p.seek(SeekFrom::End(-4));
assert_eq!(p.produce(), ProducerState::Data(&b"d"[..]));
}

#[test]
fn mem_producer_seek_beyond_end() {
let mut p = MemProducer::new(&b"abcdefg"[..], 1);
p.seek(SeekFrom::End(1));
assert_eq!(p.produce(), ProducerState::Eof(&b""[..]));
}

#[test]
fn mem_producer_seek_before_start() {
let mut p = MemProducer::new(&b"abcdefg"[..], 1);
// Let's seek a bit forward in the input to ensure that
// we don't just seek to the start when we do an
// invalid seek operation.
p.seek(SeekFrom::Start(1));
let seek_result = p.seek(SeekFrom::End(-8));
// It shouldn't do any seeking
assert_eq!(seek_result, None);
// And the position should still be at the second byte
assert_eq!(p.produce(), ProducerState::Data(&b"b"[..]));
}


#[test]
#[allow(unused_must_use)]
fn file() {
Expand Down