Skip to content

Commit

Permalink
Fix Clippy lints on WASM (bevyengine#13030)
Browse files Browse the repository at this point in the history
# Objective

- Fixes bevyengine#13024.

## Solution

- Run `cargo clippy --target wasm32-unknown-unknown` until there are no
more errors.
  - I recommend reviewing one commit at a time :)

---

## Changelog

- Fixed Clippy lints for `wasm32-unknown-unknown` target.
- Updated `bevy_transform`'s `README.md`.
  • Loading branch information
BD103 committed Apr 20, 2024
1 parent 70d9dfd commit b3d3daa
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 17 deletions.
11 changes: 4 additions & 7 deletions crates/bevy_app/src/schedule_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,10 @@ impl Plugin for ScheduleRunnerPlugin {
let g = f.clone();

let c = move || {
let mut app = Rc::get_mut(&mut rc).unwrap();
let delay = tick(&mut app, wait);
match delay {
Ok(delay) => {
set_timeout(f.borrow().as_ref().unwrap(), delay.unwrap_or(asap))
}
Err(_) => {}
let app = Rc::get_mut(&mut rc).unwrap();
let delay = tick(app, wait);
if let Ok(delay) = delay {
set_timeout(f.borrow().as_ref().unwrap(), delay.unwrap_or(asap));
}
};
*g.borrow_mut() = Some(Closure::wrap(Box::new(c) as Box<dyn FnMut()>));
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_asset/src/io/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl HttpWasmAssetReader {
}
}

fn js_value_to_err<'a>(context: &'a str) -> impl FnOnce(JsValue) -> std::io::Error + 'a {
fn js_value_to_err(context: &str) -> impl FnOnce(JsValue) -> std::io::Error + '_ {
move |value| {
let message = match JSON::stringify(&value) {
Ok(js_str) => format!("Failed to {context}: {js_str}"),
Expand Down Expand Up @@ -81,7 +81,7 @@ impl HttpWasmAssetReader {
Ok(reader)
}
404 => Err(AssetReaderError::NotFound(path)),
status => Err(AssetReaderError::HttpError(status as u16)),
status => Err(AssetReaderError::HttpError(status)),
}
}
}
Expand All @@ -94,7 +94,7 @@ impl AssetReader for HttpWasmAssetReader {

async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
let meta_path = get_meta_path(&self.root_path.join(path));
Ok(self.fetch_bytes(meta_path).await?)
self.fetch_bytes(meta_path).await
}

async fn read_directory<'a>(
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct Position { x: f32, y: f32 }

### Worlds

Entities, Components, and Resources are stored in a `World`. Worlds, much like Rust std collections like HashSet and Vec, expose operations to insert, read, write, and remove the data they store.
Entities, Components, and Resources are stored in a `World`. Worlds, much like `std::collections`'s `HashSet` and `Vec`, expose operations to insert, read, write, and remove the data they store.

```rust
use bevy_ecs::world::World;
Expand Down
6 changes: 4 additions & 2 deletions crates/bevy_render/src/view/window/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl ScreenshotManager {

#[cfg(target_arch = "wasm32")]
{
match (|| {
let save_screenshot = || {
use image::EncodableLayout;
use wasm_bindgen::{JsCast, JsValue};

Expand Down Expand Up @@ -107,7 +107,9 @@ impl ScreenshotManager {
html_element.click();
web_sys::Url::revoke_object_url(&url)?;
Ok::<(), JsValue>(())
})() {
};

match (save_screenshot)() {
Ok(_) => info!("Screenshot saved to {}", path.display()),
Err(e) => error!("Cannot save screenshot, error: {e:?}"),
};
Expand Down
13 changes: 12 additions & 1 deletion crates/bevy_tasks/src/single_threaded_task_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;
use std::{cell::RefCell, future::Future, marker::PhantomData, mem, rc::Rc};

thread_local! {
static LOCAL_EXECUTOR: async_executor::LocalExecutor<'static> = async_executor::LocalExecutor::new();
static LOCAL_EXECUTOR: async_executor::LocalExecutor<'static> = const { async_executor::LocalExecutor::new() };
}

/// Used to create a [`TaskPool`].
Expand Down Expand Up @@ -105,11 +105,21 @@ impl TaskPool {
F: for<'scope> FnOnce(&'env mut Scope<'scope, 'env, T>),
T: Send + 'static,
{
// SAFETY: This safety comment applies to all references transmuted to 'env.
// Any futures spawned with these references need to return before this function completes.
// This is guaranteed because we drive all the futures spawned onto the Scope
// to completion in this function. However, rust has no way of knowing this so we
// transmute the lifetimes to 'env here to appease the compiler as it is unable to validate safety.
// Any usages of the references passed into `Scope` must be accessed through
// the transmuted reference for the rest of this function.

let executor = &async_executor::LocalExecutor::new();
// SAFETY: As above, all futures must complete in this function so we can change the lifetime
let executor: &'env async_executor::LocalExecutor<'env> =
unsafe { mem::transmute(executor) };

let results: RefCell<Vec<Rc<RefCell<Option<T>>>>> = RefCell::new(Vec::new());
// SAFETY: As above, all futures must complete in this function so we can change the lifetime
let results: &'env RefCell<Vec<Rc<RefCell<Option<T>>>>> =
unsafe { mem::transmute(&results) };

Expand All @@ -120,6 +130,7 @@ impl TaskPool {
env: PhantomData,
};

// SAFETY: As above, all futures must complete in this function so we can change the lifetime
let scope_ref: &'env mut Scope<'_, 'env, T> = unsafe { mem::transmute(&mut scope) };

f(scope_ref);
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_transform/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Bevy Transform

This crate is largely a 1:1 port from [legion_transform](https://github.com/AThilenius/legion_transform) (ecs: legion, math: nalgebra) to bevy (ecs: bevy_ecs, math: glam)
This crate contains types and functions associated with the `Transform` component.
2 changes: 1 addition & 1 deletion crates/bevy_winit/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ pub(crate) fn changed_windows(

#[cfg(target_arch = "wasm32")]
if window.canvas != cache.window.canvas {
window.canvas = cache.window.canvas.clone();
window.canvas.clone_from(&cache.window.canvas);
warn!(
"Bevy currently doesn't support modifying the window canvas after initialization."
);
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl WinitWindows {
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let canvas = document
.query_selector(&selector)
.query_selector(selector)
.expect("Cannot query for canvas element.");
if let Some(canvas) = canvas {
let canvas = canvas.dyn_into::<web_sys::HtmlCanvasElement>().ok();
Expand Down

0 comments on commit b3d3daa

Please sign in to comment.