image4 0.8.2

A no_std-friendly library for parsing and generation of Image4 images written in pure Rust.
Documentation
//! Provides iterator types that can be used to lazily parse Image properties without allocating
//! memory.

use super::common::{self, DecodeProperty};
use crate::Tag;
use core::{iter::FusedIterator, marker::PhantomData};
use der::{Reader, SliceReader};

/// An iterator over an encoded dictionary of Image4 properties.
#[derive(Clone, Debug)]
pub struct DictIter<'a, V> {
    reader: SliceReader<'a>,
    _marker: PhantomData<V>,
}

impl<'a, V> DictIter<'a, V> {
    pub(crate) fn new(reader: SliceReader<'a>) -> Self {
        Self {
            reader,
            _marker: Default::default(),
        }
    }
}

impl<'a, V: DecodeProperty<'a> + 'a> Iterator for DictIter<'a, V> {
    type Item = der::Result<(Tag, V)>;

    fn next(&mut self) -> Option<Self::Item> {
        if !self.reader.is_finished() {
            Some(common::decode_property(&mut self.reader))
        } else {
            None
        }
    }
}

impl<'a, V: DecodeProperty<'a> + 'a> FusedIterator for DictIter<'a, V> {}

/// An iterator over an encoded array of Image4 property values.
#[derive(Clone, Debug)]
pub struct ArrayIter<'a, V> {
    reader: SliceReader<'a>,
    _marker: PhantomData<V>,
}

impl<'a, V> ArrayIter<'a, V> {
    pub(crate) fn new(reader: SliceReader<'a>) -> Self {
        Self {
            reader,
            _marker: Default::default(),
        }
    }
}

impl<'a, V: DecodeProperty<'a> + 'a> Iterator for ArrayIter<'a, V> {
    type Item = der::Result<V>;

    fn next(&mut self) -> Option<Self::Item> {
        if !self.reader.is_finished() {
            Some(V::decode(&mut self.reader))
        } else {
            None
        }
    }
}

impl<'a, V: DecodeProperty<'a> + 'a> FusedIterator for ArrayIter<'a, V> {}