pub struct MediaSource<R> { /* private fields */ }Expand description
MediaSource represents a media data source that can be parsed by
MediaParser.
-
Use
MediaSource::opento create a MediaSource from a file path. -
Use
MediaSource::from_bytesfor zero-copy in-memory input (Vec<u8>,&'static [u8],bytes::Bytes, …). Pair withMediaParser::parse_exif_from_bytes/MediaParser::parse_track_from_bytes. -
In other cases:
-
Use
MediaSource::seekableto create a MediaSource from aRead + Seek(an already-openFilegoes here). -
Use
MediaSource::unseekableto create a MediaSource from a reader that only implRead
-
Note: Please use MediaSource::seekable in preference to MediaSource::unseekable,
since the former is more efficient when the parser needs to skip a large number of bytes.
Passing in a BufRead should be avoided because MediaParser comes with
its own buffer management and the buffers can be shared between multiple
parsing tasks, thus avoiding frequent memory allocations.
Implementations§
Source§impl<R> MediaSource<R>
impl<R> MediaSource<R>
Source§impl<R: Read> MediaSource<R>
impl<R: Read> MediaSource<R>
Sourcepub fn unseekable(reader: R) -> Result<Self>
pub fn unseekable(reader: R) -> Result<Self>
Use MediaSource::unseekable to create a MediaSource from a
reader that only impl Read
Note: Please use MediaSource::seekable in preference to MediaSource::unseekable,
since the former is more efficient when the parser needs to skip a large number of bytes.
Source§impl<R: Read + Seek> MediaSource<R>
impl<R: Read + Seek> MediaSource<R>
Sourcepub fn seekable(reader: R) -> Result<Self>
pub fn seekable(reader: R) -> Result<Self>
Use MediaSource::seekable to create a MediaSource from a Read + Seek
Note: Please use MediaSource::seekable in preference to MediaSource::unseekable,
since the former is more efficient when the parser needs to skip a large number of bytes.
Source§impl MediaSource<File>
impl MediaSource<File>
Source§impl MediaSource<()>
impl MediaSource<()>
Sourcepub fn from_bytes(bytes: impl Into<Bytes>) -> Result<Self>
pub fn from_bytes(bytes: impl Into<Bytes>) -> Result<Self>
Build a MediaSource from an in-memory byte payload.
Accepts any type convertible into bytes::Bytes — Bytes,
Vec<u8>, &'static [u8], bytes::Bytes::from_owner outputs, and
HTTP-stack body types that implement Into<Bytes> directly.
The header (first up to 128 bytes) is sniffed for media kind, the
same way MediaSource::open does it for files. The full payload is
stored zero-copy: subsequent parsing through
MediaParser::parse_exif_from_bytes / MediaParser::parse_track_from_bytes
shares this Bytes directly with the returned ExifIter / sub-IFDs
via reference counting.
The returned source is parsed by the dedicated
MediaParser::parse_exif_from_bytes / MediaParser::parse_track_from_bytes
methods. The streaming parse_exif / parse_track methods do not
accept MediaSource<()> (their R: Read bound is unsatisfiable).
§Example
use nom_exif::{MediaSource, MediaParser, MediaKind};
let bytes = std::fs::read("./testdata/exif.jpg")?;
let ms = MediaSource::from_bytes(bytes)?;
assert_eq!(ms.kind(), MediaKind::Image);
let mut parser = MediaParser::new();
let _iter = parser.parse_exif_from_bytes(ms)?;