Giter VIP home page Giter VIP logo

Comments (5)

udoprog avatar udoprog commented on July 22, 2024 1

Is there any chance you can provide a working reproduction? A PR to this repo where you add it to examples would be very helpful.

from rune.

udoprog avatar udoprog commented on July 22, 2024

Here's a slightly less verbose version:

pub async fn function() {
    let [foo, bar]= future::join([foo(), bar()]).await;
    let data = { foo, bar };
    `[${data.foo}@${data.bar}]`
}

Playground (sans the warning),

Other than that, I'm pretty open to adding more functions for joining futures!

from rune.

Rycieos avatar Rycieos commented on July 22, 2024

I forgot to mention that I already tried that:

    let [foo, bar] = future::join([
        foo(),
        bar(),
    ]).await;

Error: cannot read, value is exclusively accessed (at inst 4)

Same error if first saving as a tuple and later expanding:

    let result = future::join([
        foo(),
        bar(),
    ]).await;

    let foo = result.0

Maybe it's my fault, since these functions return Rust &strs? Is there a different type I should be returning to be "async safe"?

from rune.

Rycieos avatar Rycieos commented on July 22, 2024

As resolved in the linked PR, clearly the more complicated version that I needed was caused by my mistake. So your short version does work, but I would like to see a shorter version still.

The problem with the let [foo, bar] = future::join([foo(), bar()]).await; form is two fold:

  1. The compiler issues a warning let binding might panic, which is only fixed by surrounding it in an if block (and then the variables are local to that block and can't be used outside of it).
  2. The definition of the variable and the function who's result will be assigned to it are separated. This makes keeping them in sync more work.

Hence my suggestion:

let data = future::join_to_object(#{
    foo: foo(),
    bar: bar(),
}).await;

It solves both problems: there can be no panic, as the keys of the object are assigned whatever the Future returns, and the variable/key definition is on the same line as the function call.

If you don't like the suggestion, feel free to close this issue. But I would be curious to how you would implement it. Here is my attempt:

use std::future;

pub fn join_to_object(futures) {
    let results = future::join(futures.values());
    let data = #{};

    for [key, value] in futures.keys().zip(results) {
        data[key] = value;
    }

    data
}

But that doesn't work, as the zip() Iterator function from Rust std doesn't appear to be exposed. And I get the same let binding might panic warning on the for loop, which I have no idea how to get rid of since it's an implicit let.

from rune.

udoprog avatar udoprog commented on July 22, 2024

Should probably calm down the let binding might panic lint, or put it behind a feature flag.

from rune.

Related Issues (20)

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.