@@ -9,6 +9,8 @@ use crossbeam_channel::Sender;
9
9
use std:: fmt:: Debug ;
10
10
11
11
/// Events that happen on assets of type `T`
12
+ ///
13
+ /// Events sent via the [Assets] struct will always be sent with a _Weak_ handle
12
14
pub enum AssetEvent < T : Asset > {
13
15
Created { handle : Handle < T > } ,
14
16
Modified { handle : Handle < T > } ,
@@ -44,6 +46,18 @@ impl<T: Asset> Debug for AssetEvent<T> {
44
46
}
45
47
46
48
/// Stores Assets of a given type and tracks changes to them.
49
+ ///
50
+ /// Each asset is mapped by a unique [HandleId](crate::HandleId), allowing any [Handle](crate::Handle)
51
+ /// with the same HandleId to access it. These assets remain loaded for as long as a Strong handle
52
+ /// to that asset exists.
53
+ ///
54
+ /// To store a reference to an asset without forcing it to stay loaded, you can use a Weak handle.
55
+ /// To make a Weak handle a Strong one, use [Assets::get_handle](crate::Assets::get_handle) or pass
56
+ /// the Assets collection into the handle's [make_strong](crate::Handle::make_strong) method.
57
+ ///
58
+ /// Remember, if there are no Strong handles for an asset (i.e. they have all been dropped), the asset
59
+ /// will unload. Make sure you always have a Strong handle when you want to keep an asset loaded!
60
+ ///
47
61
#[ derive( Debug ) ]
48
62
pub struct Assets < T : Asset > {
49
63
assets : HashMap < HandleId , T > ,
@@ -60,6 +74,10 @@ impl<T: Asset> Assets<T> {
60
74
}
61
75
}
62
76
77
+ /// Adds an asset to the collection, returning a Strong handle to that asset.
78
+ ///
79
+ /// # Events
80
+ /// * [`AssetEvent::Created`]
63
81
pub fn add ( & mut self , asset : T ) -> Handle < T > {
64
82
let id = HandleId :: random :: < T > ( ) ;
65
83
self . assets . insert ( id, asset) ;
@@ -69,13 +87,27 @@ impl<T: Asset> Assets<T> {
69
87
self . get_handle ( id)
70
88
}
71
89
90
+ /// Add/modify the asset pointed to by the given handle.
91
+ ///
92
+ /// Unless there exists another Strong handle for this asset, it's advised to use the returned
93
+ /// Strong handle. Not doing so may result in the unexpected release of the asset.
94
+ ///
95
+ /// See [set_untracked](Assets::set_untracked) for more info.
72
96
#[ must_use = "not using the returned strong handle may result in the unexpected release of the asset" ]
73
97
pub fn set < H : Into < HandleId > > ( & mut self , handle : H , asset : T ) -> Handle < T > {
74
98
let id: HandleId = handle. into ( ) ;
75
99
self . set_untracked ( id, asset) ;
76
100
self . get_handle ( id)
77
101
}
78
102
103
+ /// Add/modify the asset pointed to by the given handle.
104
+ ///
105
+ /// If an asset already exists with the given HandleId, it will be modified. Otherwise the new asset
106
+ /// will be inserted.
107
+ ///
108
+ /// # Events
109
+ /// * [`AssetEvent::Created`]: Sent if the asset did not yet exist with the given handle
110
+ /// * [`AssetEvent::Modified`]: Sent if the asset with given handle already existed
79
111
pub fn set_untracked < H : Into < HandleId > > ( & mut self , handle : H , asset : T ) {
80
112
let id: HandleId = handle. into ( ) ;
81
113
if self . assets . insert ( id, asset) . is_some ( ) {
@@ -89,14 +121,23 @@ impl<T: Asset> Assets<T> {
89
121
}
90
122
}
91
123
124
+ /// Get the asset for the given handle.
125
+ ///
126
+ /// This is the main method for accessing asset data from an [Assets] collection. If you need
127
+ /// mutable access to the asset, use [get_mut](Assets::get_mut).
92
128
pub fn get < H : Into < HandleId > > ( & self , handle : H ) -> Option < & T > {
93
129
self . assets . get ( & handle. into ( ) )
94
130
}
95
131
132
+ /// Checks if an asset exists for the given handle
96
133
pub fn contains < H : Into < HandleId > > ( & self , handle : H ) -> bool {
97
134
self . assets . contains_key ( & handle. into ( ) )
98
135
}
99
136
137
+ /// Get mutable access to the asset for the given handle.
138
+ ///
139
+ /// This is the main method for mutably accessing asset data from an [Assets] collection. If you
140
+ /// do not need mutable access to the asset, you may also use [get](Assets::get).
100
141
pub fn get_mut < H : Into < HandleId > > ( & mut self , handle : H ) -> Option < & mut T > {
101
142
let id: HandleId = handle. into ( ) ;
102
143
self . events . send ( AssetEvent :: Modified {
@@ -105,10 +146,15 @@ impl<T: Asset> Assets<T> {
105
146
self . assets . get_mut ( & id)
106
147
}
107
148
149
+ /// Gets a _Strong_ handle pointing to the same asset as the given one
108
150
pub fn get_handle < H : Into < HandleId > > ( & self , handle : H ) -> Handle < T > {
109
151
Handle :: strong ( handle. into ( ) , self . ref_change_sender . clone ( ) )
110
152
}
111
153
154
+ /// Get mutable access to an asset for the given handle, inserting a new value if none exists.
155
+ ///
156
+ /// # Events
157
+ /// * [`AssetEvent::Created`]: Sent if the asset did not yet exist with the given handle
112
158
pub fn get_or_insert_with < H : Into < HandleId > > (
113
159
& mut self ,
114
160
handle : H ,
@@ -129,10 +175,12 @@ impl<T: Asset> Assets<T> {
129
175
borrowed
130
176
}
131
177
178
+ /// Get an iterator over all assets in the collection.
132
179
pub fn iter ( & self ) -> impl Iterator < Item = ( HandleId , & T ) > {
133
180
self . assets . iter ( ) . map ( |( k, v) | ( * k, v) )
134
181
}
135
182
183
+ /// Get a mutable iterator over all assets in the collection.
136
184
pub fn iter_mut ( & mut self ) -> impl Iterator < Item = ( HandleId , & mut T ) > {
137
185
for id in self . assets . keys ( ) {
138
186
self . events . send ( AssetEvent :: Modified {
@@ -142,10 +190,17 @@ impl<T: Asset> Assets<T> {
142
190
self . assets . iter_mut ( ) . map ( |( k, v) | ( * k, v) )
143
191
}
144
192
193
+ /// Get an iterator over all HandleIds in the collection.
145
194
pub fn ids ( & self ) -> impl Iterator < Item = HandleId > + ' _ {
146
195
self . assets . keys ( ) . cloned ( )
147
196
}
148
197
198
+ /// Remove an asset for the given handle.
199
+ ///
200
+ /// The asset is returned if it existed in the collection, otherwise `None`.
201
+ ///
202
+ /// # Events
203
+ /// * [`AssetEvent::Removed`]
149
204
pub fn remove < H : Into < HandleId > > ( & mut self , handle : H ) -> Option < T > {
150
205
let id: HandleId = handle. into ( ) ;
151
206
let asset = self . assets . remove ( & id) ;
@@ -190,10 +245,12 @@ impl<T: Asset> Assets<T> {
190
245
}
191
246
}
192
247
248
+ /// Gets the number of assets in the collection
193
249
pub fn len ( & self ) -> usize {
194
250
self . assets . len ( )
195
251
}
196
252
253
+ /// Returns true if there are no stored assets
197
254
pub fn is_empty ( & self ) -> bool {
198
255
self . assets . is_empty ( )
199
256
}
0 commit comments