Simplify EbmlIterator & EbmlSlice's next() methods, fix EbmlIterator generic
(they don't have dynamic buffers to worry about)
This commit is contained in:
parent
3a92da1883
commit
d0fd4d4bc9
3 changed files with 10 additions and 26 deletions
|
@ -1,7 +1,6 @@
|
|||
use std::marker::PhantomData;
|
||||
|
||||
use ebml::FromEbml;
|
||||
use webm::WebmElement;
|
||||
|
||||
pub struct EbmlIterator<'a, T: FromEbml<'a>>(&'a [u8], PhantomData<fn() -> T>);
|
||||
|
||||
|
@ -10,17 +9,12 @@ pub fn ebml_iter<'a, T: FromEbml<'a>>(source: &'a [u8])-> EbmlIterator<'a, T> {
|
|||
}
|
||||
|
||||
impl<'a, T: FromEbml<'a>> Iterator for EbmlIterator<'a, T> {
|
||||
type Item = WebmElement<'a>;
|
||||
type Item = T;
|
||||
|
||||
fn next(&mut self) -> Option<WebmElement<'a>> {
|
||||
WebmElement::check_space(self.0).unwrap_or(None).and_then(|element_size| {
|
||||
let (element_data, rest) = self.0.split_at(element_size);
|
||||
self.0 = rest;
|
||||
match WebmElement::decode_element(element_data) {
|
||||
Err(_) => None,
|
||||
Ok(None) => panic!("Buffer was supposed to have enough data to parse element, somehow did not."),
|
||||
Ok(Some((element, _))) => Some(element)
|
||||
}
|
||||
fn next(&mut self) -> Option<T> {
|
||||
T::decode_element(self.0).unwrap_or(None).and_then(|(element, element_size)| {
|
||||
self.0 = &self.0[element_size..];
|
||||
Some(element)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
18
src/slice.rs
18
src/slice.rs
|
@ -9,19 +9,9 @@ impl<'b> EbmlEventSource for EbmlSlice<'b> {
|
|||
type Error = EbmlError;
|
||||
|
||||
fn poll_event<'a, T: FromEbml<'a>>(&'a mut self) -> Result<Async<Option<T>>, EbmlError> {
|
||||
T::check_space(self.0).and_then(|size_option| {
|
||||
match size_option {
|
||||
None => Ok(None),
|
||||
Some(element_size) => {
|
||||
let (element_data, rest) = self.0.split_at(element_size);
|
||||
self.0 = rest;
|
||||
match T::decode_element(element_data) {
|
||||
Err(err) => Err(err),
|
||||
Ok(None) => panic!("Buffer was supposed to have enough data to parse element, somehow did not."),
|
||||
Ok(Some((element, _))) => Ok(Some(element))
|
||||
}
|
||||
}
|
||||
}
|
||||
}).map(Async::Ready)
|
||||
T::decode_element(self.0).map(|option| option.map(|(element, element_size)| {
|
||||
self.0 = &self.0[element_size..];
|
||||
element
|
||||
})).map(Async::Ready)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -134,7 +134,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn decode_webm_test1() {
|
||||
let mut iter = parse_webm(TEST_FILE).into_iter();
|
||||
let mut iter = parse_webm(TEST_FILE);
|
||||
|
||||
// test that we match the structure of the test file
|
||||
assert_eq!(iter.next(), Some(WebmElement::EbmlHead));
|
||||
|
|
Loading…
Reference in a new issue