Skip to main content

CachingByteSource

Struct CachingByteSource 

Source
pub struct CachingByteSource<S> { /* private fields */ }
Expand description

A ByteSource wrapper that caches fixed-size pages of the bytes it reads, evicting the least-recently-used page once a configured byte budget is exceeded.

This is how a crate::codec::SampleProvider backed by a container track and a plain ByteSource (such as a file) keeps the memory for compressed sample bytes bounded: samples near the playhead are read through overlapping pages that stay cached, and pages behind the budget are dropped rather than held for the life of the source.

Reads are not required to be page-aligned or page-sized; a read spanning several pages fetches and caches each of them. Internal caching uses a RefCell, so a CachingByteSource is usable from one thread at a time, matching how a reader built over it is used.

Implementations§

Source§

impl<S: ByteSource> CachingByteSource<S>

Source

pub fn new(inner: S, page_size: u64, budget_bytes: u64) -> Result<Self>

page_size and budget_bytes must both be nonzero; a page larger than the budget is still cached (so a single read always succeeds) but is evicted again on the very next page fetch.

Source

pub fn into_inner(self) -> S

Source

pub fn resident_bytes(&self) -> u64

Total bytes currently cached, for tests and diagnostics.

Trait Implementations§

Source§

impl<S: ByteSource> ByteSource for CachingByteSource<S>

Source§

fn read_at<'a>( &'a self, offset: u64, destination: &'a mut [u8], ) -> IoFuture<'a, usize>

Answers each cached page of the read from the cache, and fetches each run of neighboring pages that is not with one read of the inner source, so a read spanning many pages, such as a large index element, costs one request however large it is (issue #692).

Source§

fn len(&self) -> Option<u64>

Source§

fn is_empty(&self) -> Option<bool>

Auto Trait Implementations§

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.