Skip to content
Open
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: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ missing_debug_implementations = "warn"
# dependencies require a feature enabled when using `--cfg docsrs` which
# we can not do. To enable for a crate set
# `#![cfg_attr(n0_future_docsrs, feature(doc_auto_cfg))]` in the crate.
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(n0_future_docsrs)"] }
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(n0_future_docsrs)", "cfg(tokio_unstable)"] }

[lints.clippy]
unused-async = "warn"

[features]
serde = ["web-time/serde"]
tracing = ["tokio/tracing"]
51 changes: 51 additions & 0 deletions src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,36 @@

#[cfg(not(wasm_browser))]
pub use tokio::spawn;

/// Spawns a future as a task.
///
/// If possible uses `name` to name the task
#[cfg(all(not(wasm_browser), not(tokio_unstable)))]
#[track_caller]
pub fn spawn_with_name<F>(_name: &str, future: F) -> JoinHandle<F::Output>
where
F: std::future::Future + Send + 'static,
F::Output: Send + 'static,
{
spawn(future)
}

/// Spawns a future as a task.
///
/// If possible uses `name` to name the task
#[cfg(all(not(wasm_browser), tokio_unstable, feature = "tracing"))]
#[track_caller]
pub fn spawn_with_name<F>(name: &str, future: F) -> JoinHandle<F::Output>
where
F: std::future::Future + Send + 'static,
F::Output: Send + 'static,
{
tokio::task::Builder::new()
.name(name)
.spawn(future)
.expect("doesn't fails")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.expect("doesn't fails")
.expect("doesn't fail")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe nice if it says why it doesn't fail.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because I looked at the implementation 🤷 not sure what else to say

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what the...

Okay maybe sth like "impl doesn't fail".

}

#[cfg(not(wasm_browser))]
pub use tokio::task::{AbortHandle, Id, JoinError, JoinHandle, JoinSet};
#[cfg(not(wasm_browser))]
Expand Down Expand Up @@ -548,6 +578,19 @@ mod wasm {

handle
}

/// Spawns a future as a task in the browser runtime.
///
/// If possible uses `name` to name the task
pub fn spawn_with_name<T: 'static>(
_name: &str,
fut: impl IntoFuture<Output = T> + 'static,
) -> JoinHandle<T>
where
T: 'static,
{
spawn(fut)
}
}

#[cfg(test)]
Expand All @@ -561,6 +604,14 @@ mod test {

use crate::task;

#[test]
async fn task_with_name() {
let h1 = task::spawn_with_name("test", async {
crate::time::sleep(Duration::from_millis(10)).await;
});
h1.await.unwrap();
}

#[test]
async fn task_abort() {
let h1 = task::spawn(async {
Expand Down
Loading