Skip to content

Commit bd7fa36

Browse files
alice-i-cecilemockersf
authored andcommitted
Fix https / http asset loader on WASM (#21283)
# Objective Our `web_asset` example does not build on Wasm. Ironic. As reported in #21280, there are a few problems: 1. Imports are broken: one hard error and a few warnings. 2. `HttpWasmAssetReader` needs a `+ use<'>` to conform to the Rust 2024 edition (unreported, but discovered) 3. We're returning a reference to a temporary value, which is ultimately caused by 2 ## Solution 1. Move the imports into the correct feature-gated block. 2. Slap some `+ use<'>` on the function definition like the compiler told me to. This fixes the "returning a reference to a temporary value" problem, as we're no longer implicitly capturing. Fixes #21280. Special thanks to @kristoff3r for pointing out my mistake! ## Testing I've tested this PR using `bevy run --features https --example web_asset web` via the bevy_cli. Because of changes to `getrandom`, this was non-trivial to build for web manually.
1 parent 752ee6f commit bd7fa36

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

crates/bevy_asset/src/io/wasm.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ fn js_value_to_err(context: &str) -> impl FnOnce(JsValue) -> std::io::Error + '_
5353

5454
impl HttpWasmAssetReader {
5555
// Also used by [`WebAssetReader`](crate::web::WebAssetReader)
56-
pub(crate) async fn fetch_bytes(&self, path: PathBuf) -> Result<impl Reader, AssetReaderError> {
56+
pub(crate) async fn fetch_bytes(
57+
&self,
58+
path: PathBuf,
59+
) -> Result<impl Reader + use<>, AssetReaderError> {
5760
// The JS global scope includes a self-reference via a specializing name, which can be used to determine the type of global context available.
5861
let global: Global = js_sys::global().unchecked_into();
5962
let promise = if !global.window().is_undefined() {

crates/bevy_asset/src/io/web.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use crate::io::{AssetReader, AssetReaderError, Reader};
22
use crate::io::{AssetSource, PathStream};
33
use crate::{AssetApp, AssetPlugin};
4-
use alloc::{borrow::ToOwned, boxed::Box};
4+
use alloc::boxed::Box;
55
use bevy_app::{App, Plugin};
66
use bevy_tasks::ConditionalSendFuture;
7-
use blocking::unblock;
87
use std::path::{Path, PathBuf};
98
use tracing::warn;
109

@@ -124,8 +123,9 @@ async fn get<'a>(path: PathBuf) -> Result<Box<dyn Reader>, AssetReaderError> {
124123
#[cfg(not(target_arch = "wasm32"))]
125124
async fn get(path: PathBuf) -> Result<Box<dyn Reader>, AssetReaderError> {
126125
use crate::io::VecReader;
127-
use alloc::{boxed::Box, vec::Vec};
126+
use alloc::{borrow::ToOwned, boxed::Box, vec::Vec};
128127
use bevy_platform::sync::LazyLock;
128+
use blocking::unblock;
129129
use std::io::{self, BufReader, Read};
130130

131131
let str_path = path.to_str().ok_or_else(|| {

0 commit comments

Comments
 (0)