diff --git a/src/iterator.rs b/src/iterator.rs index bcfcd5e..d746b3c 100644 --- a/src/iterator.rs +++ b/src/iterator.rs @@ -1,12 +1,15 @@ -use futures::Async; -use ebml::EbmlError; -use ebml::EbmlEventSource; +use std::marker::PhantomData; + use ebml::FromEbml; use webm::WebmElement; -pub struct EbmlSlice<'a>(pub &'a [u8]); +pub struct EbmlIterator<'a, T: FromEbml<'a>>(&'a [u8], PhantomData T>); -impl<'a> Iterator for EbmlSlice<'a> { +pub fn ebml_iter<'a, T: FromEbml<'a>>(source: &'a [u8])-> EbmlIterator<'a, T> { + EbmlIterator(source, PhantomData) +} + +impl<'a, T: FromEbml<'a>> Iterator for EbmlIterator<'a, T> { type Item = WebmElement<'a>; fn next(&mut self) -> Option> { @@ -21,24 +24,3 @@ impl<'a> Iterator for EbmlSlice<'a> { }) } } - -impl<'b> EbmlEventSource for EbmlSlice<'b> { - type Error = EbmlError; - - fn poll_event<'a, T: FromEbml<'a>>(&'a mut self) -> Result>, 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) - } -} diff --git a/src/lib.rs b/src/lib.rs index a5ca2bd..89a1de1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,8 @@ extern crate futures; pub mod chunk; pub mod ebml; mod iterator; +pub mod slice; + pub mod webm_stream; pub mod timecode_fixer; pub mod webm; diff --git a/src/slice.rs b/src/slice.rs new file mode 100644 index 0000000..8e126b7 --- /dev/null +++ b/src/slice.rs @@ -0,0 +1,27 @@ +use futures::Async; +use ebml::EbmlError; +use ebml::EbmlEventSource; +use ebml::FromEbml; + +pub struct EbmlSlice<'a>(pub &'a [u8]); + +impl<'b> EbmlEventSource for EbmlSlice<'b> { + type Error = EbmlError; + + fn poll_event<'a, T: FromEbml<'a>>(&'a mut self) -> Result>, 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) + } +} diff --git a/src/webm.rs b/src/webm.rs index 6a57add..c0f1d71 100644 --- a/src/webm.rs +++ b/src/webm.rs @@ -1,7 +1,8 @@ use std::io::{Cursor, Error as IoError, ErrorKind, Result as IoResult, Write, Seek}; use bytes::{BigEndian, BufMut, ByteOrder}; use ebml::*; -use iterator::EbmlSlice; +use iterator::ebml_iter; +use iterator::EbmlIterator; const SEGMENT_ID: u64 = 0x08538067; const SEEK_HEAD_ID: u64 = 0x014D9B74; @@ -12,8 +13,8 @@ const CLUSTER_ID: u64 = 0x0F43B675; const TIMECODE_ID: u64 = 0x67; const SIMPLE_BLOCK_ID: u64 = 0x23; -pub fn parse_webm<'a, T: AsRef<[u8]> + ?Sized>(source: &'a T) -> EbmlSlice<'a> { - EbmlSlice(source.as_ref()) +pub fn parse_webm<'a, T: AsRef<[u8]> + ?Sized>(source: &'a T) -> EbmlIterator<'a, WebmElement> { + ebml_iter(source.as_ref()) } #[derive(Debug, PartialEq, Copy, Clone)]