rgpui 1.3.0

GUI UI framework
Documentation
//! 异步资源缓存 —— 按来源(URI / 路径 / 嵌入)缓存并异步加载资源数据。

use crate::{App, SharedString, SharedUri};
use futures::{Future, TryFutureExt};

use std::fmt::Debug;
use std::hash::{BuildHasher, Hash};
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// 表示资源来源的枚举。
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum Resource {
    /// 此资源位于给定 URI
    Uri(SharedUri),
    /// 此资源位于文件系统中的给定路径
    Path(Arc<Path>),
    /// 此资源嵌入在应用程序二进制文件中
    Embedded(SharedString),
}

impl From<SharedUri> for Resource {
    fn from(value: SharedUri) -> Self {
        Self::Uri(value)
    }
}

impl From<PathBuf> for Resource {
    fn from(value: PathBuf) -> Self {
        Self::Path(value.into())
    }
}

impl From<Arc<Path>> for Resource {
    fn from(value: Arc<Path>) -> Self {
        Self::Path(value)
    }
}

/// 异步资产加载的 trait。
pub trait Asset: 'static {
    /// 资产的来源。
    type Source: Clone + Hash + Send;

    /// 加载后的资产
    type Output: Clone + Send;

    /// 异步加载资产
    fn load(
        source: Self::Source,
        cx: &mut App,
    ) -> impl Future<Output = Self::Output> + Send + 'static;
}

/// 资产加载器,在加载期间记录 [`Result`] 的 [`Err`] 变体日志
pub enum AssetLogger<T> {
    #[doc(hidden)]
    _Phantom(PhantomData<T>, &'static dyn crate::seal::Sealed),
}

impl<T, R, E> Asset for AssetLogger<T>
where
    T: Asset<Output = Result<R, E>>,
    R: Clone + Send,
    E: Clone + Send + std::fmt::Display,
{
    type Source = T::Source;

    type Output = T::Output;

    fn load(
        source: Self::Source,
        cx: &mut App,
    ) -> impl Future<Output = Self::Output> + Send + 'static {
        let load = T::load(source, cx);
        load.inspect_err(|e| log::error!("Failed to load asset: {}", e))
    }
}

/// 使用快速、非加密安全的哈希函数从数据获取标识符
pub fn hash<T: Hash>(data: &T) -> u64 {
    crate::collections::FxBuildHasher.hash_one(data)
}