use std::{
convert::Infallible,
future::{ready, Ready},
};
use {
crate::loader::Loader,
std::{error::Error, future::Future},
};
pub trait Asset: Clone + Sized + Send + Sync + 'static {
type Decoded: Send + Sync;
type DecodeError: Error + Send + Sync + 'static;
type BuildError: Error + Send + Sync + 'static;
type Fut: Future<Output = Result<Self::Decoded, Self::DecodeError>> + Send;
fn name() -> &'static str;
fn decode(bytes: Box<[u8]>, loader: &Loader) -> Self::Fut;
}
pub trait AssetBuild<B>: Asset {
fn build(decoded: Self::Decoded, builder: &mut B) -> Result<Self, Self::BuildError>;
}
pub trait SimpleAsset: Asset {
fn decode(bytes: Box<[u8]>) -> Result<Self::Decoded, Self::DecodeError>;
}
pub trait TrivialAsset: Clone + Sized + Send + Sync + 'static {
type Error: Error + Send + Sync + 'static;
fn name() -> &'static str;
fn decode(bytes: Box<[u8]>) -> Result<Self, Self::Error>;
}
impl<A> Asset for A
where
A: TrivialAsset,
{
type Decoded = A;
type DecodeError = A::Error;
type BuildError = Infallible;
type Fut = Ready<Result<A, A::Error>>;
fn name() -> &'static str {
<A as TrivialAsset>::name()
}
fn decode(bytes: Box<[u8]>, _: &Loader) -> Ready<Result<A, A::Error>> {
ready(<A as SimpleAsset>::decode(bytes))
}
}
impl<A> SimpleAsset for A
where
A: TrivialAsset,
{
fn decode(bytes: Box<[u8]>) -> Result<A, A::Error> {
TrivialAsset::decode(bytes)
}
}
impl<A, B> AssetBuild<B> for A
where
A: TrivialAsset,
{
fn build(decoded: A, _: &mut B) -> Result<A, Infallible> {
Ok(decoded)
}
}