Skip to content
Permalink
Browse files

Partial removal of Format<Asset> generic

  • Loading branch information...
jojolepro committed Jul 16, 2018
1 parent 93d6f4c commit 4d232c47301a799d1f12836a39854a138982fac8
Showing with 27 additions and 22 deletions.
  1. +11 −7 amethyst_assets/src/asset.rs
  2. +2 −1 amethyst_assets/src/formats.rs
  3. +12 −13 amethyst_assets/src/prefab/mod.rs
  4. +2 −1 amethyst_audio/src/formats.rs
@@ -32,14 +32,16 @@ pub trait Asset: Send + Sync + 'static {
/// A format, providing a conversion from bytes to asset data, which is then
/// in turn accepted by `Asset::from_data`. Examples for formats are
/// `Png`, `Obj` and `Wave`.
pub trait Format<A: Asset>: Send + 'static {
pub trait Format: Send + 'static {
/// A unique identifier for this format.
const NAME: &'static str;
/// Options specific to the format, which are passed to `import`.
/// E.g. for textures this would be stuff like mipmap levels and
/// sampler info.
type Options: Send + 'static;

type TargetAsset: Asset;

/// Reads the given bytes and produces asset data.
///
/// ## Reload
@@ -54,7 +56,7 @@ pub trait Format<A: Asset>: Send + 'static {
source: Arc<Source>,
options: Self::Options,
create_reload: bool,
) -> Result<FormatValue<A>>;
) -> Result<FormatValue<Self::TargetAsset>>;
}

/// The `Ok` return value of `Format::import` for a given asset type `A`.
@@ -77,33 +79,35 @@ impl<A: Asset> FormatValue<A> {
/// All `SimpleFormat` types automatically implement `Format`.
/// This format assumes that the asset name is the full path and the asset is only
/// contained in one file.
pub trait SimpleFormat<A: Asset> {
pub trait SimpleFormat {
/// A unique identifier for this format.
const NAME: &'static str;
/// Options specific to the format, which are passed to `import`.
/// E.g. for textures this would be stuff like mipmap levels and
/// sampler info.
type Options: Clone + Send + Sync + 'static;

type TargetAsset: Asset;

/// Produces asset data from given bytes.
fn import(&self, bytes: Vec<u8>, options: Self::Options) -> Result<A::Data>;
}

impl<A, T> Format<A> for T
impl<T> Format for T
where
A: Asset,
T: SimpleFormat<A> + Clone + Send + Sync + 'static,
T: SimpleFormat + Clone + Send + Sync + 'static,
{
const NAME: &'static str = T::NAME;
type Options = T::Options;
type TargetAsset = T::TargetAsset;

fn import(
&self,
name: String,
source: Arc<Source>,
options: Self::Options,
create_reload: bool,
) -> Result<FormatValue<A>> {
) -> Result<FormatValue<Self::TargetAsset>> {
#[cfg(feature = "profiler")]
profile_scope!("import_asset");
if create_reload {
@@ -7,13 +7,14 @@ use {Asset, SimpleFormat};
#[derive(Default, Clone, Debug)]
pub struct RonFormat;

impl<T> SimpleFormat<T> for RonFormat
impl<T> SimpleFormat for RonFormat
where
T: Asset,
T::Data: for<'a> Deserialize<'a> + Send + Sync + 'static,
{
const NAME: &'static str = "Ron";
type Options = ();
type TargetAsset = T;

fn import(&self, bytes: Vec<u8>, _: ()) -> Result<T::Data, Error> {
use ron::de::Deserializer;
@@ -319,39 +319,37 @@ where
/// - `A`: `Asset`,
/// - `F`: `Format` for loading `A`
#[derive(Deserialize, Serialize)]
pub enum AssetPrefab<A, F>
pub enum AssetPrefab<F>
where
A: Asset,
F: Format<A>,
F: Format,
{
/// From existing handle
#[serde(skip)]
Handle(Handle<A>),
Handle(Handle<F::TargetAsset>),

/// From file, (name, format, format options)
File(String, F, F::Options),
}

impl<'a, A, F> PrefabData<'a> for AssetPrefab<A, F>
impl<'a, F> PrefabData<'a> for AssetPrefab<F>
where
A: Asset,
F: Format<A> + Clone,
F: Format + Clone,
F::Options: Clone,
{
type SystemData = (
ReadExpect<'a, Loader>,
WriteStorage<'a, Handle<A>>,
Read<'a, AssetStorage<A>>,
WriteStorage<'a, Handle<F::TargetAsset>>,
Read<'a, AssetStorage<F::TargetAsset>>,
);

type Result = Handle<A>;
type Result = Handle<F::TargetAsset>;

fn load_prefab(
&self,
entity: Entity,
system_data: &mut Self::SystemData,
_: &[Entity],
) -> Result<Handle<A>, PrefabError> {
) -> Result<Handle<F::TargetAsset>, PrefabError> {
let handle = match *self {
AssetPrefab::Handle(ref handle) => handle.clone(),
AssetPrefab::File(ref name, ref format, ref options) => system_data.0.load(
@@ -418,11 +416,12 @@ where
format: F,
options: F::Options,
progress: P,
) -> Handle<Prefab<T>>
) -> Handle<F::TargetAsset>
where
F: Format<Prefab<T>>,
F: Format,
N: Into<String>,
P: Progress,
F::TargetAsset: Prefab<T>,
{
self.loader
.load(name, format, options, progress, &self.storage)
@@ -22,10 +22,11 @@ impl SimpleFormat<Audio> for WavFormat {
#[derive(Clone)]
pub struct OggFormat;

impl SimpleFormat<Audio> for OggFormat {
impl SimpleFormat for OggFormat {
const NAME: &'static str = "OGG";

type Options = ();
type TargetAsset = Audio;

fn import(&self, bytes: Vec<u8>, _: ()) -> Result<AudioData> {
Ok(AudioData(bytes))

0 comments on commit 4d232c4

Please sign in to comment.
You can’t perform that action at this time.