Skip to content

Commit 0ebcc2d

Browse files
AdomasBekerasADOMAS BEKERAS (from Dev Box)Copilot
authored
feat(anyspawn): Make Spawner usable without features (#343)
To use Spawner in a library, the library should not need to enable any features, it can just take a Spawner. This PR removes the feature gates from the Spaner and JoinHandle types --------- Co-authored-by: ADOMAS BEKERAS (from Dev Box) <abekeras@microsoft.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: AdomasBekeras <20183755+AdomasBekeras@users.noreply.github.com>
1 parent 85ef2c7 commit 0ebcc2d

11 files changed

Lines changed: 65 additions & 69 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/anyspawn/Cargo.toml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ allowed_external_types = [
2828
all-features = true
2929

3030
[features]
31-
default = ["tokio", "custom"]
31+
default = []
3232
tokio = ["dep:tokio"]
33-
custom = ["dep:futures-channel"]
3433

3534
[dependencies]
36-
futures-channel = { workspace = true, features = ["alloc"], optional = true }
35+
futures-channel = { workspace = true, features = ["alloc"] }
3736
thread_aware = { workspace = true, features = ["derive"] }
3837
tokio = { workspace = true, features = ["rt"], optional = true }
3938

@@ -44,29 +43,40 @@ insta.workspace = true
4443
opentelemetry = { workspace = true, features = ["futures"] }
4544
smol = "2"
4645
static_assertions.workspace = true
47-
tick = { workspace = true, features = ["tokio"] }
4846
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "sync"] }
4947

48+
[[test]]
49+
name = "spawner"
50+
required-features = ["tokio"]
51+
52+
[[test]]
53+
name = "handle"
54+
required-features = ["tokio"]
55+
56+
[[test]]
57+
name = "builder"
58+
required-features = ["tokio"]
59+
5060
[[bench]]
5161
name = "spawner"
5262
harness = false
53-
required-features = ["tokio", "custom"]
63+
required-features = ["tokio"]
5464

5565
[[example]]
5666
name = "custom"
57-
required-features = ["tokio", "custom"]
67+
required-features = ["tokio"]
5868

5969
[[example]]
6070
name = "tokio"
6171
required-features = ["tokio"]
6272

6373
[[example]]
6474
name = "otel_context"
65-
required-features = ["tokio", "custom"]
75+
required-features = ["tokio"]
6676

6777
[[example]]
6878
name = "thread_aware"
69-
required-features = ["tokio", "custom"]
79+
required-features = ["tokio"]
7080

7181
[lints]
7282
workspace = true

crates/anyspawn/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,19 @@ details and examples.
5959

6060
## Features
6161

62-
* `tokio` (default): Enables the [`Spawner::new_tokio`][__link4] and
62+
* `tokio`: Enables the [`Spawner::new_tokio`][__link4] and
6363
[`Spawner::new_tokio_with_handle`][__link5] constructors
64-
* `custom`: Enables [`Spawner::new_custom`][__link6] and [`CustomSpawnerBuilder`][__link7]
6564

6665

6766
<hr/>
6867
<sub>
6968
This crate was developed as part of <a href="../..">The Oxidizer Project</a>. Browse this crate's <a href="https://github.com/microsoft/oxidizer/tree/main/crates/anyspawn">source code</a>.
7069
</sub>
7170

