From fd2536eb28f21245c391f1a01a1404df78c3bce4 Mon Sep 17 00:00:00 2001 From: Bartosz Przygoda Date: Sun, 4 Feb 2018 21:05:18 +0100 Subject: [PATCH] Guard parallel/async deps with "parallel" feature --- Cargo.toml | 12 ++++++++++-- src/dispatch/builder.rs | 14 +++++++------- src/dispatch/dispatcher.rs | 17 +++++++++-------- src/dispatch/mod.rs | 8 ++++---- src/dispatch/stage.rs | 2 +- src/lib.rs | 8 ++++---- 6 files changed, 35 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cd9156b..ceb336f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,10 +21,18 @@ travis-ci = { repository = "torkleyy/shred" } arrayvec = "0.3" fxhash = "0.2" mopa = "0.2" -pulse = "0.5" -rayon = "0.8" +pulse = { version = "0.5", optional = true } +rayon = { version = "0.8", optional = true } shred-derive = { path = "shred-derive", version = "0.3" } smallvec = "0.4" [dev-dependencies] cgmath = "0.15" + +[features] +default = ["parallel"] +parallel = ["pulse", "rayon"] + +[[example]] +name = "par_seq" +required-features = ["parallel"] \ No newline at end of file diff --git a/src/dispatch/builder.rs b/src/dispatch/builder.rs index 32ffb98..d4ca871 100644 --- a/src/dispatch/builder.rs +++ b/src/dispatch/builder.rs @@ -90,7 +90,7 @@ pub struct DispatcherBuilder<'a, 'b> { map: FxHashMap, stages_builder: StagesBuilder<'a>, thread_local: ThreadLocal<'b>, - #[cfg(not(target_os = "emscripten"))] + #[cfg(feature = "parallel")] thread_pool: Option<::std::sync::Arc<::rayon::ThreadPool>>, } @@ -239,7 +239,7 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> { /// Same as /// [`add_pool()`](struct.DispatcherBuilder.html#method.add_pool), /// but returns `self` to enable method chaining. - #[cfg(not(target_os = "emscripten"))] + #[cfg(feature = "parallel")] pub fn with_pool(mut self, pool: ::std::sync::Arc<::rayon::ThreadPool>) -> Self { self.add_pool(pool); @@ -248,7 +248,7 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> { /// Attach a rayon thread pool to the builder /// and use that instead of creating one. - #[cfg(not(target_os = "emscripten"))] + #[cfg(feature = "parallel")] pub fn add_pool(&mut self, pool: ::std::sync::Arc<::rayon::ThreadPool>) { self.thread_pool = Some(pool); } @@ -261,14 +261,14 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> { pub fn build(self) -> Dispatcher<'a, 'b> { use dispatch::dispatcher::new_dispatcher; - #[cfg(not(target_os = "emscripten"))] + #[cfg(feature = "parallel")] let d = new_dispatcher( self.stages_builder.build(), self.thread_local, self.thread_pool.unwrap_or_else(Self::create_thread_pool), ); - #[cfg(target_os = "emscripten")] + #[cfg(not(feature = "parallel"))] let d = new_dispatcher(self.stages_builder.build(), self.thread_local); d @@ -281,7 +281,7 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> { SystemId(id) } - #[cfg(not(target_os = "emscripten"))] + #[cfg(feature = "parallel")] fn create_thread_pool() -> ::std::sync::Arc<::rayon::ThreadPool> { use std::sync::Arc; use rayon::{Configuration, ThreadPool}; @@ -292,7 +292,7 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> { } } -#[cfg(not(target_os = "emscripten"))] +#[cfg(feature = "parallel")] impl<'b> DispatcherBuilder<'static, 'b> { /// Builds an async dispatcher. /// diff --git a/src/dispatch/dispatcher.rs b/src/dispatch/dispatcher.rs index 4e4f0fa..1de3bb5 100644 --- a/src/dispatch/dispatcher.rs +++ b/src/dispatch/dispatcher.rs @@ -9,7 +9,8 @@ use system::RunNow; pub struct Dispatcher<'a, 'b> { stages: Vec>, thread_local: ThreadLocal<'b>, - #[cfg(not(target_os = "emscripten"))] thread_pool: ::std::sync::Arc<::rayon::ThreadPool>, + #[cfg(feature = "parallel")] + thread_pool: ::std::sync::Arc<::rayon::ThreadPool>, } impl<'a, 'b> Dispatcher<'a, 'b> { @@ -29,10 +30,10 @@ impl<'a, 'b> Dispatcher<'a, 'b> { /// [`dispatch_par`]: struct.Dispatcher.html#method.dispatch_par /// [`dispatch_seq`]: struct.Dispatcher.html#method.dispatch_seq pub fn dispatch(&mut self, res: &Resources) { - #[cfg(not(target_os = "emscripten"))] + #[cfg(feature = "parallel")] self.dispatch_par(res); - #[cfg(target_os = "emscripten")] + #[cfg(not(feature = "parallel"))] self.dispatch_seq(res); self.dispatch_thread_local(res); @@ -44,12 +45,11 @@ impl<'a, 'b> Dispatcher<'a, 'b> { /// This operation blocks the /// executing thread. /// - /// Only available on platforms with - /// multithreading support (so not on emscripten). + /// Only available with "parallel" feature enabled. /// /// Please note that this method assumes that no resource /// is currently borrowed. If that's the case, it panics. - #[cfg(not(target_os = "emscripten"))] + #[cfg(feature = "parallel")] pub fn dispatch_par(&mut self, res: &Resources) { let stages = &mut self.stages; @@ -88,7 +88,7 @@ pub struct SystemId(pub usize); pub type SystemExecSend<'b> = Box RunNow<'a> + Send + 'b>; pub type ThreadLocal<'a> = SmallVec<[Box RunNow<'b> + 'a>; 4]>; -#[cfg(not(target_os = "emscripten"))] +#[cfg(feature = "parallel")] pub fn new_dispatcher<'a, 'b>( stages: Vec>, thread_local: ThreadLocal<'b>, @@ -101,7 +101,7 @@ pub fn new_dispatcher<'a, 'b>( } } -#[cfg(target_os = "emscripten")] +#[cfg(not(feature = "parallel"))] pub fn new_dispatcher<'a, 'b>( stages: Vec>, thread_local: ThreadLocal<'b>, @@ -185,6 +185,7 @@ mod tests { } #[test] + #[cfg(feature = "parallel")] fn stages_async() { let mut d = new_builder().build_async(new_resources()); diff --git a/src/dispatch/mod.rs b/src/dispatch/mod.rs index 5da0ec8..8ab654a 100644 --- a/src/dispatch/mod.rs +++ b/src/dispatch/mod.rs @@ -1,15 +1,15 @@ -#[cfg(not(target_os = "emscripten"))] +#[cfg(feature = "parallel")] pub use self::async::AsyncDispatcher; pub use self::builder::DispatcherBuilder; pub use self::dispatcher::Dispatcher; -#[cfg(not(target_os = "emscripten"))] +#[cfg(feature = "parallel")] pub use self::par_seq::{Par, ParSeq, Seq}; -#[cfg(not(target_os = "emscripten"))] +#[cfg(feature = "parallel")] mod async; mod builder; mod dispatcher; -#[cfg(not(target_os = "emscripten"))] +#[cfg(feature = "parallel")] mod par_seq; mod stage; mod util; diff --git a/src/dispatch/stage.rs b/src/dispatch/stage.rs index 77e883b..b0918dd 100644 --- a/src/dispatch/stage.rs +++ b/src/dispatch/stage.rs @@ -76,7 +76,7 @@ impl<'a> Stage<'a> { Default::default() } - #[cfg(not(target_os = "emscripten"))] + #[cfg(feature = "parallel")] pub fn execute(&mut self, res: &Resources) { use rayon::prelude::*; diff --git a/src/lib.rs b/src/lib.rs index 0b2cd71..9288b68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,9 +61,9 @@ extern crate arrayvec; extern crate fxhash; #[macro_use] extern crate mopa; -#[cfg(not(target_os = "emscripten"))] +#[cfg(feature = "parallel")] extern crate pulse; -#[cfg(not(target_os = "emscripten"))] +#[cfg(feature = "parallel")] extern crate rayon; extern crate smallvec; @@ -73,9 +73,9 @@ mod res; mod system; pub use dispatch::{Dispatcher, DispatcherBuilder}; -#[cfg(not(target_os = "emscripten"))] +#[cfg(feature = "parallel")] pub use dispatch::{Par, ParSeq, Seq}; -#[cfg(not(target_os = "emscripten"))] +#[cfg(feature = "parallel")] pub use dispatch::AsyncDispatcher; pub use res::{Fetch, FetchId, FetchIdMut, FetchMut, Resource, ResourceId, Resources}; pub use system::{RunNow, RunningTime, System, SystemData};