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
use {
crate::{
sync::{Send, Sync},
Cache,
},
alloc::vec::Vec,
core::{
future::Future,
pin::Pin,
task::{Context, Poll},
},
};
#[cfg(feature = "std")]
use std::error::Error;
#[cfg(not(feature = "std"))]
use core::fmt::Display;
pub trait Asset: Send + Sync + Sized + Clone + 'static {
#[cfg(feature = "std")]
type Error: Error + Send + Sync;
#[cfg(not(feature = "std"))]
type Error: Display + Send + Sync;
type Context;
type Repr: Send;
fn build(repr: Self::Repr, ctx: &mut Self::Context) -> Result<Self, Self::Error>;
}
pub trait Format<A: Asset, K>: Send + 'static {
type DecodeFuture: Future<Output = Result<A::Repr, A::Error>> + Send + 'static;
fn decode(self, bytes: Vec<u8>, cache: &Cache<K>) -> Self::DecodeFuture;
}
pub trait AssetDefaultFormat<K>: Asset {
type DefaultFormat: Format<Self, K> + Default;
}
pub trait LeafFormat<A: Asset, K>: Send + 'static {
fn decode(self, bytes: Vec<u8>) -> Result<A::Repr, A::Error>;
}
impl<A, K, F> Format<A, K> for F
where
A: Asset,
F: LeafFormat<A, K>,
{
type DecodeFuture = Ready<Result<A::Repr, A::Error>>;
fn decode(self, bytes: Vec<u8>, _loader: &Cache<K>) -> Self::DecodeFuture {
Ready(Some(LeafFormat::decode(self, bytes)))
}
}
#[doc(hidden)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Ready<T>(Option<T>);
impl<T> Unpin for Ready<T> {}
impl<T> Future for Ready<T> {
type Output = T;
#[inline]
fn poll(mut self: Pin<&mut Self>, _ctx: &mut Context<'_>) -> Poll<T> {
Poll::Ready(self.0.take().expect("Ready polled after completion"))
}
}
pub struct PhantomContext;
pub trait SimpleAsset: Send + Sync + Sized + Clone + 'static {
#[cfg(feature = "std")]
type Error: Error + Send + Sync;
#[cfg(not(feature = "std"))]
type Error: Display + Send + Sync;
}
impl<S> Asset for S
where
S: SimpleAsset,
{
type Error = S::Error;
type Repr = Self;
type Context = PhantomContext;
fn build(repr: Self, _ctx: &mut PhantomContext) -> Result<Self, Self::Error> {
Ok(repr)
}
}