use {
crate::{Cache, Ready},
alloc::vec::Vec,
core::{convert::Infallible, future::Future},
maybe_sync::{MaybeSend, MaybeSync},
};
#[cfg(feature = "std")]
use std::error::Error;
#[cfg(not(feature = "std"))]
use core::fmt::Display;
pub trait Asset: MaybeSend + MaybeSync + Sized + Clone + 'static {
#[cfg(feature = "std")]
type Error: Error + MaybeSend + MaybeSync;
#[cfg(not(feature = "std"))]
type Error: Display + MaybeSend + MaybeSync;
type Context;
type Repr: MaybeSend;
type BuildFuture: Future<Output = Result<Self, Self::Error>> + MaybeSend + 'static;
fn build(repr: Self::Repr, ctx: &mut Self::Context) -> Self::BuildFuture;
}
pub trait Format<A: Asset, K>: MaybeSend + 'static {
#[cfg(feature = "std")]
type Error: Error + MaybeSend + MaybeSync + 'static;
#[cfg(not(feature = "std"))]
type Error: Display + MaybeSend + MaybeSync + 'static;
type DecodeFuture: Future<Output = Result<A::Repr, Self::Error>> + MaybeSend + '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 SyncAsset: MaybeSend + MaybeSync + Sized + Clone + 'static {
#[cfg(feature = "std")]
type Error: Error + MaybeSend + MaybeSync;
#[cfg(not(feature = "std"))]
type Error: Display + MaybeSend + MaybeSync;
type Context;
type Repr: MaybeSend;
fn build(repr: Self::Repr, ctx: &mut Self::Context) -> Result<Self, Self::Error>;
}
impl<S> Asset for S
where
S: SyncAsset,
{
type Error = S::Error;
type Repr = S::Repr;
type Context = S::Context;
type BuildFuture = Ready<Result<Self, Self::Error>>;
#[inline]
fn build(repr: S::Repr, ctx: &mut S::Context) -> Ready<Result<Self, Self::Error>> {
Ready(Some(S::build(repr, ctx)))
}
}
pub struct PhantomContext;
pub trait SimpleAsset: MaybeSend + MaybeSync + Sized + Clone + 'static {}
impl<S> SyncAsset for S
where
S: SimpleAsset,
{
type Error = Infallible;
type Repr = Self;
type Context = PhantomContext;
#[inline]
fn build(repr: Self, _ctx: &mut PhantomContext) -> Result<Self, Self::Error> {
Ok(repr)
}
}