From 128192c622ad224d683d0a99a4244681d0c8b417 Mon Sep 17 00:00:00 2001 From: Tangent 128 Date: Tue, 24 Jan 2017 19:11:59 -0500 Subject: [PATCH] genericize EBML iterator --- src/lib.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f3a2652..1908218 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,25 +147,27 @@ pub fn decode_element<'a, 'b: 'a, T: Schema<'a>>(bytes: &'b[u8]) -> Result { +pub struct EbmlIterator<'b, T: Schema<'b>> { + schema: std::marker::PhantomData, slice: &'b[u8], position: usize, } -impl<'b> WebmIterator<'b> { +impl<'b, T: Schema<'b>> EbmlIterator<'b, T> { pub fn new(bytes: &'b[u8]) -> Self { - WebmIterator { + EbmlIterator { + schema: std::marker::PhantomData, slice: bytes, position: 0 } } } -impl<'b> Iterator for WebmIterator<'b> { - type Item = WebmElement<'b>; +impl<'b, T: Schema<'b>> Iterator for EbmlIterator<'b, T> { + type Item = T::Element; - fn next(&mut self) -> Option> { - match decode_element::(&self.slice[self.position..]) { + fn next(&mut self) -> Option { + match decode_element::(&self.slice[self.position..]) { Err(_) => None, Ok(None) => None, Ok(Some((element, element_size))) => { @@ -271,7 +273,7 @@ mod tests { #[test] fn decode_webm_test1() { - let mut iter = WebmIterator::new(TEST_FILE); + let mut iter = EbmlIterator::::new(TEST_FILE); // EBML Header assert_webm_blob(iter.next(), 0x0A45DFA3, 31);