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
use {
crate::{
asset::Asset,
cache::{Cache, LocalCache},
},
std::{
fmt::Debug,
future::{ready, Future, Ready},
},
};
pub trait Format<A, K>: Debug + 'static {
type DecodeFuture: Future<Output = eyre::Result<A>> + Send + 'static;
fn decode(self, key: K, bytes: Box<[u8]>, cache: &Cache<K>) -> Self::DecodeFuture;
}
pub trait LocalFormat<A, K>: Debug + 'static {
type DecodeFuture: Future<Output = eyre::Result<A>> + 'static;
fn decode_local(self, key: K, bytes: Box<[u8]>, cache: &LocalCache<K>) -> Self::DecodeFuture;
}
pub trait SimpleFormat<A, K>: Debug + 'static {
fn decode_simple(self, key: K, bytes: Box<[u8]>) -> eyre::Result<A>;
}
impl<A, K, F> Format<A, K> for F
where
A: Send + 'static,
F: SimpleFormat<A, K> + 'static,
{
type DecodeFuture = Ready<eyre::Result<A>>;
fn decode(self, key: K, bytes: Box<[u8]>, _cache: &Cache<K>) -> Ready<eyre::Result<A>> {
ready(self.decode_simple(key, bytes))
}
}
impl<A, K, F> LocalFormat<A, K> for F
where
A: 'static,
F: SimpleFormat<A, K> + 'static,
{
type DecodeFuture = Ready<eyre::Result<A>>;
fn decode_local(
self,
key: K,
bytes: Box<[u8]>,
_cache: &LocalCache<K>,
) -> Ready<eyre::Result<A>> {
ready(self.decode_simple(key, bytes))
}
}
pub trait AssetDefaultFormat<K>: Asset {
type DefaultFormat: Default;
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PassthroughFormat;
impl<A, K> SimpleFormat<A, K> for PassthroughFormat
where
A: From<Box<[u8]>>,
{
fn decode_simple(self, _: K, bytes: Box<[u8]>) -> eyre::Result<A> {
Ok(bytes.into())
}
}