Expand description
Asset loader.
Asset and AssetField derive macros
Creates structures to act as two loading stages of asset and implement asset using those.
First stages must be deserializable with serde.
All fields with #[external]
must implement AssetField<External>
. Which has blanket impl for Asset
implementors and some wrappers, like Option<A>
and Arc<[A]>
where A: Asset
.
All fields with #[container]
attribute must implement AssetField<Container>
. It can be derived using derive(AssetField)
. They can in turn contain fields with #[external]
and #[container]
attributes. Also implemented for wrappers like Option<A>
and Arc<[A]>
.
All fields without special attributes of the target struct must implement DeserializeOwned
.
All fields transiently with #[external] attribute will be replaced with id for first stage struct and AssetResult
s for second stage.
Second stages will have AssetResult
s fields in place of the assets.
Example
/// Simple deserializable type. Included as-is into generated types for `#[derive(Asset)]` and #[derive(AssetField)].
#[derive(Clone, serde::Deserialize)]
struct Foo;
/// Trivial asset type.
#[derive(Clone, Asset)]
#[asset(name = "bar")]
struct Bar;
/// Asset field type. `AssetField<Container>` implementation is generated, but not `Asset` implementation.
/// Fields of types with `#[derive(AssetField)]` attribute are not replaced by uuids as external assets.
#[derive(Clone, AssetField)]
struct Baz;
/// Asset structure. Implements Asset trait using
/// two generated structures are intermediate phases.
#[derive(Clone, Asset)]
#[asset(name = "assetstruct")]
struct AssetStruct {
/// Deserializable types are inlined into asset as is.
foo: Foo,
/// `AssetField<External>` is implemented for all `Asset` implementors.
/// Deserialized as `AssetId` and loaded recursively.
#[asset(external)]
bar: Bar,
/// Container fields are deserialized similar to types that derive `Asset`.
/// If there is no external asset somewhere in hierarchy, decoded `Baz` is structurally equivalent to `Baz`.
#[asset(container)]
baz: Baz,
}
Modules
Structs
Type for unique asset identification. There are 2^64-1 valid values of this type that should be enough for now.
Virtual storage for all available assets.
AssetId
augmented with type information, specifying which asset type is referenced.
Enums
Traits
An asset type that can be built from decoded representation.
Asset building trait.
Users should implement this trait for at least single choice of B
.
But it is highly recommended to implement this trait for wide choices of B
for improved composability.
Because composite asset will implement AssetBuild<B>
only for such types B
for which all components implement AssetBuild<B>
.
This trait is used by code generated by Asset
and AssetField
derive macros.
Types must implement AssetField
in order to be usable as field types in structures
that derive Asset
or AssetField
.
This trait is AssetBuild
but for AssetField
implementations.
Simple assets have no dependencies.
For this reason their decode
function is always sync and do not take Loader
argument.
Trivial assets have no dependencies and do not require building.
They are decoded directly from bytes.
And thus they implement AssetBuild<B>
for any B
.