Comments (5)
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.
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.
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 &str
s? Is there a different type I should be returning to be "async safe"?
from rune.
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:
- The compiler issues a warning
let binding might panic
, which is only fixed by surrounding it in anif
block (and then the variables are local to that block and can't be used outside of it). - 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.
Should probably calm down the let binding might panic lint, or put it behind a feature flag.
from rune.
Related Issues (20)
- Support quote! iterator `#(iter)*` without joining
- Support patterns in quote! iterators
- Support `derive(Any)` type protocols HOT 1
- Cannot mutate nested field HOT 2
- Using a dedicated enum for the `Try` protocol HOT 2
- "halted for unexpected reason `awaited`" Error HOT 7
- Should `#[rune::function]` be associated if function returns `Self` HOT 1
- Add default `T=()` to `rune::Result` HOT 1
- Modify a vm function
- Feature request: Traits HOT 3
- Support pattern matching which is open `..` in the middle of an array HOT 1
- Memory sandboxing
- Feature request: Support autocomplete for Ace editor
- Function Pointers HOT 2
- 0.13.0 release build failure HOT 7
- Documentation enhancements HOT 1
- Bad link in rune-modules's README
- Support should_panic for rune tests HOT 3
- Error handling in built-in toml module HOT 3
- Feature request: Support variable swapping 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 rune.