1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! Conveniently load, store and cache external resources.
//!
//! This crate aims at providing a filesystem abstraction to easily load external resources.
//! It was originally thought for games, but can, of course, be used in other contexts.
//!
//! The structure [`AssetCache`] is the entry point of the crate. See [`Asset`] documentation
//! to see how to define custom asset types.
//!
//! # Cargo features
//!
//! - `hot-reloading`: Add hot-reloading.
//! - `macros`: Add support for deriving `Asset` trait.
//!
//! ### Additional sources
//!
//! Enable reading assets from sources other than the filesystem.
//! These sources are defined in the [`source`] module:
//!
//! - `embedded`: Embeds asset files directly in your binary at compile time
//! - `zip`: Reads assets from ZIP archives
//! - Optional compression: `zip-deflate`, `zip-zstd`
//! - `tar`: Reads assets from TAR archives
//!
//! ### Additional formats
//!
//! These features add support for various asset formats:
//!
//! - Serialisation formats (using [`serde`]): `bincode`, `json`,
//! `msgpack`, `ron`, `toml`, `yaml`.
//! - Image formats (using [`image`]): `bmp`, `jpeg`, `png` `webp`.
//! - GlTF format (using [`gltf`]): `gltf`.
//!
//! [`serde`]: https://docs.rs/serde
//!
//! ## External crates support
//!
//! Support of some other crates is done in external crates:
//! - [`ggez`](https://github.com/ggez/ggez): [`ggez-assets_manager`](https://crates.io/crates/ggez-assets_manager)
//! - [`kira`](https://github.com/tesselode/kira): [`assets_manager-kira`](https://crates.io/crates/assets_manager-kira)
//! - [`rodio`](https://github.com/RustAudio/rodio): [`assets_manager-rodio`](https://crates.io/crates/assets_manager-rodio)
//!
//! ### Internal features
//!
//! These features change internal data structures implementations.
//!
//! - [`parking_lot`]: Use `parking_lot`'s synchronization primitives.
//! - `faster-hash`: Use a faster hashing algorithm (enabled by default).
//!
//! # Basic example
//!
//! Given a file `assets/common/position.ron` which contains this:
//!
//! ```text
//! Point(
//! x: 5,
//! y: -6,
//! )
//! ```
//!
//! You can load and use it as follows:
//!
//! ```
//! # cfg_if::cfg_if! { if #[cfg(feature = "ron")] {
//! use assets_manager::{BoxedError, AssetCache, FileAsset};
//! use serde::Deserialize;
//! use std::borrow::Cow;
//!
//! // The struct you want to load
//! #[derive(Deserialize)]
//! struct Point {
//! x: i32,
//! y: i32,
//! }
//!
//! // Specify how you want the structure to be loaded
//! impl FileAsset for Point {
//! // The extension of the files to look into
//! const EXTENSION: &'static str = "ron";
//!
//! // The serialization format
//! //
//! // In this specific case, the derive macro could be used but we use the
//! // full version as a demo.
//! fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Result<Self, BoxedError> {
//! assets_manager::asset::load_ron(&bytes)
//! }
//! }
//!
//! // Create a new cache to load assets under the "./assets" folder
//! let cache = AssetCache::new("assets")?;
//!
//! // Get a handle on the asset
//! // This will load the file `./assets/common/position.ron`
//! let handle = cache.load::<Point>("common.position")?;
//!
//! // Lock the asset for reading
//! // Any number of read locks can exist at the same time,
//! // but none can exist when the asset is reloaded
//! let point = handle.read();
//!
//! // The asset is now ready to be used
//! assert_eq!(point.x, 5);
//! assert_eq!(point.y, -6);
//!
//! // Loading the same asset retreives it from the cache
//! let other_handle = cache.load("common.position")?;
//! assert!(std::ptr::eq(handle, other_handle));
//! # }}
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! # Hot-reloading
//!
//! Hot-reloading is a major feature of `assets_manager`: when a file is added,
//! modified or deleted, the values of all assets that depend on this file are
//! automatically and transparently updated. It is managed automatically in the
//! background.
//!
//! See the [`asset`] module for a precise description of how assets interact
//! with hot-reloading.
extern crate self as assets_manager;
pub use ;
pub use AssetCache;
pub use ;
pub use ;
pub use ;
pub use OnceInitCell;
pub use ;
/// Implements [`Asset`] for a type.
///
/// Note that the type must implement the right traits for it to work (eg
/// `serde::Deserialize` or `std::str::FromStr`).
///
/// # Supported formats
///
/// - `"json"`: Use [`asset::load_json`] and extension `.json`
/// - `"ron"`: Use [`asset::load_ron`] and extension `.ron`
/// - `"toml"`: Use [`asset::load_toml`] and extension `.toml`
/// - `"txt"`: Use [`asset::load_text`] and extension `.txt`
/// - `"yaml"` or `"yml"`: Use [`asset::load_yaml`] and extensions `.yaml` and `.yml`
///
/// # Example
///
/// ```rust
/// # cfg_if::cfg_if! { if #[cfg(feature = "ron")] {
/// use assets_manager::{Asset, AssetCache, BoxedError};
/// // Define a type loaded as ron
/// #[derive(Asset, serde::Deserialize)]
/// #[asset_format = "ron"]
/// struct Point {
/// x: i32,
/// y: i32,
/// }
///
/// // Define a type loaded as text
/// #[derive(Asset)]
/// #[asset_format = "txt"]
/// struct Name(String);
///
/// impl std::str::FromStr for Name {
/// type Err = BoxedError;
///
/// fn from_str(s: &str) -> Result<Self, BoxedError> {
/// Ok(Self(String::from(s)))
/// }
/// }
///
/// let cache = AssetCache::new("assets")?;
///
/// // Load "assets/common/position.ron"
/// let position = cache.load::<Point>("common.position")?;
/// assert_eq!(position.read().x, 5);
/// assert_eq!(position.read().y, -6);
///
/// // Load "assets/common/name.txt"
/// let name = cache.load::<Name>("common.name")?;
/// assert_eq!(name.read().0, "Aragorn");
/// # }}
/// # Ok::<(), assets_manager::BoxedError>(())
/// ```
pub use Asset;