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 std::marker::PhantomData;
|
||||||
|
|
||||||
use ebml::FromEbml;
|
use ebml::FromEbml;
|
||||||
use webm::WebmElement;
|
|
||||||
|
|
||||||
pub struct EbmlIterator<'a, T: FromEbml<'a>>(&'a [u8], PhantomData<fn() -> T>);
|
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> {
|
impl<'a, T: FromEbml<'a>> Iterator for EbmlIterator<'a, T> {
|
||||||
type Item = WebmElement<'a>;
|
type Item = T;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<WebmElement<'a>> {
|
fn next(&mut self) -> Option<T> {
|
||||||
WebmElement::check_space(self.0).unwrap_or(None).and_then(|element_size| {
|
T::decode_element(self.0).unwrap_or(None).and_then(|(element, element_size)| {
|
||||||
let (element_data, rest) = self.0.split_at(element_size);
|
self.0 = &self.0[element_size..];
|
||||||
self.0 = rest;
|
Some(element)
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
18
src/slice.rs
18
src/slice.rs
|
@ -9,19 +9,9 @@ impl<'b> EbmlEventSource for EbmlSlice<'b> {
|
||||||
type Error = EbmlError;
|
type Error = EbmlError;
|
||||||
|
|
||||||
fn poll_event<'a, T: FromEbml<'a>>(&'a mut self) -> Result<Async<Option<T>>, 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| {
|
T::decode_element(self.0).map(|option| option.map(|(element, element_size)| {
|
||||||
match size_option {
|
self.0 = &self.0[element_size..];
|
||||||
None => Ok(None),
|
element
|
||||||
Some(element_size) => {
|
})).map(Async::Ready)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,7 +134,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn decode_webm_test1() {
|
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
|
// test that we match the structure of the test file
|
||||||
assert_eq!(iter.next(), Some(WebmElement::EbmlHead));
|
assert_eq!(iter.next(), Some(WebmElement::EbmlHead));
|
||||||
|
|
Loading…
Reference in a new issue