diff --git a/crates/bevy_asset/Cargo.toml b/crates/bevy_asset/Cargo.toml index 5352a798ed..9de73b565c 100644 --- a/crates/bevy_asset/Cargo.toml +++ b/crates/bevy_asset/Cargo.toml @@ -37,7 +37,6 @@ log = { version = "0.4", features = ["release_max_level_info"] } notify = { version = "5.0.0-pre.2", optional = true } parking_lot = "0.11.0" rand = "0.7.3" -async-trait = "0.1.40" [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = { version = "0.2" } diff --git a/crates/bevy_asset/src/io/file_asset_io.rs b/crates/bevy_asset/src/io/file_asset_io.rs index cda69be745..c98a2920df 100644 --- a/crates/bevy_asset/src/io/file_asset_io.rs +++ b/crates/bevy_asset/src/io/file_asset_io.rs @@ -1,7 +1,6 @@ use crate::{filesystem_watcher::FilesystemWatcher, AssetIo, AssetIoError, AssetServer}; use anyhow::Result; -use async_trait::async_trait; -use bevy_ecs::Res; +use bevy_ecs::{bevy_utils::BoxedFuture, Res}; use bevy_utils::HashSet; use crossbeam_channel::TryRecvError; use fs::File; @@ -42,23 +41,24 @@ impl FileAssetIo { } } -#[async_trait] impl AssetIo for FileAssetIo { - async fn load_path(&self, path: &Path) -> Result, AssetIoError> { - let mut bytes = Vec::new(); - match File::open(self.root_path.join(path)) { - Ok(mut file) => { - file.read_to_end(&mut bytes)?; - } - Err(e) => { - if e.kind() == std::io::ErrorKind::NotFound { - return Err(AssetIoError::NotFound(path.to_owned())); - } else { - return Err(e.into()); + fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result, AssetIoError>> { + Box::pin(async move { + let mut bytes = Vec::new(); + match File::open(self.root_path.join(path)) { + Ok(mut file) => { + file.read_to_end(&mut bytes)?; + } + Err(e) => { + if e.kind() == std::io::ErrorKind::NotFound { + return Err(AssetIoError::NotFound(path.to_owned())); + } else { + return Err(e.into()); + } } } - } - Ok(bytes) + Ok(bytes) + }) } fn read_directory( @@ -103,7 +103,7 @@ impl AssetIo for FileAssetIo { } } -#[cfg(feature = "filesystem_watcher")] +#[cfg(all(feature = "filesystem_watcher", not(target_arch = "wasm32")))] pub fn filesystem_watcher_system(asset_server: Res) { let mut changed = HashSet::default(); let asset_io = diff --git a/crates/bevy_asset/src/io/mod.rs b/crates/bevy_asset/src/io/mod.rs index 156bb24b1a..113df6d681 100644 --- a/crates/bevy_asset/src/io/mod.rs +++ b/crates/bevy_asset/src/io/mod.rs @@ -9,7 +9,7 @@ pub use file_asset_io::*; pub use wasm_asset_io::*; use anyhow::Result; -use async_trait::async_trait; +use bevy_ecs::bevy_utils::BoxedFuture; use downcast_rs::{impl_downcast, Downcast}; use std::{ io, @@ -29,10 +29,8 @@ pub enum AssetIoError { } /// Handles load requests from an AssetServer -#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] -#[cfg_attr(not(target_arch = "wasm32"), async_trait)] pub trait AssetIo: Downcast + Send + Sync + 'static { - async fn load_path(&self, path: &Path) -> Result, AssetIoError>; + fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result, AssetIoError>>; fn read_directory( &self, path: &Path, diff --git a/crates/bevy_asset/src/io/wasm_asset_io.rs b/crates/bevy_asset/src/io/wasm_asset_io.rs index 86d490b328..ecac806744 100644 --- a/crates/bevy_asset/src/io/wasm_asset_io.rs +++ b/crates/bevy_asset/src/io/wasm_asset_io.rs @@ -1,6 +1,6 @@ use crate::{AssetIo, AssetIoError}; use anyhow::Result; -use async_trait::async_trait; +use bevy_ecs::bevy_utils::BoxedFuture; use js_sys::Uint8Array; use std::path::{Path, PathBuf}; use wasm_bindgen::JsCast; @@ -19,18 +19,19 @@ impl WasmAssetIo { } } -#[async_trait(?Send)] impl AssetIo for WasmAssetIo { - async fn load_path(&self, path: &Path) -> Result, AssetIoError> { - let path = self.root_path.join(path); - let window = web_sys::window().unwrap(); - let resp_value = JsFuture::from(window.fetch_with_str(path.to_str().unwrap())) - .await - .unwrap(); - let resp: Response = resp_value.dyn_into().unwrap(); - let data = JsFuture::from(resp.array_buffer().unwrap()).await.unwrap(); - let bytes = Uint8Array::new(&data).to_vec(); - Ok(bytes) + fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result, AssetIoError>> { + Box::pin(async move { + let path = self.root_path.join(path); + let window = web_sys::window().unwrap(); + let resp_value = JsFuture::from(window.fetch_with_str(path.to_str().unwrap())) + .await + .unwrap(); + let resp: Response = resp_value.dyn_into().unwrap(); + let data = JsFuture::from(resp.array_buffer().unwrap()).await.unwrap(); + let bytes = Uint8Array::new(&data).to_vec(); + Ok(bytes) + }) } fn read_directory( diff --git a/crates/bevy_utils/src/lib.rs b/crates/bevy_utils/src/lib.rs index 06639c4daf..ca594002e7 100644 --- a/crates/bevy_utils/src/lib.rs +++ b/crates/bevy_utils/src/lib.rs @@ -3,7 +3,12 @@ use std::{future::Future, pin::Pin}; pub use ahash::AHasher; +#[cfg(not(target_arch = "wasm32"))] pub type BoxedFuture<'a, T> = Pin + Send + 'a>>; + +#[cfg(target_arch = "wasm32")] +pub type BoxedFuture<'a, T> = Pin + 'a>>; + pub type HashMap = std::collections::HashMap; pub type HashSet = std::collections::HashSet;