Git Product home page Git Product logo

Comments (2)

mcseemk avatar mcseemk commented on June 26, 2024

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.

brunocodutra avatar brunocodutra commented on June 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.