Commonize some code for EbmlCursor
This commit is contained in:
parent
0e2e5ad48a
commit
90c1892668
2 changed files with 18 additions and 19 deletions
|
@ -17,32 +17,31 @@ impl<S> EbmlCursor<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> EbmlCursor<&'a [u8]> {
|
||||
fn decode_element<T: FromEbml<'a>>(&mut self) -> Result<Option<T>, 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<WebmElement<'a>> {
|
||||
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<S> {
|
||||
impl<'b> WebmEventSource for EbmlCursor<&'b [u8]> {
|
||||
type Error = EbmlError;
|
||||
|
||||
fn poll_event<'a>(&'a mut self) -> Result<Async<Option<WebmElement<'a>>>, 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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,8 +13,8 @@ const CLUSTER_ID: u64 = 0x0F43B675;
|
|||
const TIMECODE_ID: u64 = 0x67;
|
||||
const SIMPLE_BLOCK_ID: u64 = 0x23;
|
||||
|
||||
pub fn parse_webm<T: AsRef<[u8]>>(source: T) -> EbmlCursor<T> {
|
||||
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)]
|
||||
|
|
Loading…
Reference in a new issue