72-
[__cargo_doc2readme_dependencies_info]: ggGkYW0CYXSEGy4k8ldDFPOhG2VNeXtD5nnKG6EPY6OfW5wBG8g18NOFNdxpYXKEG9wiT4mviSlSG7bkeqiO1sYsG8AqKBp2b7sGG8q0eDukfWyeYWSCgmhhbnlzcGF3bmUwLjIuMIJsdGhyZWFkX2F3YXJlZTAuNi4y
71+
[__cargo_doc2readme_dependencies_info]: ggGmYW0CYXZlMC43LjJhdIQbLiTyV0MU86EbZU15e0PmecoboQ9jo59bnAEbyDXw04U13GlhYvRhcoQb2kzPFSWDwP0bFmeuSlBPmOYbJhoG25idR60bDe4xDajaP_lhZIKCaGFueXNwYXduZTAuMi4wgmx0aHJlYWRfYXdhcmVlMC42LjI
7372
[__link0]: https://docs.rs/anyspawn/0.2.0/anyspawn/?search=Spawner
7473
[__link1]: https://docs.rs/thread_aware/0.6.2/thread_aware/?search=ThreadAware
7574
[__link2]: https://docs.rs/anyspawn/0.2.0/anyspawn/?search=Spawner::new_thread_aware
7675
[__link3]: Spawner#thread-aware-support
7776
[__link4]: https://docs.rs/anyspawn/0.2.0/anyspawn/?search=Spawner::new_tokio
7877
[__link5]: https://docs.rs/anyspawn/0.2.0/anyspawn/?search=Spawner::new_tokio_with_handle
79-
[__link6]: https://docs.rs/anyspawn/0.2.0/anyspawn/?search=Spawner::new_custom
80-
[__link7]: https://docs.rs/anyspawn/0.2.0/anyspawn/?search=CustomSpawnerBuilder

