Questions tagged [rust-tokio]

Tokio is an event-driven, non-blocking I/O platform for writing asynchronous applications with the Rust programming language.

Tokio aims to be:

  • Fast: Tokio's zero-cost abstractions give you bare-metal performance.

  • Productive: Tokio makes it easy to implement protocols and program asynchronously.

  • Reliable: Tokio leverages Rust's ownership and concurrency model to ensure thread safety.

  • Scalable: Tokio has a minimal footprint, and handles backpressure and cancellation naturally.

At a high level, it provides a few major components:

  • A multithreaded, work-stealing based task scheduler.
  • A reactor backed by the operating system's event queue (epoll, kqueue, IOCP, etc...).
  • Asynchronous TCP and UDP sockets.

You can find more information in

1053 questions
3
votes
1 answer

Rust tokio_postgres row to object mapping fails with serde_postgres for timestamp column

I have rust actix-web rest project, which uses tokio_postgres and serde_postgres. The Table acme=# \d job Table "public.job" Column | Type | Collation | Nullable | …
Bopsi
  • 2,090
  • 5
  • 36
  • 58
3
votes
1 answer

reqwest segmentation fault from AWS Lambda with Rust

I can't figure out how to make a simple HTTP request from AWS Lambda using rust without getting a segmentation fault. I get this error regardless of whether I use tokio-0.2 or tokio-0.3 with a compatibility layer. It looks like lambda_http was…
Dan Jenson
  • 961
  • 7
  • 20
3
votes
1 answer

Second mutable borrow error disapears after introducing a magic line

This is my struct (for completeness sake): struct Client<'a, T> where T: AsyncRead + AsyncWrite + Unpin { socket: T, stage: u8, n: usize, buf: &'a mut [u8], } I then implemented a Future for the struct, but changed self to me…
Ark
  • 117
  • 2
  • 7
3
votes
1 answer

Receiver on "tokio::mpsc::channel" only receives messages when buffer is full

In my code snippet the tokio (v0.3) mpsc:channel receiver only receives a message when the buffer is full. It doesn't matter how big or small the buffer is. use std::io; use std::net::{SocketAddr, ToSocketAddrs}; use std::sync::Arc; use…
createproblem
  • 1,542
  • 16
  • 30
3
votes
1 answer

How to write a simple warp handler returning json or html?

I have the following: use warp::Filter; pub struct Router {} impl Router { pub async fn handle( &self, ) -> std::result::Result { let uri = "/path"; match uri { …
Patryk
  • 22,602
  • 44
  • 128
  • 244
3
votes
1 answer

Test does not compile: "the async keyword is missing from the function declaration"

I'm trying to get working tests in my project (src/subdir/subdir2/file.rs): #[cfg(test)] mod tests { #[tokio::test] async fn test_format_str() { let src = "a"; let expect = "a"; assert_eq!(expect, src); } } And…
Marcus Grass
  • 1,043
  • 2
  • 17
  • 38
3
votes
1 answer

Tokio & serde: deserializing JSON

While trying to deserialize into a JSON Value from a tokio TcpStream, I'm trying to use this function: use futures::prelude::*; use serde_json::Value; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::{TcpListener, TcpStream}; use…
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
3
votes
0 answers

how to read in chunks using tokio::io::BufReader

I am calculating the checksum of a file using something like this: pub fn sha256_digest(file_path: &str) -> Result> { let file = fs::File::open(file_path)?; let mut reader = BufReader::new(file); let mut context =…
nbari
  • 25,603
  • 10
  • 76
  • 131
3
votes
1 answer

Rust use Postgres JSON attribute: cannot convert between the Rust type `alloc::string::String` and the Postgres type `jsonb`

Currently I am able to use the following code, but I do not want to have to cast my JSON to text in my postgres query since it adds latency. async fn reverse_geocode(min : f32, max : f32, pool: &Pool) -> Result { let client:…
Werner Raath
  • 1,322
  • 3
  • 16
  • 34
3
votes
1 answer

Send messages to clients with multiple references to websockets

My question here is in the context of using actix-web with Rust. Unfortunately I can't explain this without a somewhat hefty code example, so let me start with that. struct MyWs { game: Arc>, } impl Actor for MyWs { type…
Daniel Porteous
  • 5,536
  • 3
  • 25
  • 44
3
votes
1 answer

Request body is empty while making post request from "reqwest" crate rust

I am trying to make post request using reqwest crate of rust. Here is the snippet of cargo.toml [dependencies] tokio = { version = "0.2", features = ["full"] } reqwest = { version = "0.10", features = ["json"] } Here is the snippet of code from…
Jawwad Turabi
  • 322
  • 4
  • 12
3
votes
1 answer

Rust JNI async callback with Tokio and Reqwest for Android

I'm testing Rust with JNI async execution. I want to do execute requests in Rust and return the result to Android asynchronously with callback. I'm testing code to execute the request in the command line and it works fine. That is how it works on…
ilbets
  • 710
  • 1
  • 9
  • 35
3
votes
1 answer

How do I solve "cannot return value referencing local data" when using threads and async/await?

I am learning Rust especially multithreading and async requests in parallel. I read the documentation and still I do not understand where I made a mistake. I assume I know where, but do not see how to resolve it. main.rs use std::thread; struct…
3
votes
3 answers

Does rust currently have a library implement function similar to JavaScript's setTimeout and setInterval?

Does rust currently have a library implement function similar to JavaScript's setTimeout and setInverval?, that is, a library that can call multiple setTimeout and setInterval to implement management of multiple tasks at the same time. I feel that…
聂小涛
  • 503
  • 3
  • 16
3
votes
0 answers

Pass Arc into async handler

I have a global state which can be used down the code. For my hyper request handling, I generate a local state to group the data for easier access. I decided against a global static variable to use Arc> for thread-safe access. Now I have…
fragsalat
  • 500
  • 4
  • 14