mediasan_common/
lib.rs

1#![warn(missing_docs)]
2
3//! `mediasan-common` is a common library shared by the `mediasan` media format "sanitizers".
4
5#[macro_use]
6pub mod macros;
7
8pub mod async_skip;
9pub mod error;
10pub mod parse;
11mod skip;
12pub mod sync;
13pub mod util;
14
15use std::io;
16use std::pin::Pin;
17use std::task::{Context, Poll};
18
19use derive_more::{Deref, DerefMut};
20
21//
22// public types
23//
24
25pub use error::{Error, Report, Result, ResultExt};
26
27/// A pointer to a span in the given input.
28#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
29pub struct InputSpan {
30    /// The offset from the beginning of the input where the span begins.
31    pub offset: u64,
32
33    /// The length of the span.
34    pub len: u64,
35}
36
37/// A subset of the [`Seek`] trait, providing a cursor which can skip forward within a stream of bytes.
38pub trait Skip {
39    /// Skip an amount of bytes in a stream.
40    ///
41    /// A skip beyond the end of a stream is allowed, but behavior is defined by the implementation.
42    fn skip(&mut self, amount: u64) -> io::Result<()>;
43
44    /// Returns the current position of the cursor from the start of the stream.
45    fn stream_position(&mut self) -> io::Result<u64>;
46
47    /// Returns the length of this stream, in bytes.
48    fn stream_len(&mut self) -> io::Result<u64>;
49}
50
51/// A subset of the [`AsyncSeek`] trait, providing a cursor which can skip forward within a stream of bytes.
52pub trait AsyncSkip {
53    /// Skip an amount of bytes in a stream.
54    ///
55    /// A skip beyond the end of a stream is allowed, but behavior is defined by the implementation.
56    fn poll_skip(self: Pin<&mut Self>, cx: &mut Context<'_>, amount: u64) -> Poll<io::Result<()>>;
57
58    /// Returns the current position of the cursor from the start of the stream.
59    fn poll_stream_position(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<u64>>;
60
61    /// Returns the length of this stream, in bytes.
62    fn poll_stream_len(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<u64>>;
63}
64
65/// An adapter implementing [`Skip`]/[`AsyncSkip`] for all types implementing [`Seek`]/[`AsyncSeek`].
66#[derive(Clone, Copy, Debug, Default, Deref, DerefMut)]
67pub struct SeekSkipAdapter<T: ?Sized>(pub T);
68
69pub use async_skip::AsyncSkipExt;