Skip to content

Commit

Permalink
Partial removal of Format<Asset> generic
Browse files Browse the repository at this point in the history
  • Loading branch information
AnneKitsune committed Jul 16, 2018
1 parent 93d6f4c commit 4d232c4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
18 changes: 11 additions & 7 deletions amethyst_assets/src/asset.rs
Expand Up @@ -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
Expand All @@ -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`.
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion amethyst_assets/src/formats.rs
Expand Up @@ -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;
Expand Down
25 changes: 12 additions & 13 deletions amethyst_assets/src/prefab/mod.rs
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion amethyst_audio/src/formats.rs
Expand Up @@ -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))
Expand Down

0 comments on commit 4d232c4

Please sign in to comment.