Skip to main content

ByteSource

Trait ByteSource 

Source
pub trait ByteSource: Send + Sync {
    // Required methods
    fn len(&self) -> u64;
    fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize, SourceError>;

    // Provided methods
    fn is_empty(&self) -> bool { ... }
    fn as_slice(&self) -> Option<&[u8]> { ... }
}
Expand description

Random-access input bytes: in memory, memory-mapped, a browser Blob, an HTTP range cache.

Synchronous by design (the core has no async runtime): a source whose bytes have not arrived yet answers SourceError::Pending, and the shell fetches them and retries. In-memory sources return their bytes from ByteSource::as_slice, which lets the parser work on them in place.

A source without as_slice is read in aligned 64 KiB windows (and small probes for xref entry checks); read_at may return fewer bytes than asked for and is called again for the rest. A SourceError::Pending is reported to the caller as Error::DataPending with the range the source gave.

Core implementations: Vec<u8> (moved in), Arc<[u8]> (shared), &'static [u8]. Platform sources (mmap, Blob, OPFS) live in edge crates.

Added in 0.2.0.

Required Methods§

Source

fn len(&self) -> u64

Total length in bytes.

Added in 0.2.0.

Source

fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize, SourceError>

Fills buf from offset; returns the bytes read (short only at the end of input).

§Errors

SourceError::Pending when the range is not available yet, SourceError::Failed when it cannot be read.

Added in 0.2.0.

Provided Methods§

Source

fn is_empty(&self) -> bool

Whether the source holds no bytes.

Added in 0.2.0.

Source

fn as_slice(&self) -> Option<&[u8]>

The whole input, when it is already in memory (lets the parser skip copies).

Added in 0.2.0.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl ByteSource for &'static [u8]

Source§

fn len(&self) -> u64

Source§

fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize, SourceError>

Source§

fn as_slice(&self) -> Option<&[u8]>

Source§

impl ByteSource for Arc<[u8]>

Source§

fn len(&self) -> u64

Source§

fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize, SourceError>

Source§

fn as_slice(&self) -> Option<&[u8]>

Source§

impl ByteSource for Vec<u8>

Source§

fn len(&self) -> u64

Source§

fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize, SourceError>

Source§

fn as_slice(&self) -> Option<&[u8]>

Implementors§