Skip to content

Commit 5f42c9a

Browse files
Fix no_std CI Warnings and WASM Compatibility (#17049)
# Objective - Resolve several warnings encountered when compiling for `no_std` around `dead_code` - Fix compatibility with `wasm32-unknown-unknown` when using `no_std` (identified by Sachymetsu on [Discord](https://discord.com/channels/691052431525675048/692572690833473578/1323365426901549097)) ## Solution - Removed some unused imports - Added `allow(dead_code)` for certain private items when compiling on `no_std` - Fixed `bevy_app` and `bevy_tasks` compatibility with WASM when compiling without `std` by appropriately importing `Box` and feature gating panic unwinding ## Testing - CI
1 parent b78efd3 commit 5f42c9a

File tree

9 files changed

+55
-18
lines changed

9 files changed

+55
-18
lines changed

crates/bevy_app/src/schedule_runner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use bevy_utils::Instant;
1010

1111
#[cfg(target_arch = "wasm32")]
1212
use {
13-
alloc::rc::Rc,
13+
alloc::{boxed::Box, rc::Rc},
1414
core::cell::RefCell,
1515
wasm_bindgen::{prelude::*, JsCast},
1616
};

crates/bevy_math/src/primitives/polygon.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#[cfg(feature = "alloc")]
2-
use alloc::{collections::BTreeMap, vec::Vec};
2+
use {
3+
super::{Measured2d, Triangle2d},
4+
alloc::{collections::BTreeMap, vec::Vec},
5+
};
36

47
use core::cmp::Ordering;
58

69
use crate::Vec2;
710

8-
use super::{Measured2d, Triangle2d};
9-
11+
#[cfg_attr(
12+
not(feature = "alloc"),
13+
expect(dead_code, reason = "this type is only used with the alloc feature")
14+
)]
1015
#[derive(Debug, Clone, Copy)]
1116
enum Endpoint {
1217
Left,
@@ -20,12 +25,20 @@ enum Endpoint {
2025
///
2126
/// This is the order expected by the [`SweepLine`].
2227
#[derive(Debug, Clone, Copy)]
28+
#[cfg_attr(
29+
not(feature = "alloc"),
30+
allow(dead_code, reason = "this type is only used with the alloc feature")
31+
)]
2332
struct SweepLineEvent {
2433
segment: Segment,
2534
/// Type of the vertex (left or right)
2635
endpoint: Endpoint,
2736
}
2837
impl SweepLineEvent {
38+
#[cfg_attr(
39+
not(feature = "alloc"),
40+
allow(dead_code, reason = "this type is only used with the alloc feature")
41+
)]
2942
fn position(&self) -> Vec2 {
3043
match self.endpoint {
3144
Endpoint::Left => self.segment.left,
@@ -51,6 +64,10 @@ impl Ord for SweepLineEvent {
5164
}
5265

5366
/// Orders 2D points according to the order expected by the sweep line and event queue from -X to +X and then -Y to Y.
67+
#[cfg_attr(
68+
not(feature = "alloc"),
69+
allow(dead_code, reason = "this type is only used with the alloc feature")
70+
)]
5471
fn xy_order(a: Vec2, b: Vec2) -> Ordering {
5572
a.x.total_cmp(&b.x).then_with(|| a.y.total_cmp(&b.y))
5673
}
@@ -134,6 +151,10 @@ impl Ord for Segment {
134151
}
135152

