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
use std::path::Path;
#[cfg(target_arch="wasm32")]
extern "C" {
fn is_loaded(index: u32) -> bool;
fn is_errored(index: u32) -> bool;
}
pub trait Loadable: Sized + Clone {
type Error: Clone;
fn load<P: AsRef<Path>>(path: P) -> LoadingAsset<Self>;
#[cfg(target_arch="wasm32")]
fn parse_result(handle: LoadingHandle, loaded: bool, errored: bool) -> LoadingAsset<Self>;
}
#[derive(Clone)]
pub struct LoadingHandle(pub(crate) u32);
#[derive(Clone)]
pub enum LoadingAsset<Asset: Loadable> {
Loading(LoadingHandle),
Loaded(Asset),
Errored(Asset::Error)
}
impl<T: Loadable> LoadingAsset<T> {
pub fn update(&mut self) {
self.update_impl();
}
#[cfg(not(target_arch="wasm32"))]
fn update_impl(&mut self) {}
#[cfg(target_arch="wasm32")]
fn update_impl(&mut self) {
*self = match self.clone() {
LoadingAsset::Loading(handle) => {
let loaded = unsafe { is_loaded(handle.0) };
let errored = unsafe { is_errored(handle.0) };
T::parse_result(handle, loaded, errored)
},
LoadingAsset::Errored(error) => LoadingAsset::Errored(error),
LoadingAsset::Loaded(result) => LoadingAsset::Loaded(result)
}
}
}