Questions tagged [rust-async-std]

Async version of the Rust standard library.

async-std is a foundation of portable Rust software, a set of minimal and battle-tested shared abstractions for the broader Rust ecosystem. It offers std types, like Future and Stream, library-defined operations on language primitives, standard macros, I/O and multithreading, among many other things.

async-std is available from crates.io. Once included, async-std can be accessed in use statements through the path async_std, as in use async_std::future.

48 questions
5
votes
3 answers

Why do asynchronous versions of a TCP echo server use 50x more memory than a synchronous one?

I have a simple TCP echo server using standard library: use std::net::TcpListener; fn main() { let listener = TcpListener::bind("localhost:4321").unwrap(); loop { let (conn, _addr) = listener.accept().unwrap(); …
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
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, { true } struct Test {} impl Test { pub async fn…
gurghet
  • 7,591
  • 4
  • 36
  • 63
4
votes
1 answer

If I have nested async/await calls should I be concerned with accumulated overhead?

I'm processing data via a number of channels where each channel feeds into the next (pipeline processing). I end up with a spawn at the top that looks like this: let future = async move { while let Ok(msg) = r.recv().await { …
Bruce
  • 503
  • 3
  • 13
4
votes
0 answers

How to create a Vec of futures

I have this function: async fn get_events(r: RequestBuilder) -> Result, reqwest::Error> { Ok(r.send().await?.json::>().await?) } I want to store a Vec of futures and await them all: let mut events = vec![]; for i…
zino
  • 1,222
  • 2
  • 17
  • 47
3
votes
1 answer

How do i run a future without awaiting it? (in rust)

I have some async function async fn get_player(name: String, i: Instant) -> Option { // some code here that returns a player structs } in my main function i want to run the above function concurrently in a loop, this function takes about…
SomeOnionGamer
  • 203
  • 1
  • 4
  • 8
3
votes
1 answer

Variable that chooses between two async functions in rust - incompatible arm types

I have a variable that will control which function is the default behavior of my web app, but both functions are async and it wont let me because they're different closures. Something like this reproduces the same…
3
votes
0 answers

How to access a global resource in an asynchronous context?

I've been writing a small web service with the tide framework in the async-std flavor. The project uses a database that is used sparsely but throughout the entire project. I thought it would be reasonable to declare it as a global variable, so that…
veprolet
  • 361
  • 2
  • 12
2
votes
0 answers

Rust - async-std's "tokio1" feature, how it works?

There is a feature flag on async-std: tokio1 async-std = { version = "1", features = ["tokio1", "attributes"] } Then I can use async_std::main on tokio stuff: #[async_std::main] async fn main() -> std::io::Result<()> { // some tokio…
eminfedar
  • 558
  • 3
  • 16
2
votes
3 answers

Timeout doesn't time out in AsyncRead

I'm trying to implement an async read wrapper that will add read timeout functionality. The objective is that the API is plain AsyncRead. In other words, I don't want to add io.read(buf).timeout(t) everywehere in the code. Instead, the read instance…
Robert Cutajar
  • 3,181
  • 1
  • 30
  • 42
2
votes
0 answers

Cannot collect Stream of Results into a Future Result

Given function that returns a stream for which each item could fail: pub type Error = Box; pub type Result = result::Result; fn query_paginated_items(params: Query) -> impl Stream> { ... } I…
cheezsteak
  • 2,731
  • 4
  • 26
  • 41
2
votes
2 answers

How to use async_std::task::sleep to simulate blocking operation?

I have a simple code like this to simulate how asynchronous code works on a blocking operation. I'm expecting all these "Hello" prints will be shown after a 1000ms. But this code works like a normal blocking code, each hello_wait call waits 1000ms…
eminfedar
  • 558
  • 3
  • 16
2
votes
1 answer

How to use async to parallelize heavy computation?

I would like to perform the following processing in multi-threads using tokio or async-std. I have read tutorials but I haven't seen any mention of parallelizing a for loop. In my program, all threads refer to the same array but will access…
musako
  • 897
  • 2
  • 10
  • 26
2
votes
1 answer

Rust, how to perform basic recursive async?

I am just doing some quick experimenting in an attempt to learn the rust language, I have done a few successful async tests, this is my starting point: use async_std::task; use futures; use std::time::SystemTime; fn main() { let now =…
Daniel Robinson
  • 13,806
  • 18
  • 64
  • 112
2
votes
1 answer

Why can't I send multiple requests in parallel using rust tonic?

I implemented the tonic helloworld tutorial. I then tried to change the client code so that I could send multiple requests before awaiting any. #[tokio::main] async fn main() -> Result<(), Box> { let num_requests = 10; …
matanmarkind
  • 219
  • 3
  • 13
1
vote
1 answer

Rust compiler does not unify types that both `impl Future` in branches of `if let` expression

While trying to create a Future conditionally, for later use with select_biased!: let mut heartbeat_timer = if let ServerState::Leader(_, _, _) = server_state { // if we're the Leader, we want to send out AppendEntries…
Tommy Knowlton
  • 580
  • 1
  • 4
  • 15
1
2 3 4