Trait 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 method
fn configure_hot_reloading(
&self,
_events: EventSender,
) -> Result<(), Box<dyn Error + Sync + Send>> { ... }
}
Expand description
Bytes sources to load assets from.
This trait provides an abstraction over filesystem operations, allowing assets to be loaded independently of their storage backend (filesystem, archive, embedded, etc.).
As a consumer of this library, you typically only need to use this trait
directly when implementing [Asset
] or [DirLoadable
].
See module-level documentation for more information.
Required Methods§
fn read(&self, id: &str, ext: &str) -> Result<FileContent<'_>, Error>
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 Asset
s.
fn read_dir(
&self,
id: &str,
f: &mut dyn FnMut(DirEntry<'_>),
) -> Result<(), Error>
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
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 configure_hot_reloading(
&self,
_events: EventSender,
) -> Result<(), Box<dyn Error + Sync + Send>>
fn configure_hot_reloading( &self, _events: EventSender, ) -> Result<(), Box<dyn Error + Sync + Send>>
Starts hot-reloading.
This method receives an EventSender
to notify the hot-reloading
subsystem when assets should be reloaded.
The returned result is there purely for conveniency: if this function returns an error, it is logged and nothing more is done with it.
The default implementation does nothing and returns Ok(())
.