Skip to main content

bevy_cache/
asset_server_ext.rs

1use bevy::prelude::*;
2
3use crate::config::CacheConfig;
4use crate::error::CacheError;
5use crate::manifest::CacheManifest;
6
7/// Extension trait that adds cache-aware loading to [`AssetServer`].
8///
9/// # Example
10/// ```rust,ignore
11/// use bevy_cache::AssetServerCacheExt;
12///
13/// fn setup(
14///     asset_server: Res<AssetServer>,
15///     manifest: Res<CacheManifest>,
16///     config: Res<CacheConfig>,
17/// ) {
18///     let handle: Handle<Image> = asset_server
19///         .load_cached(&manifest, &config, "scene_thumb")
20///         .unwrap_or_else(|_| regenerate_thumbnail(&asset_server));
21/// }
22/// ```
23pub trait AssetServerCacheExt {
24    /// Load a cached asset by key.
25    ///
26    /// Returns `Ok(Handle<A>)` when the cache file is present, or
27    /// [`CacheError::NotFound`] when there is no manifest entry or the
28    /// file was deleted from disk.
29    fn load_cached<A: Asset>(
30        &self,
31        manifest: &CacheManifest,
32        config: &CacheConfig,
33        key: &str,
34    ) -> Result<Handle<A>, CacheError>;
35}
36
37impl AssetServerCacheExt for AssetServer {
38    fn load_cached<A: Asset>(
39        &self,
40        manifest: &CacheManifest,
41        config: &CacheConfig,
42        key: &str,
43    ) -> Result<Handle<A>, CacheError> {
44        manifest.load_cached(config, key, self)
45    }
46}