Struct ebml_iterable::TagIterator
source · pub struct TagIterator<R: Read, TSpec>{ /* private fields */ }
Expand description
Provides an iterator over EBML files (read from a source implementing the std::io::Read
trait). Can be configured to read specific “Master” tags as complete objects rather than just emitting when they start and end.
This is a generic struct that requires a specification implementing EbmlSpecification
and EbmlTag
. No specifications are included in this crate - you will need to either use another crate providing a spec (such as the Matroska spec implemented in the webm-iterable or write your own spec if you want to iterate over a custom EBML file. The iterator outputs TSpec
variants representing the type of tag (defined by the specification) and the accompanying tag data. “Master” tags (defined by the specification) usually will be read as Start
and End
variants, but the iterator can be configured to buffer Master tags into a Full
variant using the tags_to_buffer
parameter.
Note: The Self::with_capacity()
method can be used to construct a TagIterator
with a specified default buffer size. This is only useful as a microoptimization to memory management if you know the maximum tag size of the file you’re reading.
§Example
use std::fs::File;
use ebml_iterable::TagIterator;
let file = File::open("my_ebml_file.ebml")?;
let mut my_iterator: TagIterator<_, EmptySpec> = TagIterator::new(file, &[]);
for tag in my_iterator {
println!("{:?}", tag?);
}
§Errors
The Item
type for the associated Iterator
implementation is a Result<TSpec, TagIteratorError>
, meaning each next()
call has the potential to fail. This is because the source data is not parsed all at once - it is incrementally parsed as the iterator progresses. If the iterator runs into an error (such as corrupted data or an unexpected end-of-file), it needs to be propagated to the logic trying to read the tags. The different possible error states are enumerated in TagIteratorError
.
§Panics
The iterator can panic if <TSpec>
is an internally inconsistent specification (i.e. it claims that a specific tag id has a specific data type but fails to produce a tag variant using data of that type). This won’t happen if the specification being used was created using the #[ebml_specification]
attribute macro.
Implementations§
source§impl<R: Read, TSpec> TagIterator<R, TSpec>
impl<R: Read, TSpec> TagIterator<R, TSpec>
sourcepub fn new(source: R, tags_to_buffer: &[TSpec]) -> Self
pub fn new(source: R, tags_to_buffer: &[TSpec]) -> Self
Returns a new TagIterator<TSpec>
instance.
The source
parameter must implement std::io::Read
. The second argument, tags_to_buffer
, specifies which “Master” tags should be read as Master::Full
s rather than as Master::Start
and Master::End
s. Refer to the documentation on TagIterator
for more explanation of how to use the returned instance.
sourcepub fn with_capacity(
source: R,
tags_to_buffer: &[TSpec],
capacity: usize
) -> Self
pub fn with_capacity( source: R, tags_to_buffer: &[TSpec], capacity: usize ) -> Self
Returns a new TagIterator<TSpec>
instance with the specified internal buffer capacity.
This initializes the TagIterator
with a specific byte capacity. The iterator will still reallocate if necessary. (Reallocation occurs if the iterator comes across a tag that should be output as a Master::Full
and its size in bytes is greater than the iterator’s current buffer capacity.)
sourcepub fn allow_errors(&mut self, errors: &[AllowableErrors])
pub fn allow_errors(&mut self, errors: &[AllowableErrors])
Configures how strictly the iterator abides <TSpec>
.
By default (as of v0.5.0), the iterator assumes <TSpec>
is complete and that any tags that do not conform to <TSpec>
are due to corrupted file data. This method can be used to relax some of these checks so that fewer TagIteratorError::CorruptedFileData
errors occur.
§Important
Relaxing these checks do not necessarily make the iterator more robust. If all errors are allowed, the iterator will assume any incoming tag id and tag data size are valid, and it will produce “RawTag“s containing binary contents for any tag ids not in <TSpec>
. However, if the file truly has corrupted data, the “size” of these elements will likely be corrupt as well. This can typically result in requests for massive allocations, causing delays and eventual crashing. By eagerly returning errors (the default), applications can decide how to handle corrupted elements more quickly and efficiently.
tldr; allow errors at your own risk
Note: TagIterators returned by
Self::new()
andSelf::with_capacity()
allow no errors by default.
sourcepub fn set_max_allowable_tag_size(&mut self, size: Option<usize>)
pub fn set_max_allowable_tag_size(&mut self, size: Option<usize>)
Configures the maximum size a tag is allowed to be before the iterator considers it invalid.
By default (as of v0.6.1), the iterator will throw an CorruptedFileError::InvalidTagSize
error if it comes across any tags that declare their data to be more than 4GB. This method can be used to change (and optionally remove) this behavior. Note that increasing this size can potentially result in massive allocations, causing delays and panics.
sourcepub fn try_recover(&mut self) -> Result<(), TagIteratorError>
pub fn try_recover(&mut self) -> Result<(), TagIteratorError>
Instructs the iterator to attempt to recover after reaching corrupted file data.
This method can be used to skip over corrupted sections of a read stream without recreating a new iterator. The iterator will seek forward from its current internal position until it reaches either a valid EBML tag id or EOF. After recovery, Iterator::next()
should return an Ok
result.
sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Consumes self and returns the underlying read stream.
Note that any leftover tags in the internal emission queue are lost, and any data read into TagIterator
’s internal buffer is dropped. Therefore, constructing a new TagIterator
using the returned stream may lead to data loss unless it is rewound.
sourcepub fn get_mut(&mut self) -> &mut R
pub fn get_mut(&mut self) -> &mut R
Gets a mutable reference to the underlying read stream.
It is inadvisable to directly read from the underlying stream.
sourcepub fn get_ref(&self) -> &R
pub fn get_ref(&self) -> &R
Gets a reference to the underlying read stream.
It is inadvisable to directly read from the underlying stream.
sourcepub fn last_emitted_tag_offset(&self) -> usize
pub fn last_emitted_tag_offset(&self) -> usize
Returns the byte offset of the last emitted tag.
This function returns a byte index specifying the start of the last emitted tag in the context of the TagIterator
’s source read stream. This value is not guaranteed to always increase as the file is read. Whenever the iterator emits a Master::End
variant, Self::last_emitted_tag_offset()
will reflect the start index of the “Master” tag, which will be before previous values that were obtainable when any children of the master were emitted.
Trait Implementations§
source§impl<R: Read, TSpec> Iterator for TagIterator<R, TSpec>
impl<R: Read, TSpec> Iterator for TagIterator<R, TSpec>
§type Item = Result<TSpec, TagIteratorError>
type Item = Result<TSpec, TagIteratorError>
source§fn next(&mut self) -> Option<Self::Item>
fn next(&mut self) -> Option<Self::Item>
source§fn next_chunk<const N: usize>(
&mut self
) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>where
Self: Sized,
fn next_chunk<const N: usize>(
&mut self
) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>where
Self: Sized,
iter_next_chunk
)N
values. Read more1.0.0 · source§fn size_hint(&self) -> (usize, Option<usize>)
fn size_hint(&self) -> (usize, Option<usize>)
1.0.0 · source§fn count(self) -> usizewhere
Self: Sized,
fn count(self) -> usizewhere
Self: Sized,
1.0.0 · source§fn last(self) -> Option<Self::Item>where
Self: Sized,
fn last(self) -> Option<Self::Item>where
Self: Sized,
source§fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
iter_advance_by
)n
elements. Read more1.0.0 · source§fn nth(&mut self, n: usize) -> Option<Self::Item>
fn nth(&mut self, n: usize) -> Option<Self::Item>
n
th element of the iterator. Read more1.28.0 · source§fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
1.0.0 · source§fn chain<U>(self, other: U) -> Chain<Self, <U as IntoIterator>::IntoIter>
fn chain<U>(self, other: U) -> Chain<Self, <U as IntoIterator>::IntoIter>
1.0.0 · source§fn zip<U>(self, other: U) -> Zip<Self, <U as IntoIterator>::IntoIter>where
Self: Sized,
U: IntoIterator,
fn zip<U>(self, other: U) -> Zip<Self, <U as IntoIterator>::IntoIter>where
Self: Sized,
U: IntoIterator,
source§fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G>
fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G>
iter_intersperse
)separator
between adjacent items of the original iterator. Read more1.0.0 · source§fn map<B, F>(self, f: F) -> Map<Self, F>
fn map<B, F>(self, f: F) -> Map<Self, F>
1.0.0 · source§fn filter<P>(self, predicate: P) -> Filter<Self, P>
fn filter<P>(self, predicate: P) -> Filter<Self, P>
1.0.0 · source§fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F>
fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F>
1.0.0 · source§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
1.0.0 · source§fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
1.0.0 · source§fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
1.57.0 · source§fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P>
fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P>
1.0.0 · source§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n
elements. Read more1.0.0 · source§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n
elements, or fewer
if the underlying iterator ends sooner. Read more1.0.0 · source§fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
source§fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N>
fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N>
iter_map_windows
)f
for each contiguous window of size N
over
self
and returns an iterator over the outputs of f
. Like slice::windows()
,
the windows during mapping overlap as well. Read more1.0.0 · source§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
source§fn collect_into<E>(self, collection: &mut E) -> &mut E
fn collect_into<E>(self, collection: &mut E) -> &mut E
iter_collect_into
)1.0.0 · source§fn partition<B, F>(self, f: F) -> (B, B)
fn partition<B, F>(self, f: F) -> (B, B)
source§fn is_partitioned<P>(self, predicate: P) -> bool
fn is_partitioned<P>(self, predicate: P) -> bool
iter_is_partitioned
)true
precede all those that return false
. Read more1.27.0 · source§fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
1.27.0 · source§fn try_for_each<F, R>(&mut self, f: F) -> R
fn try_for_each<F, R>(&mut self, f: F) -> R
1.0.0 · source§fn fold<B, F>(self, init: B, f: F) -> B
fn fold<B, F>(self, init: B, f: F) -> B
1.51.0 · source§fn reduce<F>(self, f: F) -> Option<Self::Item>
fn reduce<F>(self, f: F) -> Option<Self::Item>
source§fn try_reduce<F, R>(
&mut self,
f: F
) -> <<R as Try>::Residual as Residual<Option<<R as Try>::Output>>>::TryType
fn try_reduce<F, R>( &mut self, f: F ) -> <<R as Try>::Residual as Residual<Option<<R as Try>::Output>>>::TryType
iterator_try_reduce
)1.0.0 · source§fn all<F>(&mut self, f: F) -> bool
fn all<F>(&mut self, f: F) -> bool
1.0.0 · source§fn any<F>(&mut self, f: F) -> bool
fn any<F>(&mut self, f: F) -> bool
1.0.0 · source§fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
1.30.0 · source§fn find_map<B, F>(&mut self, f: F) -> Option<B>
fn find_map<B, F>(&mut self, f: F) -> Option<B>
source§fn try_find<F, R>(
&mut self,
f: F
) -> <<R as Try>::Residual as Residual<Option<Self::Item>>>::TryType
fn try_find<F, R>( &mut self, f: F ) -> <<R as Try>::Residual as Residual<Option<Self::Item>>>::TryType
try_find
)1.0.0 · source§fn position<P>(&mut self, predicate: P) -> Option<usize>
fn position<P>(&mut self, predicate: P) -> Option<usize>
1.6.0 · source§fn max_by_key<B, F>(self, f: F) -> Option<Self::Item>
fn max_by_key<B, F>(self, f: F) -> Option<Self::Item>
1.15.0 · source§fn max_by<F>(self, compare: F) -> Option<Self::Item>
fn max_by<F>(self, compare: F) -> Option<Self::Item>
1.6.0 · source§fn min_by_key<B, F>(self, f: F) -> Option<Self::Item>
fn min_by_key<B, F>(self, f: F) -> Option<Self::Item>
1.15.0 · source§fn min_by<F>(self, compare: F) -> Option<Self::Item>
fn min_by<F>(self, compare: F) -> Option<Self::Item>
1.0.0 · source§fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB)
fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB)
1.36.0 · source§fn copied<'a, T>(self) -> Copied<Self>
fn copied<'a, T>(self) -> Copied<Self>
source§fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N>where
Self: Sized,
fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N>where
Self: Sized,
iter_array_chunks
)N
elements of the iterator at a time. Read more1.11.0 · source§fn product<P>(self) -> P
fn product<P>(self) -> P
source§fn cmp_by<I, F>(self, other: I, cmp: F) -> Ordering
fn cmp_by<I, F>(self, other: I, cmp: F) -> Ordering
iter_order_by
)Iterator
with those
of another with respect to the specified comparison function. Read more1.5.0 · source§fn partial_cmp<I>(self, other: I) -> Option<Ordering>
fn partial_cmp<I>(self, other: I) -> Option<Ordering>
PartialOrd
elements of
this Iterator
with those of another. The comparison works like short-circuit
evaluation, returning a result without comparing the remaining elements.
As soon as an order can be determined, the evaluation stops and a result is returned. Read moresource§fn partial_cmp_by<I, F>(self, other: I, partial_cmp: F) -> Option<Ordering>where
Self: Sized,
I: IntoIterator,
F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Option<Ordering>,
fn partial_cmp_by<I, F>(self, other: I, partial_cmp: F) -> Option<Ordering>where
Self: Sized,
I: IntoIterator,
F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Option<Ordering>,
iter_order_by
)Iterator
with those
of another with respect to the specified comparison function. Read moresource§fn eq_by<I, F>(self, other: I, eq: F) -> bool
fn eq_by<I, F>(self, other: I, eq: F) -> bool
iter_order_by
)1.5.0 · source§fn lt<I>(self, other: I) -> bool
fn lt<I>(self, other: I) -> bool
Iterator
are lexicographically
less than those of another. Read more1.5.0 · source§fn le<I>(self, other: I) -> bool
fn le<I>(self, other: I) -> bool
Iterator
are lexicographically
less or equal to those of another. Read more1.5.0 · source§fn gt<I>(self, other: I) -> bool
fn gt<I>(self, other: I) -> bool
Iterator
are lexicographically
greater than those of another. Read more1.5.0 · source§fn ge<I>(self, other: I) -> bool
fn ge<I>(self, other: I) -> bool
Iterator
are lexicographically
greater than or equal to those of another. Read moresource§fn is_sorted_by<F>(self, compare: F) -> bool
fn is_sorted_by<F>(self, compare: F) -> bool
is_sorted
)source§fn is_sorted_by_key<F, K>(self, f: F) -> bool
fn is_sorted_by_key<F, K>(self, f: F) -> bool
is_sorted
)