diff --git a/src/iterator.rs b/src/iterator.rs index 29e19a8..a668835 100644 --- a/src/iterator.rs +++ b/src/iterator.rs @@ -17,32 +17,31 @@ impl EbmlCursor { } } +impl<'a> EbmlCursor<&'a [u8]> { + fn decode_element>(&mut self) -> Result, EbmlError> { + match T::decode_element(&self.source.as_ref()[self.position..]) { + Err(err) => Err(err), + Ok(None) => Ok(None), + Ok(Some((element, element_size))) => { + self.position += element_size; + Ok(Some(element)) + } + } + } +} + impl<'a> Iterator for EbmlCursor<&'a [u8]> { type Item = WebmElement<'a>; fn next(&mut self) -> Option> { - match Self::Item::decode_element(&self.source.as_ref()[self.position..]) { - Err(_) => None, - Ok(None) => None, - Ok(Some((element, element_size))) => { - self.position += element_size; - Some(element) - } - } + self.decode_element().unwrap_or(None) } } -impl<'b, S: AsRef<[u8]>> WebmEventSource for EbmlCursor { +impl<'b> WebmEventSource for EbmlCursor<&'b [u8]> { type Error = EbmlError; fn poll_event<'a>(&'a mut self) -> Result>>, EbmlError> { - match WebmElement::decode_element(&self.source.as_ref()[self.position..]) { - Err(err) => Err(err), - Ok(None) => Ok(Async::Ready(None)), - Ok(Some((element, element_size))) => { - self.position += element_size; - Ok(Async::Ready(Some(element))) - } - } + self.decode_element().map(Async::Ready) } } diff --git a/src/webm.rs b/src/webm.rs index bce0a05..338723f 100644 --- a/src/webm.rs +++ b/src/webm.rs @@ -13,8 +13,8 @@ const CLUSTER_ID: u64 = 0x0F43B675; const TIMECODE_ID: u64 = 0x67; const SIMPLE_BLOCK_ID: u64 = 0x23; -pub fn parse_webm>(source: T) -> EbmlCursor { - EbmlCursor::new(source) +pub fn parse_webm<'a, T: AsRef<[u8]> + ?Sized>(source: &'a T) -> EbmlCursor<&'a [u8]> { + EbmlCursor::new(source.as_ref()) } #[derive(Debug, PartialEq, Copy, Clone)]