Skip to content

Commit

Permalink
fix example custom_asset_reader on wasm (bevyengine#10574)
Browse files Browse the repository at this point in the history
# Objective

- Example `custom_asset_reader` fails to build in wasm
```
$ cargo build --profile release --target wasm32-unknown-unknown --example custom_asset_reader
   Compiling bevy v0.12.0 (/Users/runner/work/bevy-website/bevy-website)
error[E0432]: unresolved import `bevy::asset::io::file`
 --> examples/asset/custom_asset_reader.rs:7:9
  |
7 |         file::FileAssetReader, AssetReader, AssetReaderError, AssetSource, AssetSourceId,
  |  
```

## Solution

- Wrap the platform default asset reader instead of the
`FileAssetReader`
  • Loading branch information
mockersf authored and Ray Redondo committed Jan 9, 2024
1 parent c1487e0 commit 5888561
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions examples/asset/custom_asset_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
//! It does not know anything about the asset formats, only how to talk to the underlying storage.
use bevy::{
asset::io::{
file::FileAssetReader, AssetReader, AssetReaderError, AssetSource, AssetSourceId,
PathStream, Reader,
},
asset::io::{AssetReader, AssetReaderError, AssetSource, AssetSourceId, PathStream, Reader},
prelude::*,
utils::BoxedFuture,
};
use std::path::Path;

/// A custom asset reader implementation that wraps a given asset reader implementation
struct CustomAssetReader<T: AssetReader>(T);
struct CustomAssetReader(Box<dyn AssetReader>);

impl<T: AssetReader> AssetReader for CustomAssetReader<T> {
impl AssetReader for CustomAssetReader {
fn read<'a>(
&'a self,
path: &'a Path,
Expand Down Expand Up @@ -52,8 +49,12 @@ impl Plugin for CustomAssetReaderPlugin {
fn build(&self, app: &mut App) {
app.register_asset_source(
AssetSourceId::Default,
AssetSource::build()
.with_reader(|| Box::new(CustomAssetReader(FileAssetReader::new("assets")))),
AssetSource::build().with_reader(|| {
Box::new(CustomAssetReader(
// This is the default reader for the current platform
AssetSource::get_default_reader("assets".to_string())(),
))
}),
);
}
}
Expand Down

0 comments on commit 5888561

Please sign in to comment.