Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 18 additions & 8 deletions crates/anyspawn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ allowed_external_types = [
all-features = true

[features]
default = ["tokio", "custom"]
default = []
tokio = ["dep:tokio"]
custom = ["dep:futures-channel"]

[dependencies]
futures-channel = { workspace = true, features = ["alloc"], optional = true }
futures-channel = { workspace = true, features = ["alloc"] }
thread_aware = { workspace = true, features = ["derive"] }
tokio = { workspace = true, features = ["rt"], optional = true }

Expand All @@ -44,29 +43,40 @@ insta.workspace = true
opentelemetry = { workspace = true, features = ["futures"] }
smol = "2"
static_assertions.workspace = true
tick = { workspace = true, features = ["tokio"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "sync"] }

[[test]]
name = "spawner"
required-features = ["tokio"]

[[test]]
name = "handle"
required-features = ["tokio"]

[[test]]
name = "builder"
required-features = ["tokio"]

[[bench]]
name = "spawner"
harness = false
required-features = ["tokio", "custom"]
required-features = ["tokio"]

[[example]]
name = "custom"
required-features = ["tokio", "custom"]
required-features = ["tokio"]

[[example]]
name = "tokio"
required-features = ["tokio"]

[[example]]
name = "otel_context"
required-features = ["tokio", "custom"]
required-features = ["tokio"]

[[example]]
name = "thread_aware"
required-features = ["tokio", "custom"]
required-features = ["tokio"]

[lints]
workspace = true
7 changes: 2 additions & 5 deletions crates/anyspawn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,19 @@ details and examples.

## Features

* `tokio` (default): Enables the [`Spawner::new_tokio`][__link4] and
* `tokio`: Enables the [`Spawner::new_tokio`][__link4] and
[`Spawner::new_tokio_with_handle`][__link5] constructors
* `custom`: Enables [`Spawner::new_custom`][__link6] and [`CustomSpawnerBuilder`][__link7]


<hr/>
<sub>
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>.
</sub>

[__cargo_doc2readme_dependencies_info]: ggGkYW0CYXSEGy4k8ldDFPOhG2VNeXtD5nnKG6EPY6OfW5wBG8g18NOFNdxpYXKEG9wiT4mviSlSG7bkeqiO1sYsG8AqKBp2b7sGG8q0eDukfWyeYWSCgmhhbnlzcGF3bmUwLjIuMIJsdGhyZWFkX2F3YXJlZTAuNi4y
[__cargo_doc2readme_dependencies_info]: ggGmYW0CYXZlMC43LjJhdIQbLiTyV0MU86EbZU15e0PmecoboQ9jo59bnAEbyDXw04U13GlhYvRhcoQb2kzPFSWDwP0bFmeuSlBPmOYbJhoG25idR60bDe4xDajaP_lhZIKCaGFueXNwYXduZTAuMi4wgmx0aHJlYWRfYXdhcmVlMC42LjI
[__link0]: https://docs.rs/anyspawn/0.2.0/anyspawn/?search=Spawner
[__link1]: https://docs.rs/thread_aware/0.6.2/thread_aware/?search=ThreadAware
[__link2]: https://docs.rs/anyspawn/0.2.0/anyspawn/?search=Spawner::new_thread_aware
[__link3]: Spawner#thread-aware-support
[__link4]: https://docs.rs/anyspawn/0.2.0/anyspawn/?search=Spawner::new_tokio
[__link5]: https://docs.rs/anyspawn/0.2.0/anyspawn/?search=Spawner::new_tokio_with_handle
[__link6]: https://docs.rs/anyspawn/0.2.0/anyspawn/?search=Spawner::new_custom
[__link7]: https://docs.rs/anyspawn/0.2.0/anyspawn/?search=CustomSpawnerBuilder
18 changes: 12 additions & 6 deletions crates/anyspawn/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ use crate::custom::BoxedFuture;
/// # Examples
///
/// ```rust
/// use anyspawn::{BoxedFuture, CustomSpawnerBuilder};
///
/// # #[cfg(feature = "tokio")]
/// # #[tokio::main]
/// # async fn main() {
/// use anyspawn::{BoxedFuture, CustomSpawnerBuilder};
///
/// let spawner = CustomSpawnerBuilder::tokio()
/// .layer(|fut: BoxedFuture, spawn: &dyn Fn(BoxedFuture)| {
/// spawn(Box::pin(async move {
Expand All @@ -45,6 +46,8 @@ use crate::custom::BoxedFuture;
/// let result = spawner.spawn(async { 42 }).await;
/// assert_eq!(result, 42);
/// # }
/// # #[cfg(not(feature = "tokio"))]
/// # fn main() {}
/// ```
pub struct CustomSpawnerBuilder<S> {
spawn_fn: S,
Expand Down Expand Up @@ -73,7 +76,7 @@ impl CustomSpawnerBuilder<()> {
/// # }
/// ```
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "tokio", feature = "custom"))))]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
#[must_use]
pub fn tokio() -> CustomSpawnerBuilder<impl Fn(BoxedFuture) + Send + Sync + 'static> {
CustomSpawnerBuilder {
Expand Down Expand Up @@ -107,7 +110,7 @@ impl CustomSpawnerBuilder<()> {
/// # }
/// ```
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "tokio", feature = "custom"))))]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
#[must_use]
pub fn tokio_with_handle(handle: ::tokio::runtime::Handle) -> CustomSpawnerBuilder<impl Fn(BoxedFuture) + Send + Sync + 'static> {
CustomSpawnerBuilder {
Expand Down Expand Up @@ -184,10 +187,11 @@ where
/// # Examples
///
/// ```rust
/// use anyspawn::{BoxedFuture, CustomSpawnerBuilder};
///
/// # #[cfg(feature = "tokio")]
/// # #[tokio::main]
/// # async fn main() {
/// use anyspawn::{BoxedFuture, CustomSpawnerBuilder};
///
/// let spawner = CustomSpawnerBuilder::tokio()
/// .layer(|fut: BoxedFuture, spawn: &dyn Fn(BoxedFuture)| {
/// spawn(Box::pin(async move {
Expand All @@ -198,6 +202,8 @@ where
/// .build();
/// # let _ = spawner;
/// # }
/// # #[cfg(not(feature = "tokio"))]
/// # fn main() {}
/// ```
pub fn layer<L>(self, layer_fn: L) -> CustomSpawnerBuilder<impl Fn(BoxedFuture) + Send + Sync + 'static>
where
Expand Down
3 changes: 0 additions & 3 deletions crates/anyspawn/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::fmt::Debug;
use std::pin::Pin;
use std::task::{Context, Poll};

#[cfg(feature = "custom")]
use futures_channel::oneshot;

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

Expand All @@ -34,7 +32,6 @@ impl<T> Future for JoinHandle<T> {
match &mut self.get_mut().0 {
#[cfg(feature = "tokio")]
JoinHandleInner::Tokio(jh) => Pin::new(jh).poll(cx).map(|res| res.expect("spawned task panicked")),
#[cfg(feature = "custom")]
JoinHandleInner::Custom(rx) => Pin::new(rx).poll(cx).map(|res| res.expect("spawned task panicked")),
}
}
Expand Down
20 changes: 7 additions & 13 deletions crates/anyspawn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@
//! ## Using Tokio
//!
//! ```rust
//! use anyspawn::Spawner;
//!
//! # #[cfg(feature = "tokio")]
//! # #[tokio::main]
//! # async fn main() {
//! use anyspawn::Spawner;
//!
//! let spawner = Spawner::new_tokio();
//! let result = spawner.spawn(async { 1 + 1 }).await;
//! assert_eq!(result, 2);
//! # }
//! # #[cfg(not(feature = "tokio"))]
//! # fn main() {}
//! ```
//!
//! ## Custom Runtime
//!
//! ```rust,ignore
//! ```rust
//! use anyspawn::Spawner;
//!
//! let spawner = Spawner::new_custom("threadpool", |fut| {
Expand All @@ -54,27 +57,18 @@
//!
//! # Features
//!
//! - `tokio` (default): Enables the [`Spawner::new_tokio`] and
//! - `tokio`: Enables the [`Spawner::new_tokio`] and
//! [`Spawner::new_tokio_with_handle`] constructors
//! - `custom`: Enables [`Spawner::new_custom`] and [`CustomSpawnerBuilder`]

#![doc(html_logo_url = "https://media.githubusercontent.com/media/microsoft/oxidizer/refs/heads/main/crates/anyspawn/logo.png")]
#![doc(html_favicon_url = "https://media.githubusercontent.com/media/microsoft/oxidizer/refs/heads/main/crates/anyspawn/favicon.ico")]

#[cfg(feature = "custom")]
mod builder;
#[cfg(feature = "custom")]
mod custom;
#[cfg(any(feature = "tokio", feature = "custom"))]
mod handle;
#[cfg(any(feature = "tokio", feature = "custom"))]
mod spawner;

#[cfg(feature = "custom")]
pub use builder::CustomSpawnerBuilder;
#[cfg(feature = "custom")]
pub use custom::BoxedFuture;
#[cfg(any(feature = "tokio", feature = "custom"))]
pub use handle::JoinHandle;
#[cfg(any(feature = "tokio", feature = "custom"))]
pub use spawner::Spawner;
Loading
Loading