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
use crate::{
Future, Result,
error::QuicksilverError,
};
use futures::Async;
pub struct Asset<T>(AssetData<T>);
enum AssetData<T> {
Loading(Box<dyn Future<Item = T, Error = QuicksilverError>>),
Loaded(T)
}
impl<T> Asset<T> {
pub fn new(future: impl Future<Item = T, Error = QuicksilverError> + 'static) -> Asset<T> {
Asset(AssetData::Loading(Box::new(future)))
}
pub fn execute(&mut self, loaded: impl FnOnce(&mut T) -> Result<()>) -> Result<()> {
self.execute_or(loaded, || Ok(()))
}
pub fn execute_or(&mut self, loaded: impl FnOnce(&mut T) -> Result<()>, loading: impl FnOnce() -> Result<()>) -> Result<()> {
let result = match self.0 {
AssetData::Loading(ref mut asset) => asset.poll()?,
_ => Async::NotReady
};
if let Async::Ready(asset) = result {
self.0 = AssetData::Loaded(asset);
}
match self.0 {
AssetData::Loading(_) => loading(),
AssetData::Loaded(ref mut data) => loaded(data)
}
}
}