assets.rs

 1use anyhow::{anyhow, Result};
 2use std::borrow::Cow;
 3
 4pub trait AssetSource: 'static {
 5    fn load(&self, path: &str) -> Result<Cow<[u8]>>;
 6}
 7
 8impl AssetSource for () {
 9    fn load(&self, path: &str) -> Result<Cow<[u8]>> {
10        Err(anyhow!(
11            "get called on empty asset provider with \"{}\"",
12            path
13        ))
14    }
15}
16
17pub struct AssetCache {
18    source: Box<dyn AssetSource>,
19}
20
21impl AssetCache {
22    pub fn new(source: impl AssetSource) -> Self {
23        Self {
24            source: Box::new(source),
25        }
26    }
27}