Skip to main content

AssetSource

Trait AssetSource 

Source
pub trait AssetSource:
    'static
    + Send
    + Sync {
    // Required methods
    fn load(&self, path: &str) -> Result<Option<Cow<'static, [u8]>>>;
    fn list(&self, path: &str) -> Result<Vec<SharedString>>;

    // Provided methods
    fn iter_preloaded(
        &self,
    ) -> Box<dyn Iterator<Item = (SharedString, AssetEntry)> + '_> { ... }
    fn iter_ondemand(
        self: Arc<Self>,
    ) -> Box<dyn Iterator<Item = (SharedString, AssetEntry)>> { ... }
}
Expand description

A read-only source of files that the app can load by path, such as a directory or a bundle.

Prefer AssetRegistry when you control the entries: it can hold preloaded bytes as well as on-demand loaders. An AssetSource is useful when the source already owns the path lookup. It converts into an AssetRegistry with From, and crate::Application::with_assets accepts that conversion. The converted registry loads each file on demand.

Required Methods§

Source

fn load(&self, path: &str) -> Result<Option<Cow<'static, [u8]>>>

Returns the bytes of the file at path, or None if there is no such file.

The paths are the ones that AssetSource::list returns.

Source

fn list(&self, path: &str) -> Result<Vec<SharedString>>

Returns the paths of the assets under path. Pass "" to list all of them.

Each returned path is one that AssetSource::load accepts.

Provided Methods§

Source

fn iter_preloaded( &self, ) -> Box<dyn Iterator<Item = (SharedString, AssetEntry)> + '_>

Loads every asset now and yields each one as an AssetEntry::PreLoaded.

It lists everything with list(""), loads each path, and skips the paths that fail or are missing. Pass the result to AssetRegistry::extend.

Source

fn iter_ondemand( self: Arc<Self>, ) -> Box<dyn Iterator<Item = (SharedString, AssetEntry)>>

Yields one AssetEntry::OnDemand per path. Each entry calls AssetSource::load on this source when the asset is requested.

Nothing is read until an asset is requested, and each request reads the file again. A failed load is logged and reported as None. Pass the result to AssetRegistry::extend.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementations on Foreign Types§

Source§

impl AssetSource for ()

Source§

fn load(&self, _path: &str) -> Result<Option<Cow<'static, [u8]>>>

Source§

fn list(&self, _path: &str) -> Result<Vec<SharedString>>

Implementors§