rust-tokio

The trait `std::future::Future` is not implemented for `std::result::Result<reqwest::Response, reqwest::Error>`

北慕城南 提交于 2020-05-29 09:43:24
问题 I'm trying to run basic reqwest example: extern crate reqwest; extern crate tokio; #[tokio::main] async fn main() -> Result<(), reqwest::Error> { let res = reqwest::Client::new() .get("https://hyper.rs") .send() .await?; println!("Status: {}", res.status()); let body = res.text().await?; println!("Body:\n\n{}", body); Ok(()) } Error that I'm getting: --> src/main.rs:6:15 | 6 | let res = reqwest::Client::new() | _______________^ 7 | | .get("https://hyper.rs") 8 | | .send() 9 | | .await?; | |__

The trait `std::future::Future` is not implemented for `std::result::Result<reqwest::Response, reqwest::Error>`

烈酒焚心 提交于 2020-05-29 09:41:58
问题 I'm trying to run basic reqwest example: extern crate reqwest; extern crate tokio; #[tokio::main] async fn main() -> Result<(), reqwest::Error> { let res = reqwest::Client::new() .get("https://hyper.rs") .send() .await?; println!("Status: {}", res.status()); let body = res.text().await?; println!("Body:\n\n{}", body); Ok(()) } Error that I'm getting: --> src/main.rs:6:15 | 6 | let res = reqwest::Client::new() | _______________^ 7 | | .get("https://hyper.rs") 8 | | .send() 9 | | .await?; | |__

The trait `std::future::Future` is not implemented for `std::result::Result<reqwest::Response, reqwest::Error>`

我怕爱的太早我们不能终老 提交于 2020-05-29 09:41:20
问题 I'm trying to run basic reqwest example: extern crate reqwest; extern crate tokio; #[tokio::main] async fn main() -> Result<(), reqwest::Error> { let res = reqwest::Client::new() .get("https://hyper.rs") .send() .await?; println!("Status: {}", res.status()); let body = res.text().await?; println!("Body:\n\n{}", body); Ok(()) } Error that I'm getting: --> src/main.rs:6:15 | 6 | let res = reqwest::Client::new() | _______________^ 7 | | .get("https://hyper.rs") 8 | | .send() 9 | | .await?; | |__

understanding error: trait `futures::future::Future` is not implemented for `()`

偶尔善良 提交于 2020-05-16 18:40:32
问题 This question is about how to read the Rust documentation and improve my understanding of Rust so as to understand how to address this specific compiler error. I've read the tokio docs and experimented with many of the examples. In writing my own code, I frequently run into compiler errors that I don't understand and often found I can fix the code, but don't understand why specific syntax is needed. I reproduced with a very simple example based on tokio's hello world: use futures::Future; use

Shared mutable state in Hyper

ぃ、小莉子 提交于 2020-03-25 04:41:14
问题 I'm trying to create a counter in a Hyper web server that counts the number of requests it has received. I'm using a Arc<Mutex<u64>> to hold onto count. However, I haven't been able to figure out the right combination of move and .clone() to satisfy the types of the closures. Here's some code that compiles, but resets the counter on each request: extern crate hyper; use hyper::rt::Future; use hyper::service::service_fn_ok; use hyper::{Body, Response, Server}; use std::sync::{Arc, Mutex}; fn

Shared mutable state in Hyper

廉价感情. 提交于 2020-03-25 04:40:00
问题 I'm trying to create a counter in a Hyper web server that counts the number of requests it has received. I'm using a Arc<Mutex<u64>> to hold onto count. However, I haven't been able to figure out the right combination of move and .clone() to satisfy the types of the closures. Here's some code that compiles, but resets the counter on each request: extern crate hyper; use hyper::rt::Future; use hyper::service::service_fn_ok; use hyper::{Body, Response, Server}; use std::sync::{Arc, Mutex}; fn

Shared mutable state in Hyper

拜拜、爱过 提交于 2020-03-25 04:39:09
问题 I'm trying to create a counter in a Hyper web server that counts the number of requests it has received. I'm using a Arc<Mutex<u64>> to hold onto count. However, I haven't been able to figure out the right combination of move and .clone() to satisfy the types of the closures. Here's some code that compiles, but resets the counter on each request: extern crate hyper; use hyper::rt::Future; use hyper::service::service_fn_ok; use hyper::{Body, Response, Server}; use std::sync::{Arc, Mutex}; fn

Expected XYZ found ()

与世无争的帅哥 提交于 2020-03-05 03:58:08
问题 For example: use futures::future::Future; fn main() { let (stop_tokio, time_to_stop) = tokio::sync::oneshot::channel::<()>(); let handler = std::thread::spawn(|| { tokio::run( time_to_stop, // .map_err(|_| ()) ); }); handler.join().expect("join failed"); } The compiler prints the error: error[E0271]: type mismatch resolving `<tokio_sync::oneshot::Receiver<()> as futures::future::Future>::Error == ()` --> src/main.rs:6:9 | 6 | tokio::run( | ^^^^^^^^^^ expected struct `tokio_sync::oneshot:

Expected XYZ found ()

可紊 提交于 2020-03-05 03:57:11
问题 For example: use futures::future::Future; fn main() { let (stop_tokio, time_to_stop) = tokio::sync::oneshot::channel::<()>(); let handler = std::thread::spawn(|| { tokio::run( time_to_stop, // .map_err(|_| ()) ); }); handler.join().expect("join failed"); } The compiler prints the error: error[E0271]: type mismatch resolving `<tokio_sync::oneshot::Receiver<()> as futures::future::Future>::Error == ()` --> src/main.rs:6:9 | 6 | tokio::run( | ^^^^^^^^^^ expected struct `tokio_sync::oneshot:

Initializing a Rust variable passed to async code such as tokio and hyper

早过忘川 提交于 2020-02-16 05:29:22
问题 I have a value that cannot be computed at compile time. It needs to be computed before any of the app code runs, and then it will only be read throughout the lifetime of the app. It also needs to be passed around to executors such as tokio and hyper handlers. How can I create such a value safely, idiomatically and without unneeded performance losses? If I create it in main and pass it to hyper , it does not live long enough. If I create it with lazy_static! , it only gets computed when it's