Skip to main content

RangeReader

Struct RangeReader 

Source
pub struct RangeReader<F: RangeFetcher> { /* private fields */ }
Expand description

Read + Seek over a RangeFetcher, with tail caching, read-ahead, and hard safety budgets.

See the module docs for the access-pattern and budget design.

Implementations§

Source§

impl<F: RangeFetcher> RangeReader<F>

Source

pub const fn new(fetcher: F) -> Self

Creates a reader with the default safety budgets (MAX_RANGE_REQUESTS, MAX_TRANSFER_BUDGET).

Source

pub const fn with_limits( fetcher: F, max_requests: u32, max_transfer_bytes: u64, ) -> Self

Creates a reader with explicit safety budgets.

max_requests caps the number of range fetches; max_transfer_bytes caps the total content bytes fetched. Exceeding either fails the offending read with a std::io::Error naming the cap.

Source

pub fn stats(&self) -> RangeStats

Transfer statistics so far (range fetches + transport probes).

Source

pub fn total_size(&self) -> u64

Total size of the remote file in bytes.

Source

pub const fn take_last_error(&mut self) -> Option<FetchError>

Takes the most recent typed transport error, if any.

Read flattens FetchError into std::io::Error strings; after a parse fails, callers use this to recover the typed error (e.g. to let the CLI upgrade an HTTP 403 into a gated-repo diagnosis).

Source§

impl RangeReader<HttpRangeFetcher>

Source

pub async fn open( repo_id: &str, revision: Option<&str>, filename: &str, token: Option<&str>, ) -> Result<Self, FetchError>

Opens a range reader over filename in repo_id at revision (default main).

Performs the probe eagerly, so failures (missing file, gated repo, no Range support) surface here as typed errors rather than mid-parse io::Errors.

Must be called from within a tokio runtime; the returned reader is then handed to a blocking context (tokio::task::spawn_blocking), where its Read / Seek impls drive async requests via the captured runtime handle.

§Errors

Returns FetchError::Http if the probe fails, the repository or file is inaccessible (including gated repos: returned status 401/403 errors, which the CLI upgrades into a gated-repo diagnosis), or the server does not support Range requests.

Source

pub async fn open_with_limits( repo_id: &str, revision: Option<&str>, filename: &str, token: Option<&str>, max_requests: u32, max_transfer_bytes: u64, ) -> Result<Self, FetchError>

Opens a range reader with explicit safety budgets (see RangeReader::with_limits) instead of the MAX_RANGE_REQUESTS / MAX_TRANSFER_BUDGET defaults HttpRangeReader::open uses.

The defaults are tuned for archive-header parsing (inspect’s .safetensors / .gguf / .npz / .pth paths) — a handful of requests, well under 1 MiB. A caller with its own, larger, user-facing byte budget (hf-fm peek’s --max) needs its transport budget sized to match, or safety limits tuned for header parsing cut it off first with a misleading “pathological archive layout” error.

Same preconditions as HttpRangeReader::open (must be called from within a tokio runtime; the reader is then driven from a blocking context).

§Errors

Same as HttpRangeReader::open.

Source

pub fn probe_etag(&self) -> &str

The etag observed at probe time, before any range request.

Lets a caller key a cache entry (inspect --cache-headers) on the same etag hf-hub’s own blob layout uses, without an extra round trip beyond the probe HttpRangeReader::open already made.

Trait Implementations§

Source§

impl<F: RangeFetcher> Read for RangeReader<F>

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact( &mut self, cursor: BorrowedCursor<'_, u8>, ) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>
where Self: Sized,

🔬This is a nightly-only experimental API. (read_array)
Read and return a fixed array of bytes from this source. Read more
Source§

fn read_le<T>(&mut self) -> Result<T, Error>
where T: FromEndianBytes, Self: Sized,

🔬This is a nightly-only experimental API. (read_le)
Read and return a type (e.g. an integer) in little-endian order. Read more
Source§

fn read_be<T>(&mut self) -> Result<T, Error>
where T: FromEndianBytes, Self: Sized,

🔬This is a nightly-only experimental API. (read_le)
Read and return a type (e.g. an integer) in big-endian order. Read more
Source§

impl<F: RangeFetcher> Seek for RangeReader<F>

Source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

Seek to an offset, in bytes, in a stream. Read more
1.55.0 · Source§

fn rewind(&mut self) -> Result<(), Error>

Rewind to the beginning of a stream. Read more
Source§

fn stream_len(&mut self) -> Result<u64, Error>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · Source§

fn stream_position(&mut self) -> Result<u64, Error>

Returns the current seek position from the start of the stream. Read more
1.80.0 · Source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

Seeks relative to the current position. Read more

Auto Trait Implementations§

§

impl<F> !RefUnwindSafe for RangeReader<F>

§

impl<F> !UnwindSafe for RangeReader<F>

§

impl<F> Freeze for RangeReader<F>
where F: Freeze,

§

impl<F> Send for RangeReader<F>
where F: Send,

§

impl<F> Sync for RangeReader<F>
where F: Sync,

§

impl<F> Unpin for RangeReader<F>
where F: Unpin,

§

impl<F> UnsafeUnpin for RangeReader<F>
where F: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> DropFlavorWrapper<T> for T

Source§

type Flavor = MayDrop

The DropFlavor that wraps T into Self
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

impl<T> Identity for T
where T: ?Sized,

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more