Skip to content

Commit 3f38b2d

Browse files
authored
Add no default feature flag panic-multiple-global-spawners (#1)
Global executors should be capable to spawn everywhere and work with all runtime agnostic io, net, timer, channel and etc. crates. So there is no reason to panic on default if there are multiple global spawners. But still a feature flag for binary crate to decide. Currently, spawns choose an random on from `linkme`. We could introduce named global spawner to choose from env variable.
1 parent 0ecefb1 commit 3f38b2d

File tree

7 files changed

+15
-15
lines changed

7 files changed

+15
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ The API is capable to spawn, join and cancel tasks as what `tokio`, `smol` and `
8484

8585
## Packages
8686
1. [spawns-core][] provides `Spawn` and `enter()` for async runtimes to setup thread context task spawner.
87-
2. [spawns-compat][] provides compatibility for `tokio`, `smol` and `async-global-executor`(which is used by `async-std`) through feature gates. Be ware of that, `smol` and `async-global-executor` can't coexist as they don't have `tokio::runtime::Handle::try_current()` like method to detect thread context aware executor. `spawn()` will panic if can't find any thread context spawners but multiple blinding global spawners.
87+
2. [spawns-compat][] provides compatibility for `tokio`, `smol` and `async-global-executor`(which is used by `async-std`) through feature gates.
8888
3. [spawns-executor][] provides full functional `block_on` with both current thread executor and multi-thread executor.
8989
4. [spawns][] exports all above packages including feature gates `tokio`, `smol` and `async-global-executor`. In addition, it provides feature gate `executor` to include `spawns-executor`.
9090

spawns-compat/src/lib.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,3 @@ mod tokio;
2222

2323
#[cfg(feature = "async-global-executor")]
2424
mod async_global_executor;
25-
26-
#[cfg(test)]
27-
mod tests {
28-
#[test]
29-
#[cfg(all(feature = "async-global-executor", feature = "smol"))]
30-
#[should_panic(expected = "multiple global spawners")]
31-
fn multiple_global() {
32-
spawns_core::spawn(async move {});
33-
}
34-
}

spawns-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ description = "Async runtime agnostic thread context task spawner for Rust"
1212
[features]
1313
default = []
1414
compat = ["linkme"]
15+
panic-multiple-global-spawners = []
1516
test-compat-global1 = ["compat"]
1617
test-compat-global2 = ["compat", "test-compat-global1"]
1718

spawns-core/src/compat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ pub(crate) fn find_spawn() -> Option<fn(Task)> {
3838
}) {
3939
Some(spawn) => Some(spawn),
4040
None => {
41+
#[cfg(feature = "panic-multiple-global-spawners")]
4142
if globals > 1 {
4243
panic!("multiple global spawners")
43-
} else {
44-
last_global.copied()
4544
}
45+
last_global.copied()
4646
}
4747
}
4848
}

spawns-core/src/spawn.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,18 @@ mod tests {
255255
}
256256

257257
#[cfg(feature = "test-compat-global2")]
258+
#[cfg(feature = "panic-multiple-global-spawners")]
258259
#[test]
259260
#[should_panic(expected = "multiple global spawners")]
260261
fn multiple_globals() {
261262
spawn(ready(()));
262263
}
264+
265+
#[cfg(feature = "test-compat-global2")]
266+
#[cfg(not(feature = "panic-multiple-global-spawners"))]
267+
#[test]
268+
fn multiple_globals() {
269+
block_on(spawn(ready(()))).unwrap();
270+
}
263271
}
264272
}

spawns/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ executor = ["spawns-executor"]
1515
tokio = ["spawns-compat/tokio"]
1616
smol = ["spawns-compat/smol"]
1717
async-global-executor = ["spawns-compat/async-global-executor"]
18+
panic-multiple-global-spawners = ["spawns-core/panic-multiple-global-spawners"]
1819

1920
[dependencies]
2021
spawns-core = { path = "../spawns-core", version = "1.0.2" }

spawns/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
//! * `smol`: uses `smol::spawn` to spawn task in absent of thread local spawners.
4242
//! * `async-global-executor`: uses `async_global_executor::spawn` to spawn task in absent of thread local spawners.
4343
//!
44-
//! Be aware that, `smol` and `async-global-executor` are not compatible as they both blindly spawn
45-
//! tasks. [spawn()] will panic if there is no thread local spawners but multiple global spawners.
44+
//! Since `smol` and `async-global-executor` both blindly spawn tasks, it is unknown which one is
45+
//! chosen. Feature "panic-multiple-global-spawners" is provided to panic on this situation.
4646
4747
pub use spawns_core::*;
4848

0 commit comments

Comments
 (0)