From 0a8576b71026678394c13109bc5291eb68e43d7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sat, 24 Apr 2021 18:14:04 +0000 Subject: [PATCH] support assets of any size (#1997) Fixes #1892 The following code is a cut down version of the issue, and crashes the same way: ```rust enum AssetLifecycleEvent { Create(T), Free } fn main() { let (sender, _receiver) = crossbeam_channel::unbounded(); sender.send(AssetLifecycleEvent::<[u32; 32000]>::Free).unwrap(); } ``` - We're creating a channel that need to be able to hold `AssetLifecycleEvent::Create(T)` which has the size of our type `T` - The two variants of the enums have a very different size By keeping `T` boxed while sending through the channel, it doesn't crash --- crates/bevy_asset/src/asset_server.rs | 2 +- crates/bevy_asset/src/loader.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index 7edbd337b64f3..0f4ae4081ab67 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -477,7 +477,7 @@ impl AssetServer { } } - let _ = assets.set(result.id, result.asset); + let _ = assets.set(result.id, *result.asset); } Ok(AssetLifecycleEvent::Free(handle_id)) => { if let HandleId::AssetPathId(id) = handle_id { diff --git a/crates/bevy_asset/src/loader.rs b/crates/bevy_asset/src/loader.rs index aa0f3cd009b5a..cf50622711dba 100644 --- a/crates/bevy_asset/src/loader.rs +++ b/crates/bevy_asset/src/loader.rs @@ -139,7 +139,7 @@ impl<'a> LoadContext<'a> { /// The result of loading an asset of type `T` #[derive(Debug)] pub struct AssetResult { - pub asset: T, + pub asset: Box, pub id: HandleId, pub version: usize, } @@ -168,7 +168,7 @@ impl AssetLifecycle for AssetLifecycleChannel { self.sender .send(AssetLifecycleEvent::Create(AssetResult { id, - asset: *asset, + asset, version, })) .unwrap()