Trait veloren_common_assets::source::Source

pub trait Source {
    // Required methods
    fn read(&self, id: &str, ext: &str) -> Result<FileContent<'_>, Error>;
    fn read_dir(
        &self,
        id: &str,
        f: &mut dyn FnMut(DirEntry<'_>),
    ) -> Result<(), Error>;
    fn exists(&self, entry: DirEntry<'_>) -> bool;

    // Provided methods
    fn make_source(&self) -> Option<Box<dyn Source + Send>> { ... }
    fn configure_hot_reloading(
        &self,
        _events: EventSender,
    ) -> Result<(), Box<dyn Error + Send + Sync>> { ... }
}
Expand description

Bytes sources to load assets from.

This trait provides an abstraction over a basic filesystem, which is used to load assets independantly from the actual storage kind.

As a consumer of this library, you generally don’t need to use this trait, exept when implementing [DirLoadable].

See module-level documentation for more informations.

Required Methods§

fn read(&self, id: &str, ext: &str) -> Result<FileContent<'_>, Error>

Try reading the source given an id and an extension.

If no error occurs, this function returns the raw content of the file as a FileContent, so it can avoid copying bytes around if possible.

Most of the time, you won’t need to use this method, directly, as it is done for you by an [AssetCache] when you load Assets.

fn read_dir( &self, id: &str, f: &mut dyn FnMut(DirEntry<'_>), ) -> Result<(), Error>

Reads the content of a directory.

If no error occurs, this function executes the given closure for each entry in the directory.

§Example
use assets_manager::source::{DirEntry, FileSystem, Source};

// In "assets/example" directory, there are "giant_bat.ron",
// "goblin.ron", and other files that do not have "ron" extension.

let fs = FileSystem::new("assets")?;

let mut dir_content = Vec::new();
fs.read_dir("example.monsters", &mut |entry| {
    if let DirEntry::File(id, ext) = entry {
        if ext == "ron" {
            dir_content.push(id.to_owned());
        }
    }
})?;

// Sort for equality comparison
dir_content.sort();

assert_eq!(dir_content, ["example.monsters.giant_bat", "example.monsters.goblin"]);

fn exists(&self, entry: DirEntry<'_>) -> bool

Returns true if the entry points at an existing entity.

§Example
use assets_manager::source::{DirEntry, FileSystem, Source};

let fs = FileSystem::new("assets")?;

assert!(fs.exists(DirEntry::File("example.monsters.goblin", "ron")));
assert!(!fs.exists(DirEntry::File("example.monsters.spider", "ron")));

Provided Methods§

fn make_source(&self) -> Option<Box<dyn Source + Send>>

Returns a source to use with hot-reloading.

This method returns None when the source does not support hot-reloading. In this case, configure_hot_reloading will never be called.

fn configure_hot_reloading( &self, _events: EventSender, ) -> Result<(), Box<dyn Error + Send + Sync>>

Configures hot-reloading.

This method receives an EventSender to notify the hot-reloading subsystem when assets should be reloaded. It returns a DynUpdateSender to be notified of cache state changes.

Implementations on Foreign Types§

§

impl<S> Source for &S
where S: Source + ?Sized,

§

fn read(&self, id: &str, ext: &str) -> Result<FileContent<'_>, Error>

§

fn read_dir( &self, id: &str, f: &mut dyn FnMut(DirEntry<'_>), ) -> Result<(), Error>

§

fn exists(&self, entry: DirEntry<'_>) -> bool

§

fn make_source(&self) -> Option<Box<dyn Source + Send>>

§

fn configure_hot_reloading( &self, events: EventSender, ) -> Result<(), Box<dyn Error + Send + Sync>>

§

impl<S> Source for Box<S>
where S: Source + ?Sized,

§

fn read(&self, id: &str, ext: &str) -> Result<FileContent<'_>, Error>

§

fn read_dir( &self, id: &str, f: &mut dyn FnMut(DirEntry<'_>), ) -> Result<(), Error>

§

fn exists(&self, entry: DirEntry<'_>) -> bool

§

fn make_source(&self) -> Option<Box<dyn Source + Send>>

§

fn configure_hot_reloading( &self, events: EventSender, ) -> Result<(), Box<dyn Error + Send + Sync>>

§

impl<S> Source for Arc<S>
where S: Source + ?Sized,

§

fn read(&self, id: &str, ext: &str) -> Result<FileContent<'_>, Error>

§

fn read_dir( &self, id: &str, f: &mut dyn FnMut(DirEntry<'_>), ) -> Result<(), Error>

§

fn exists(&self, entry: DirEntry<'_>) -> bool

§

fn make_source(&self) -> Option<Box<dyn Source + Send>>

§

fn configure_hot_reloading( &self, events: EventSender, ) -> Result<(), Box<dyn Error + Send + Sync>>

Implementors§

source§

impl Source for veloren_common_assets::fs::FileSystem

source§

impl Source for CombinedSource

§

impl Source for Empty

§

impl Source for veloren_common_assets::source::FileSystem

§

impl<R> Source for Tar<R>
where R: Read + Seek + Clone,