Skip to content

Commit 7b4717f

Browse files
committed
Don't use rayon in examples on wasm
1 parent d735510 commit 7b4717f

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ version = "0.3.55"
3737
features = ["CanvasRenderingContext2d", "Document", "Element", "HtmlCanvasElement", "ImageData", "Window"]
3838

3939
[dev-dependencies]
40+
instant = "0.1.12"
4041
winit = "0.26.1"
42+
43+
[dev-dependencies.image]
44+
version = "0.23.14"
45+
# Disable rayon on web
46+
default-features = false
47+
features = ["jpeg"]
48+
49+
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
50+
# Turn rayon back on everywhere else; creating the separate entry resets the features to default.
4151
image = "0.23.14"
4252
rayon = "1.5.1"

examples/animation.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::f64::consts::PI;
2-
use std::time::Instant;
2+
use instant::Instant;
3+
#[cfg(not(target_arch = "wasm32"))]
34
use rayon::prelude::*;
45
use softbuffer::GraphicsContext;
56
use winit::event::{Event, WindowEvent};
@@ -64,7 +65,7 @@ fn main() {
6465
}
6566

6667
fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>>{
67-
(0..60).into_par_iter().map(|frame_id|{
68+
let render = |frame_id|{
6869
let elapsed = ((frame_id as f64)/(60.0))*2.0*PI;
6970
let buffer = (0..((width * height) as usize))
7071
.map(|index| {
@@ -81,5 +82,11 @@ fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>>{
8182
.collect::<Vec<_>>();
8283

8384
buffer
84-
}).collect()
85+
};
86+
87+
#[cfg(target_arch = "wasm32")]
88+
return (0..60).map(render).collect();
89+
90+
#[cfg(not(target_arch = "wasm32"))]
91+
(0..60).into_par_iter().map(render).collect()
8592
}

src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub enum SoftBufferError<W: HasRawWindowHandle> {
1616
PlatformError(Option<String>, Option<Box<dyn Error>>)
1717
}
1818

19+
#[allow(unused)] // This isn't used on all platforms
1920
pub(crate) fn unwrap<T, E: std::error::Error + 'static, W: HasRawWindowHandle>(res: Result<T, E>, str: &str) -> Result<T, SoftBufferError<W>>{
2021
match res{
2122
Ok(t) => Ok(t),

0 commit comments

Comments
 (0)