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>
impl<F: RangeFetcher> RangeReader<F>
Sourcepub const fn new(fetcher: F) -> Self
pub const fn new(fetcher: F) -> Self
Creates a reader with the default safety budgets
(MAX_RANGE_REQUESTS, MAX_TRANSFER_BUDGET).
Sourcepub const fn with_limits(
fetcher: F,
max_requests: u32,
max_transfer_bytes: u64,
) -> Self
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.
Sourcepub fn stats(&self) -> RangeStats
pub fn stats(&self) -> RangeStats
Transfer statistics so far (range fetches + transport probes).
Sourcepub fn total_size(&self) -> u64
pub fn total_size(&self) -> u64
Total size of the remote file in bytes.
Sourcepub const fn take_last_error(&mut self) -> Option<FetchError>
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>
impl RangeReader<HttpRangeFetcher>
Sourcepub async fn open(
repo_id: &str,
revision: Option<&str>,
filename: &str,
token: Option<&str>,
) -> Result<Self, FetchError>
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.
Sourcepub 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>
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.
Sourcepub fn probe_etag(&self) -> &str
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>
impl<F: RangeFetcher> Read for RangeReader<F>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(
&mut self,
cursor: BorrowedCursor<'_, u8>,
) -> Result<(), Error>
fn read_buf_exact( &mut self, cursor: BorrowedCursor<'_, u8>, ) -> Result<(), Error>
read_buf)cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read more1.0.0 · Source§fn chain<R>(self, next: R) -> Chain<Self, R> ⓘ
fn chain<R>(self, next: R) -> Chain<Self, R> ⓘ
1.0.0 · Source§fn take(self, limit: u64) -> Take<Self> ⓘwhere
Self: Sized,
fn take(self, limit: u64) -> Take<Self> ⓘwhere
Self: Sized,
limit bytes from it. Read moreSource§fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
read_array)Source§impl<F: RangeFetcher> Seek for RangeReader<F>
impl<F: RangeFetcher> Seek for RangeReader<F>
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len)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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> DropFlavorWrapper<T> for T
impl<T> DropFlavorWrapper<T> for T
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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