Comment by tcfhgj

Comment by tcfhgj a day ago

3 replies

> If you are using a library in rust, it has to be async await, tokio, send+sync and all the other crap

Send and sync is only required if you want to access something from multiple threads, which isn't required by async await (parallelism vs concurrency)

1) You can use async await without parallelism and 2) send and sync aren't a product of async/await in Rust, but generally memory safety, i.e. you need Send generally when something can/is allowed to move between threads.

m11a a day ago

Yes, but async Rust is basically built on tokio's runtime, which is what most the big async libraries depend on, like hyper/axum/tokio etc. And tokio is a thread-per-core work-stealing architecture, which requires Send + Sync bounds everywhere. You can avoid them if you depend on tokio-proper, but it's more icky when building on something like axum, where your application handlers also require these bounds.

A good article on this: https://emschwartz.me/async-rust-can-be-a-pleasure-to-work-w...

  • tcfhgj a day ago

    Iirc I had a situation a while back, in which I used async await with tokio with a non Send or Sync type and it compiled when I didn't use spawn[1] (implying multithreading) but a simple loop with sequential processing.

    Only when I wanted to enable parallelism using spawn, I got a compilation error.

    [1]: https://docs.rs/tokio/latest/tokio/task/fn.spawn.html

WhyNotHugo a day ago

> Send and sync is only required if you want to access something from multiple threads, which isn't required by async await (parallelism vs concurrency)

In theory this is correct. In practice, a lot of APIs (including many in tokio) require both traits even for single-thread use cases.