Comments (2)
Another workaround: if channel is cloned before and then passed by value, it compiles fine.
So this code compiles:
let tx2 = tx.clone();
tst_nested(tx2).await;
While this one doesn't:
tst_nested(tx.clone()).await;
Looks like a compiler bug now (?) I checked on beta and last nightly.
from ring-channel.
Thanks for taking the time to report this.
TL;DR;
This is neither an issue with the compiler, nor with ring_channel.
Why?
To understand why, consider the reduced snippet below, which reproduces the same error you see on 1.40.0-nightly (3a9abe3f0 2019-10-15)
.
async fn second(_: RingSender<u32>) {}
async fn first(tx: RingSender<u32>) {
second(tx.clone()).await;
}
let mut pool = ThreadPool::new().unwrap();
let (tx, _) = ring_channel(std::num::NonZeroUsize::new(1).unwrap());
pool.spawn(first(tx)).unwrap();
Let us rewrite the line second(tx.clone()).await;
the way the compiler sees it:
async {
let tx2 = tx.clone();
second(tx2).await
}.await
Notice that tx
is captured by reference, since its only use within the async
block binds it to a shared reference in the call to clone
. Recall that the async
block is scheduled for execution in the ThreadPool
and the corresponding task can be picked up by any thread, so &tx
is required to implement Send
, or equivalently, tx
is required to implement Sync
, which it does not, hence the compilation error.
Why does removing the call to clone
avoid the error?
This is what the compiler sees in this case:
async {
second(tx).await
}.await
Notice that tx
is captured by value, since its only use within the async
block binds it to a move
into second
. Following the same reasoning around the fact the async
block can be executed on any thread, we conclude that tx
is only required to implement Send
, which it does, so the borrow checker is happy.
Why does cloning tx
upfront work?
Once again, let's rewrite it the way the compiler sees it:
let tx2 = tx.clone();
async {
second(tx2).await
}.await
Following the same logic, tx2
is captured by value, which we have just verified to be fine.
Why does crossbeam_channel work when captured by reference?
Simply because crossbeam_channel::Sender
does implement Sync
.
Why doesn't ring_channel::Sender
implement Sync?
The way it's currently implemented does allow it to implement Sync, but I decided to reserve the right to change the implementation in the future in a way that breaks this property, considering it can just be cloned instead .
from ring-channel.
Related Issues (10)
- Asyncronous? HOT 3
- Would it be possible to relax `RingSender::send`'s `&mut self` to `&self`? HOT 3
- I think there's an error in impl<T> Stream for RingReceiver<T> HOT 23
- Replace futures-preview with futures to 0.3.1 HOT 1
- Cargo audit warning: spin is no longer actively maintained HOT 2
- Can't use `ring channel` in async context: `NonNull<ControlBlock> is not `Send` HOT 8
- Can't get consistent behavior HOT 2
- RUSTSEC-2020-0095: difference is unmaintained HOT 2
- RUSTSEC-2021-0127: serde_cbor is unmaintained HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ring-channel.