Skip to content

Commit 5888561

Browse files
mockersfRay Redondo
authored and
Ray Redondo
committed
fix example custom_asset_reader on wasm (bevyengine#10574)
# 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`
1 parent c1487e0 commit 5888561

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

examples/asset/custom_asset_reader.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
//! It does not know anything about the asset formats, only how to talk to the underlying storage.
44
55
use bevy::{
6-
asset::io::{
7-
file::FileAssetReader, AssetReader, AssetReaderError, AssetSource, AssetSourceId,
8-
PathStream, Reader,
9-
},
6+
asset::io::{AssetReader, AssetReaderError, AssetSource, AssetSourceId, PathStream, Reader},
107
prelude::*,
118
utils::BoxedFuture,
129
};
1310
use std::path::Path;
1411

1512
/// A custom asset reader implementation that wraps a given asset reader implementation
16-
struct CustomAssetReader<T: AssetReader>(T);
13+
struct CustomAssetReader(Box<dyn AssetReader>);
1714

18-
impl<T: AssetReader> AssetReader for CustomAssetReader<T> {
15+
impl AssetReader for CustomAssetReader {
1916
fn read<'a>(
2017
&'a self,
2118
path: &'a Path,
@@ -52,8 +49,12 @@ impl Plugin for CustomAssetReaderPlugin {
5249
fn build(&self, app: &mut App) {
5350
app.register_asset_source(
5451
AssetSourceId::Default,
55-
AssetSource::build()
56-
.with_reader(|| Box::new(CustomAssetReader(FileAssetReader::new("assets")))),
52+
AssetSource::build().with_reader(|| {
53+
Box::new(CustomAssetReader(
54+
// This is the default reader for the current platform
55+
AssetSource::get_default_reader("assets".to_string())(),
56+
))
57+
}),
5758
);
5859
}
5960
}

0 commit comments

Comments
 (0)