asset_cache.rs

 1use crate::{AppContext, SharedString, SharedUri};
 2use futures::Future;
 3use std::hash::{Hash, Hasher};
 4use std::path::PathBuf;
 5use std::sync::Arc;
 6
 7#[derive(Debug, PartialEq, Eq, Hash, Clone)]
 8pub(crate) enum UriOrPath {
 9    Uri(SharedUri),
10    Path(Arc<PathBuf>),
11    Embedded(SharedString),
12}
13
14impl From<SharedUri> for UriOrPath {
15    fn from(value: SharedUri) -> Self {
16        Self::Uri(value)
17    }
18}
19
20impl From<Arc<PathBuf>> for UriOrPath {
21    fn from(value: Arc<PathBuf>) -> Self {
22        Self::Path(value)
23    }
24}
25
26/// A trait for asynchronous asset loading.
27pub trait Asset {
28    /// The source of the asset.
29    type Source: Clone + Hash + Send;
30
31    /// The loaded asset
32    type Output: Clone + Send;
33
34    /// Load the asset asynchronously
35    fn load(
36        source: Self::Source,
37        cx: &mut AppContext,
38    ) -> impl Future<Output = Self::Output> + Send + 'static;
39}
40
41/// Use a quick, non-cryptographically secure hash function to get an identifier from data
42pub fn hash<T: Hash>(data: &T) -> u64 {
43    let mut hasher = collections::FxHasher::default();
44    data.hash(&mut hasher);
45    hasher.finish()
46}