Struct assets_manager::AssetCache
source · pub struct AssetCache<S = FileSystem> { /* private fields */ }Expand description
The main structure of this crate, used to cache assets.
It uses interior mutability, so assets can be added in the cache without requiring a mutable reference, but one is required to remove an asset.
Within the cache, assets are identified with their type and a string. This
string is constructed from the asset path, replacing / by . and removing
the extension. Given that, you cannot use . in your file names except for
the extension.
Note: Using symbolic or hard links within the cached directory can lead to surprising behavior (especially with hot-reloading), and thus should be avoided.
Example
use assets_manager::{Asset, AssetCache, loader};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Point {
x: i32,
y: i32,
}
impl Asset for Point {
const EXTENSION: &'static str = "ron";
type Loader = loader::RonLoader;
}
// Create a cache
let cache = AssetCache::new("assets")?;
// Get an asset from the file `assets/common/position.ron`
let point_handle = cache.load::<Point>("common.position")?;
// Read it
let point = point_handle.read();
println!("Loaded position: {:?}", point);
// Release the lock
drop(point);
// Use hot-reloading
loop {
cache.hot_reload();
println!("Position: {:?}", point_handle.read());
}
Implementations§
source§impl AssetCache<FileSystem>
impl AssetCache<FileSystem>
sourcepub fn new<P: AsRef<Path>>(path: P) -> Result<AssetCache<FileSystem>>
pub fn new<P: AsRef<Path>>(path: P) -> Result<AssetCache<FileSystem>>
Creates a cache that loads assets from the given directory.
Errors
An error will be returned if path is not valid readable directory.
source§impl<S: Source> AssetCache<S>
impl<S: Source> AssetCache<S>
sourcepub fn with_source(source: S) -> AssetCache<S>
pub fn with_source(source: S) -> AssetCache<S>
Creates a cache that loads assets from the given source and tries to
start hot-reloading (if feature hot-reloading is used).
If hot-reloading fails to start, an error is logged.
sourcepub fn without_hot_reloading(source: S) -> AssetCache<S>
pub fn without_hot_reloading(source: S) -> AssetCache<S>
Creates a cache that loads assets from the given source.
sourcepub fn no_record<T, F: FnOnce() -> T>(&self, f: F) -> T
pub fn no_record<T, F: FnOnce() -> T>(&self, f: F) -> T
Temporarily prevent Compound dependencies to be recorded.
This function disables dependencies recording in Compound::load.
Assets loaded during the given closure will not be recorded as
dependencies and the currently loading asset will not be reloaded when
they are.
When hot-reloading is disabled or if the cache’s Source does not
support hot-reloading, this function only returns the result of the
closure given as parameter.
sourcepub fn get_cached<A: Storable>(&self, id: &str) -> Option<Handle<'_, A>>
pub fn get_cached<A: Storable>(&self, id: &str) -> Option<Handle<'_, A>>
Gets a value from the cache.
The value does not have to be an asset, but if it is not, its type must
be marked with the Storable trait.
This function does not attempt to load the value from the source if it is not found in the cache.
sourcepub fn get_or_insert<A: Storable>(&self, id: &str, default: A) -> Handle<'_, A>
pub fn get_or_insert<A: Storable>(&self, id: &str, default: A) -> Handle<'_, A>
Gets a value from the cache or inserts one.
As for get_cached, non-assets types must be marked with Storable.
sourcepub fn contains<A: Storable>(&self, id: &str) -> bool
pub fn contains<A: Storable>(&self, id: &str) -> bool
Returns true if the cache contains the specified asset.
sourcepub fn remove<A: Storable>(&mut self, id: &str) -> bool
pub fn remove<A: Storable>(&mut self, id: &str) -> bool
Removes an asset from the cache, and returns whether it was present in the cache.
Note that you need a mutable reference to the cache, so you cannot have
any Handle, AssetGuard, etc when you call this function.
sourcepub fn take<A: Storable>(&mut self, id: &str) -> Option<A>
pub fn take<A: Storable>(&mut self, id: &str) -> Option<A>
Takes ownership on a cached asset.
The corresponding asset is removed from the cache.
sourcepub fn as_any_cache(&self) -> AnyCache<'_>
pub fn as_any_cache(&self) -> AnyCache<'_>
Converts to an AnyCache.
sourcepub fn load<A: Compound>(&self, id: &str) -> Result<Handle<'_, A>, Error>
pub fn load<A: Compound>(&self, id: &str) -> Result<Handle<'_, A>, Error>
Loads an asset.
If the asset is not found in the cache, it is loaded from the source.
Errors
Errors for Assets can occur in several cases :
- The source could not be read
- Loaded data could not be converted properly
- The asset has no extension
sourcepub fn load_expect<A: Compound>(&self, id: &str) -> Handle<'_, A>
pub fn load_expect<A: Compound>(&self, id: &str) -> Handle<'_, A>
sourcepub fn get_cached_dir<A: DirLoadable>(
&self,
id: &str,
recursive: bool
) -> Option<DirHandle<'_, A>>
pub fn get_cached_dir<A: DirLoadable>(
&self,
id: &str,
recursive: bool
) -> Option<DirHandle<'_, A>>
Loads an directory from the cache.
This function does not attempt to load the it from the source if it is not found in the cache.
sourcepub fn contains_dir<A: DirLoadable>(&self, id: &str, recursive: bool) -> bool
pub fn contains_dir<A: DirLoadable>(&self, id: &str, recursive: bool) -> bool
Returns true if the cache contains the specified directory with the
given recursive parameter.
sourcepub fn load_dir<A: DirLoadable>(
&self,
id: &str,
recursive: bool
) -> Result<DirHandle<'_, A>, Error>
pub fn load_dir<A: DirLoadable>(
&self,
id: &str,
recursive: bool
) -> Result<DirHandle<'_, A>, Error>
Loads all assets of a given type from a directory.
If recursive is true, this function also loads assets recursively
from subdirectories.
The directory’s id is constructed the same way as assets. To specify
the cache’s root, give the empty string ("") as id.
The returned structure can be use to iterate over the loaded assets.
Errors
An error is returned if the given id does not match a valid readable directory.
When loading a directory recursively, directories that can’t be read are ignored.
sourcepub fn load_owned<A: Compound>(&self, id: &str) -> Result<A, Error>
pub fn load_owned<A: Compound>(&self, id: &str) -> Result<A, Error>
Loads an owned version of an asset
Note that the asset will not be fetched from the cache nor will it be
cached. In addition, hot-reloading does not affect the returned value
(if used during Compound::load. It will still be registered as a
dependency).
This can be useful if you need ownership on a non-clonable value.
source§impl<S> AssetCache<S>where
S: Source + Sync,
impl<S> AssetCache<S>where
S: Source + Sync,
sourcepub fn hot_reload(&self)
Available on crate feature hot-reloading only.
pub fn hot_reload(&self)
hot-reloading only.Reloads changed assets.
This function is typically called within a loop.
If an error occurs while reloading an asset, a warning will be logged and the asset will be left unchanged.
This function blocks the current thread until all changed assets are
reloaded, but it does not perform any I/O. However, it needs to lock
some assets for writing, so you must not have any AssetGuard
from the given AssetCache, or you might experience deadlocks. You are
free to keep Handles, though.
If self.source() was created without hot-reloading or if it failed to
start, this function is a no-op.
sourcepub fn enhance_hot_reloading(&'static self)
Available on crate feature hot-reloading only.
pub fn enhance_hot_reloading(&'static self)
hot-reloading only.Enhances hot-reloading.
Having a 'static reference to the cache enables some optimizations,
which you can take advantage of with this function. If an AssetCache
is behind a 'static reference, you should always prefer using this
function over hot_reload.
You only have to call this function once for it to take effect. After
calling this function, subsequent calls to hot_reload and to this
function have no effect.
If self.source() was created without hot-reloading or if it failed to
start, this function is a no-op.