genericize EBML iterator

This commit is contained in:
Tangent 128 2017-01-24 19:11:59 -05:00
parent 4a42b7ae82
commit 128192c622

View file

@ -147,25 +147,27 @@ pub fn decode_element<'a, 'b: 'a, T: Schema<'a>>(bytes: &'b[u8]) -> Result<Optio
} }
} }
pub struct WebmIterator<'b> { pub struct EbmlIterator<'b, T: Schema<'b>> {
schema: std::marker::PhantomData<T>,
slice: &'b[u8], slice: &'b[u8],
position: usize, position: usize,
} }
impl<'b> WebmIterator<'b> { impl<'b, T: Schema<'b>> EbmlIterator<'b, T> {
pub fn new(bytes: &'b[u8]) -> Self { pub fn new(bytes: &'b[u8]) -> Self {
WebmIterator { EbmlIterator {
schema: std::marker::PhantomData,
slice: bytes, slice: bytes,
position: 0 position: 0
} }
} }
} }
impl<'b> Iterator for WebmIterator<'b> { impl<'b, T: Schema<'b>> Iterator for EbmlIterator<'b, T> {
type Item = WebmElement<'b>; type Item = T::Element;
fn next(&mut self) -> Option<WebmElement<'b>> { fn next(&mut self) -> Option<T::Element> {
match decode_element::<Webm>(&self.slice[self.position..]) { match decode_element::<T>(&self.slice[self.position..]) {
Err(_) => None, Err(_) => None,
Ok(None) => None, Ok(None) => None,
Ok(Some((element, element_size))) => { Ok(Some((element, element_size))) => {
@ -271,7 +273,7 @@ mod tests {
#[test] #[test]
fn decode_webm_test1() { fn decode_webm_test1() {
let mut iter = WebmIterator::new(TEST_FILE); let mut iter = EbmlIterator::<Webm>::new(TEST_FILE);
// EBML Header // EBML Header
assert_webm_blob(iter.next(), 0x0A45DFA3, 31); assert_webm_blob(iter.next(), 0x0A45DFA3, 31);