mirror of
https://github.com/bevyengine/bevy
synced 2024-11-28 23:50:20 +00:00
ac49dce4ca
# Objective Simplify implementing some asset traits without Box::pin(async move{}) shenanigans. Fixes (in part) https://github.com/bevyengine/bevy/issues/11308 ## Solution Use async-fn in traits when possible in all traits. Traits with return position impl trait are not object safe however, and as AssetReader and AssetWriter are both used with dynamic dispatch, you need a Boxed version of these futures anyway. In the future, Rust is [adding ](https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html)proc macros to generate these traits automatically, and at some point in the future dyn traits should 'just work'. Until then.... this seemed liked the right approach given more ErasedXXX already exist, but, no clue if there's plans here! Especially since these are public now, it's a bit of an unfortunate API, and means this is a breaking change. In theory this saves some performance when these traits are used with static dispatch, but, seems like most code paths go through dynamic dispatch, which boxes anyway. I also suspect a bunch of the lifetime annotations on these function could be simplified now as the BoxedFuture was often the only thing returned which needed a lifetime annotation, but I'm not touching that for now as traits + lifetimes can be so tricky. This is a revival of [pull/11362](https://github.com/bevyengine/bevy/pull/11362) after a spectacular merge f*ckup, with updates to the latest Bevy. Just to recap some discussion: - Overall this seems like a win for code quality, especially when implementing these traits, but a loss for having to deal with ErasedXXX variants. - `ConditionalSend` was the preferred name for the trait that might be Send, to deal with wasm platforms. - When reviewing be sure to disable whitespace difference, as that's 95% of the PR. ## Changelog - AssetReader, AssetWriter, AssetLoader, AssetSaver and Process now use async-fn in traits rather than boxed futures. ## Migration Guide - Custom implementations of AssetReader, AssetWriter, AssetLoader, AssetSaver and Process should switch to async fn rather than returning a bevy_utils::BoxedFuture. - Simultaniously, to use dynamic dispatch on these traits you should instead use dyn ErasedXXX.
68 lines
2.1 KiB
Rust
68 lines
2.1 KiB
Rust
//! Implements a custom asset io loader.
|
|
//! An [`AssetReader`] is what the asset server uses to read the raw bytes of assets.
|
|
//! It does not know anything about the asset formats, only how to talk to the underlying storage.
|
|
|
|
use bevy::{
|
|
asset::io::{
|
|
AssetReader, AssetReaderError, AssetSource, AssetSourceId, ErasedAssetReader, PathStream,
|
|
Reader,
|
|
},
|
|
prelude::*,
|
|
};
|
|
use std::path::Path;
|
|
|
|
/// A custom asset reader implementation that wraps a given asset reader implementation
|
|
struct CustomAssetReader(Box<dyn ErasedAssetReader>);
|
|
|
|
impl AssetReader for CustomAssetReader {
|
|
async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
|
info!("Reading {:?}", path);
|
|
self.0.read(path).await
|
|
}
|
|
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
|
self.0.read_meta(path).await
|
|
}
|
|
|
|
async fn read_directory<'a>(
|
|
&'a self,
|
|
path: &'a Path,
|
|
) -> Result<Box<PathStream>, AssetReaderError> {
|
|
self.0.read_directory(path).await
|
|
}
|
|
|
|
async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
|
|
self.0.is_directory(path).await
|
|
}
|
|
}
|
|
|
|
/// A plugins that registers our new asset reader
|
|
struct CustomAssetReaderPlugin;
|
|
|
|
impl Plugin for CustomAssetReaderPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.register_asset_source(
|
|
AssetSourceId::Default,
|
|
AssetSource::build().with_reader(|| {
|
|
Box::new(CustomAssetReader(
|
|
// This is the default reader for the current platform
|
|
AssetSource::get_default_reader("assets".to_string())(),
|
|
))
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins((CustomAssetReaderPlugin, DefaultPlugins))
|
|
.add_systems(Startup, setup)
|
|
.run();
|
|
}
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
commands.spawn(Camera2dBundle::default());
|
|
commands.spawn(SpriteBundle {
|
|
texture: asset_server.load("branding/icon.png"),
|
|
..default()
|
|
});
|
|
}
|