Split apart iterator and slice modules
This commit is contained in:
parent
9686eb2713
commit
3a92da1883
4 changed files with 41 additions and 29 deletions
|
@ -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<fn() -> 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<WebmElement<'a>> {
|
||||
|
@ -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<Async<Option<T>>, 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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
27
src/slice.rs
Normal file
27
src/slice.rs
Normal file
|
@ -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<Async<Option<T>>, 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)
|
||||
}
|
||||
}
|
|
@ -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)]
|
||||
|
|
Loading…
Reference in a new issue