136153
/// Holds information about which segment is above and which is below a given [`Segment`]
154+
#[cfg_attr(
155+
not(feature = "alloc"),
156+
expect(dead_code, reason = "this type is only used with the alloc feature")
157+
)]
137158
#[derive(Debug, Clone, Copy)]
138159
struct SegmentOrder {
139160
above: Option<usize>,
@@ -241,6 +262,13 @@ impl<'a> SweepLine<'a> {
241262
/// Test what side of the line through `p1` and `p2` `q` is.
242263
///
243264
/// The result will be `0` if the `q` is on the segment, negative for one side and positive for the other.
265+
#[cfg_attr(
266+
not(feature = "alloc"),
267+
expect(
268+
dead_code,
269+
reason = "this function is only used with the alloc feature"
270+
)
271+
)]
244272
#[inline(always)]
245273
fn point_side(p1: Vec2, p2: Vec2, q: Vec2) -> f32 {
246274
(p2.x - p1.x) * (q.y - p1.y) - (q.x - p1.x) * (p2.y - p1.y)

crates/bevy_reflect/src/impls/glam.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ use assert_type_match::assert_type_match;
44
use bevy_reflect_derive::{impl_reflect, impl_reflect_opaque};
55
use glam::*;
66

7-
#[cfg(not(feature = "std"))]
8-
use alloc::format;
9-
107
/// Reflects the given foreign type as an enum and asserts that the variants/fields match up.
118
macro_rules! reflect_enum {
129
($(#[$meta:meta])* enum $ident:ident { $($ty:tt)* } ) => {
@@ -381,6 +378,7 @@ impl_reflect_opaque!(::glam::BVec4A(Debug, Default, Deserialize, Serialize));
381378

382379
#[cfg(test)]
383380
mod tests {
381+
use alloc::format;
384382
use ron::{
385383
ser::{to_string_pretty, PrettyConfig},
386384
Deserializer,

crates/bevy_reflect/src/impls/smallvec.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
use alloc::boxed::Box;
1+
use alloc::{boxed::Box, vec::Vec};
22
use bevy_reflect_derive::impl_type_path;
33
use core::any::Any;
44
use smallvec::{Array as SmallArray, SmallVec};
55

6-
#[cfg(not(feature = "std"))]
7-
use alloc::{format, vec, vec::Vec};
8-
96
use crate::{
107
self as bevy_reflect, utility::GenericTypeInfoCell, ApplyError, FromReflect, FromType,
118
Generics, GetTypeRegistration, List, ListInfo, ListIter, MaybeTyped, PartialReflect, Reflect,

crates/bevy_tasks/src/executor.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//! [`async-executor`]: https://crates.io/crates/async-executor
99
//! [`edge-executor`]: https://crates.io/crates/edge-executor
1010
11-
pub use async_task::Task;
1211
use core::{
1312
fmt,
1413
panic::{RefUnwindSafe, UnwindSafe},

crates/bevy_tasks/src/single_threaded_task_pool.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl TaskPool {
198198
where
199199
T: 'static + MaybeSend + MaybeSync,
200200
{
201-
#[cfg(all(target_arch = "wasm32", feature = "std"))]
201+
#[cfg(target_arch = "wasm32")]
202202
return Task::wrap_future(future);
203203

204204
#[cfg(all(not(target_arch = "wasm32"), feature = "std"))]
@@ -210,7 +210,7 @@ impl TaskPool {
210210
Task::new(task)
211211
});
212212

213-
#[cfg(not(feature = "std"))]
213+
#[cfg(all(not(target_arch = "wasm32"), not(feature = "std")))]
214214
return {
215215
let task = LOCAL_EXECUTOR.spawn(future);
216216
// Loop until all tasks are done

crates/bevy_tasks/src/task.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use core::{
1414
/// Tasks that panic get immediately canceled. Awaiting a canceled task also causes a panic.
1515
#[derive(Debug)]
1616
#[must_use = "Tasks are canceled when dropped, use `.detach()` to run them in the background."]
17-
pub struct Task<T>(crate::executor::Task<T>);
17+
pub struct Task<T>(async_task::Task<T>);
1818

1919
impl<T> Task<T> {
2020
/// Creates a new task from a given `async_executor::Task`
21-
pub fn new(task: crate::executor::Task<T>) -> Self {
21+
pub fn new(task: async_task::Task<T>) -> Self {
2222
Self(task)
2323
}
2424

crates/bevy_tasks/src/thread_executor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use core::marker::PhantomData;
22
use std::thread::{self, ThreadId};
33

4-
use crate::executor::{Executor, Task};
4+
use crate::executor::Executor;
5+
use async_task::Task;
56
use futures_lite::Future;
67

78
/// An executor that can only be ticked on the thread it was instantiated on. But

crates/bevy_tasks/src/wasm_task.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use alloc::boxed::Box;
12
use core::{
23
any::Any,
34
future::{Future, IntoFuture},
@@ -59,7 +60,12 @@ impl<T> Future for Task<T> {
5960
// NOTE: Propagating the panic here sorta has parity with the async_executor behavior.
6061
// For those tasks, polling them after a panic returns a `None` which gets `unwrap`ed, so
6162
// using `resume_unwind` here is essentially keeping the same behavior while adding more information.
63+
#[cfg(feature = "std")]
6264
Poll::Ready(Ok(Err(panic))) => std::panic::resume_unwind(panic),
65+
#[cfg(not(feature = "std"))]
66+
Poll::Ready(Ok(Err(_panic))) => {
67+
unreachable!("catching a panic is only possible with std")
68+
}
6369
Poll::Ready(Err(_)) => panic!("Polled a task after it was cancelled"),
6470
Poll::Pending => Poll::Pending,
6571
}
@@ -74,6 +80,14 @@ struct CatchUnwind<F: UnwindSafe>(#[pin] F);
7480
impl<F: Future + UnwindSafe> Future for CatchUnwind<F> {
7581
type Output = Result<F::Output, Panic>;
7682
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
77-
std::panic::catch_unwind(AssertUnwindSafe(|| self.project().0.poll(cx)))?.map(Ok)
83+
let f = AssertUnwindSafe(|| self.project().0.poll(cx));
84+
85+
#[cfg(feature = "std")]
86+
let result = std::panic::catch_unwind(f)?;
87+
88+
#[cfg(not(feature = "std"))]
89+
let result = f();
90+
91+
result.map(Ok)
7892
}
7993
}

0 commit comments

Comments
 (0)