async-rs / async-rs/async-std

Make async spawn take a closure

Open
#1,072 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
4.1k
Forks
339
PR merge metrics
No merged PRs in 30d

Description

Hello,

I'm posting this idea to many Rust async runtimes discussion threads in order to push for the alternative design of the task spawn API described by @matklad in https://matklad.github.io/2023/12/10/nsfw.html

```rust
// std::thread::spawn
pub fn spawn(f: F) -> JoinHandle
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static {}

// A hypothetical better async spawn
pub fn spawn(f: F) -> JoinHandle
where
F: FnOnce() -> Fut + Send + 'static,
Fut: Future,
Fut::Output: Send + 'static {}
```

If you think that this is the right direction for rust to take, I suggest to do the following to arrive at the destination ASAP:

1. change task spawn definition to one that takes a closure returning a future
2. Provide a safe executor constructor that pins tasks to threads
3. Make workstealing executors unsafe to construct... until the language developers "fix" this issue of entangling of Send with OS threads

What do you think?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.