Questions tagged [rust-futures]
69 questions
19
votes
2 answers
Rust Async Drop
I'm facing a scenario where I need to run async code from the drop handler of an object. The whole application runs in a tokio async context, so I know that the drop handler is called with an active tokio Runtime, but unfortunately drop itself is a…

Heinzi
- 5,793
- 4
- 40
- 69
11
votes
1 answer
Function taking an async closure that takes a reference and captures by reference
I want to do something like this:
// NOTE: This doesn't compile
struct A { v: u32 }
async fn foo<
C: for<'a> FnOnce(&'a A) -> Pin + 'a>>
>(c: C) {
c(&A {
v: 8,
}).await
}
#[tokio::main]
async fn…

AlastairHolmes
- 427
- 2
- 10
9
votes
1 answer
How to use async/await inside closure of `Option::and_then` or `Option::map` without using OptionFuture?
I would like to run something like the following code:
async fn get_user(s: &str) -> Option { /* ... */ }
let user_id = Some("sessiontoken").and_then(|session_token| {
get_user(session_token)
.await // <- error
.map(|user|…

Filippo Orrù
- 195
- 3
- 8
6
votes
1 answer
impl Stream cannot be unpinned
I'm trying to get data using crates_io_api.
I attempted to get data from a stream, but
I can not get it to work.
AsyncClient::all_crates returns an impl Stream. How do I get data from it? It would be helpful if you provide code.
I checked out the…

Pytan
- 1,138
- 11
- 28
4
votes
1 answer
"one type is more general than the other" error in Rust while types are identical
I have the following code
use std::future::Future;
fn main() {
handle(Test::my_func);
}
fn handle(fun: for<'r> fn(&'r mut Test) -> Fut) -> bool
where
Fut: Future

gurghet
- 7,591
- 4
- 36
- 63
3
votes
1 answer
Ready or pending future depending on a condition
I need to save a ready or pending future in a variable depending on a condition.
Would be nice if I could do this:
let f = futures::future::ready(true);
But the API provides two different functions, which have different return types, so, this does…

nicolai
- 1,140
- 9
- 17
3
votes
1 answer
Limiting the number of concurrent futures in join_all!()
How to make Rust execute all given futures (like join_all!) limiting to execute say 10 futures at once?
I need to download files from a big number of servers, but query no more than 10 servers simultaneously (to accurately measure their timeouts: if…

porton
- 5,214
- 11
- 47
- 95
3
votes
2 answers
How can I `fut.await` to run a future in the "background" (execute it but not wait for it)?
I want to able to start a future running in the background, and not wait for it immediately in the parent function scope.
Something like a dynamic join_all where I can add new futures in a loop to a set, and then pass the set to another function…

zino
- 1,222
- 2
- 17
- 47
3
votes
1 answer
Hanging promise canceled
I'm trying to set Cloudflare's workers to track the circulation of some ERC20 tokens as an exercise to learn web3 and wasm. Thought it could be simple enough, but about 90% of the time so far has been trying to solve this elusive error
A hanging…

Corfucinas
- 353
- 2
- 11
3
votes
1 answer
How do I transpose a Future out of an Option?
Is there a more idiomatic or prettier way to do an operation like this in rust?
let maybe_output = match maybe_input {
Some(input) => Some(async_result(input).await?),
None => None,
};
I tried to use map like this,
let maybe_output =…

cheezsteak
- 2,731
- 4
- 26
- 41
2
votes
1 answer
How can I wait for a specific result from a pool of futures with async rust?
Using the futures crate.
I have a vec of futures which return a bool and I want to wait specifically for the future that returns true.
consider the following pool of futures.
async fn async_function(guess: u8) -> bool {
let random_wait =…

Daniel Garcia
- 462
- 3
- 8
2
votes
1 answer
Make returned Future Send if parameters are Send
Can I propagate the Send trait of function parameters to its return type, so that the return type is impl Send if and only if the parameters are?
Details:
An async function has a nice feature. Its returned Future is automatically Send if it can be.…

Heinzi
- 5,793
- 4
- 40
- 69
2
votes
1 answer
Call stored closure from an async closure in rust
I have a closure that mutates variables designed outside of it. How would I go about calling this closure that modifies the state from inside an async scope?
I have the following code (abstracted, to show the issue):
#[tokio::main]
async fn main()…

SnailsSeveral
- 23
- 3
2
votes
2 answers
How to collect multiple results from concurrently working for loop?
I would like to run a function with lots of different variables
Let assume my function is :
async fn do_the_hard_job(my_input: u16) {
...
// do the hard job that takes some time
if my_condition {
println!("{}", input);
…

Kürşat Kobya
- 111
- 11
2
votes
2 answers
try_join to make mongodb transactions sent at the same time
I'm new to Rust and I'm using the default MongoDB driver
https://docs.rs/mongodb/2.0.0/mongodb/
I remember when coding with Node.js, there was a possibility to send transactions with some Promise.all() in order to execute all transactions at the…

Max Mishkoy
- 61
- 6