From 7b4717fdc4e3533ed52c0eae3b975fddcc68d3e5 Mon Sep 17 00:00:00 2001 From: Liam Murphy Date: Wed, 23 Feb 2022 19:13:26 +1100 Subject: [PATCH] Don't use rayon in examples on wasm --- Cargo.toml | 10 ++++++++++ examples/animation.rs | 13 ++++++++++--- src/error.rs | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fb929ad..8022e3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,16 @@ version = "0.3.55" features = ["CanvasRenderingContext2d", "Document", "Element", "HtmlCanvasElement", "ImageData", "Window"] [dev-dependencies] +instant = "0.1.12" winit = "0.26.1" + +[dev-dependencies.image] +version = "0.23.14" +# Disable rayon on web +default-features = false +features = ["jpeg"] + +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] +# Turn rayon back on everywhere else; creating the separate entry resets the features to default. image = "0.23.14" rayon = "1.5.1" \ No newline at end of file diff --git a/examples/animation.rs b/examples/animation.rs index d28e9ef..f3f5d09 100644 --- a/examples/animation.rs +++ b/examples/animation.rs @@ -1,5 +1,6 @@ use std::f64::consts::PI; -use std::time::Instant; +use instant::Instant; +#[cfg(not(target_arch = "wasm32"))] use rayon::prelude::*; use softbuffer::GraphicsContext; use winit::event::{Event, WindowEvent}; @@ -64,7 +65,7 @@ fn main() { } fn pre_render_frames(width: usize, height: usize) -> Vec>{ - (0..60).into_par_iter().map(|frame_id|{ + let render = |frame_id|{ let elapsed = ((frame_id as f64)/(60.0))*2.0*PI; let buffer = (0..((width * height) as usize)) .map(|index| { @@ -81,5 +82,11 @@ fn pre_render_frames(width: usize, height: usize) -> Vec>{ .collect::>(); buffer - }).collect() + }; + + #[cfg(target_arch = "wasm32")] + return (0..60).map(render).collect(); + + #[cfg(not(target_arch = "wasm32"))] + (0..60).into_par_iter().map(render).collect() } \ No newline at end of file diff --git a/src/error.rs b/src/error.rs index 6e19074..e7656ca 100644 --- a/src/error.rs +++ b/src/error.rs @@ -16,6 +16,7 @@ pub enum SoftBufferError { PlatformError(Option, Option>) } +#[allow(unused)] // This isn't used on all platforms pub(crate) fn unwrap(res: Result, str: &str) -> Result>{ match res{ Ok(t) => Ok(t),