crates/anyspawn/src/builder.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ use crate::custom::BoxedFuture;
2828
/// # Examples
2929
///
3030
/// ```rust
31-
/// use anyspawn::{BoxedFuture, CustomSpawnerBuilder};
32-
///
31+
/// # #[cfg(feature = "tokio")]
3332
/// # #[tokio::main]
3433
/// # async fn main() {
34+
/// use anyspawn::{BoxedFuture, CustomSpawnerBuilder};
35+
///
3536
/// let spawner = CustomSpawnerBuilder::tokio()
3637
/// .layer(|fut: BoxedFuture, spawn: &dyn Fn(BoxedFuture)| {
3738
/// spawn(Box::pin(async move {
@@ -45,6 +46,8 @@ use crate::custom::BoxedFuture;
4546
/// let result = spawner.spawn(async { 42 }).await;
4647
/// assert_eq!(result, 42);
4748
/// # }
49+
/// # #[cfg(not(feature = "tokio"))]
50+
/// # fn main() {}
4851
/// ```
4952
pub struct CustomSpawnerBuilder<S> {
5053
spawn_fn: S,
@@ -73,7 +76,7 @@ impl CustomSpawnerBuilder<()> {
7376
/// # }
7477
/// ```
7578
#[cfg(feature = "tokio")]
76-
#[cfg_attr(docsrs, doc(cfg(all(feature = "tokio", feature = "custom"))))]
79+
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
7780
#[must_use]
7881
pub fn tokio() -> CustomSpawnerBuilder<impl Fn(BoxedFuture) + Send + Sync + 'static> {
7982
CustomSpawnerBuilder {
@@ -107,7 +110,7 @@ impl CustomSpawnerBuilder<()> {
107110
/// # }
108111
/// ```
109112
#[cfg(feature = "tokio")]
110-
#[cfg_attr(docsrs, doc(cfg(all(feature = "tokio", feature = "custom"))))]
113+
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
111114
#[must_use]
112115
pub fn tokio_with_handle(handle: ::tokio::runtime::Handle) -> CustomSpawnerBuilder<impl Fn(BoxedFuture) + Send + Sync + 'static> {
113116
CustomSpawnerBuilder {
@@ -184,10 +187,11 @@ where
184187
/// # Examples
185188
///
186189
/// ```rust
187-
/// use anyspawn::{BoxedFuture, CustomSpawnerBuilder};
188-
///
190+
/// # #[cfg(feature = "tokio")]
189191
/// # #[tokio::main]
190192
/// # async fn main() {
193+
/// use anyspawn::{BoxedFuture, CustomSpawnerBuilder};
194+
///
191195
/// let spawner = CustomSpawnerBuilder::tokio()
192196
/// .layer(|fut: BoxedFuture, spawn: &dyn Fn(BoxedFuture)| {
193197
/// spawn(Box::pin(async move {
@@ -198,6 +202,8 @@ where
198202
/// .build();
199203
/// # let _ = spawner;
200204
/// # }
205+
/// # #[cfg(not(feature = "tokio"))]
206+
/// # fn main() {}
201207
/// ```
202208
pub fn layer<L>(self, layer_fn: L) -> CustomSpawnerBuilder<impl Fn(BoxedFuture) + Send + Sync + 'static>
203209
where

crates/anyspawn/src/handle.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::fmt::Debug;
77
use std::pin::Pin;
88
use std::task::{Context, Poll};
99

10-
#[cfg(feature = "custom")]
1110
use futures_channel::oneshot;
1211

1312
/// A handle to a spawned task that can be awaited to retrieve its result.
@@ -23,7 +22,6 @@ pub struct JoinHandle<T>(pub(crate) JoinHandleInner<T>);
2322
pub(crate) enum JoinHandleInner<T> {
2423
#[cfg(feature = "tokio")]
2524
Tokio(::tokio::task::JoinHandle<T>),
26-
#[cfg(feature = "custom")]
2725
Custom(oneshot::Receiver<T>),
2826
}
2927

@@ -34,7 +32,6 @@ impl<T> Future for JoinHandle<T> {
3432
match &mut self.get_mut().0 {
3533
#[cfg(feature = "tokio")]
3634
JoinHandleInner::Tokio(jh) => Pin::new(jh).poll(cx).map(|res| res.expect("spawned task panicked")),
37-
#[cfg(feature = "custom")]
3835
JoinHandleInner::Custom(rx) => Pin::new(rx).poll(cx).map(|res| res.expect("spawned task panicked")),
3936
}
4037
}

crates/anyspawn/src/lib.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@
2121
//! ## Using Tokio
2222
//!
2323
//! ```rust
24-
//! use anyspawn::Spawner;
25-
//!
24+
//! # #[cfg(feature = "tokio")]
2625
//! # #[tokio::main]
2726
//! # async fn main() {
27+
//! use anyspawn::Spawner;
28+
//!
2829
//! let spawner = Spawner::new_tokio();
2930
//! let result = spawner.spawn(async { 1 + 1 }).await;
3031
//! assert_eq!(result, 2);
3132
//! # }
33+
//! # #[cfg(not(feature = "tokio"))]
34+
//! # fn main() {}
3235
//! ```
3336
//!
3437
//! ## Custom Runtime
3538
//!
36-
//! ```rust,ignore
39+
//! ```rust
3740
//! use anyspawn::Spawner;
3841
//!
3942
//! let spawner = Spawner::new_custom("threadpool", |fut| {
@@ -54,27 +57,18 @@
5457
//!
5558
//! # Features
5659
//!
57-
//! - `tokio` (default): Enables the [`Spawner::new_tokio`] and
60+
//! - `tokio`: Enables the [`Spawner::new_tokio`] and
5861
//! [`Spawner::new_tokio_with_handle`] constructors
59-
//! - `custom`: Enables [`Spawner::new_custom`] and [`CustomSpawnerBuilder`]
6062
6163
#![doc(html_logo_url = "https://media.githubusercontent.com/media/microsoft/oxidizer/refs/heads/main/crates/anyspawn/logo.png")]
6264
#![doc(html_favicon_url = "https://media.githubusercontent.com/media/microsoft/oxidizer/refs/heads/main/crates/anyspawn/favicon.ico")]
6365

64-
#[cfg(feature = "custom")]
6566
mod builder;
66-
#[cfg(feature = "custom")]
6767
mod custom;
68-
#[cfg(any(feature = "tokio", feature = "custom"))]
6968
mod handle;
70-
#[cfg(any(feature = "tokio", feature = "custom"))]
7169
mod spawner;
7270

73-
#[cfg(feature = "custom")]
7471
pub use builder::CustomSpawnerBuilder;
75-
#[cfg(feature = "custom")]
7672
pub use custom::BoxedFuture;
77-
#[cfg(any(feature = "tokio", feature = "custom"))]
7873
pub use handle::JoinHandle;
79-
#[cfg(any(feature = "tokio", feature = "custom"))]
8074
pub use spawner::Spawner;

0 commit comments

Comments
 (0)