rust-tokio

What does the 'static lifetime mean in a trait bound in a Rust future? [duplicate]

前提是你 提交于 2019-12-01 11:03:46
This question already has an answer here: Why does the Rust compiler request I constrain a generic type parameter's lifetime (error E0309)? 1 answer Why is the bound `T: 'a` required in order to store a reference `&'a T`? 2 answers The compiler suggests I add a 'static lifetime because the parameter type may not live long enough, but I don't think that's what I want 2 answers I thought that I've got what the 'static lifetime is, but now I'm not sure. I'm trying to understand how to work with Tokio and Futures. My app is working, but the structure is awful, so I need to decompose it. Here comes

What does the 'static lifetime mean in a trait bound in a Rust future? [duplicate]

青春壹個敷衍的年華 提交于 2019-12-01 08:31:52
问题 This question already has answers here : Why does the Rust compiler request I constrain a generic type parameter's lifetime (error E0309)? (1 answer) Why is the bound `T: 'a` required in order to store a reference `&'a T`? (2 answers) The compiler suggests I add a 'static lifetime because the parameter type may not live long enough, but I don't think that's what I want (2 answers) Closed last year . I thought that I've got what the 'static lifetime is, but now I'm not sure. I'm trying to

How can I perform parallel asynchronous HTTP GET requests with reqwest?

人走茶凉 提交于 2019-11-30 21:17:34
The async example is useful, but being new to Rust and Tokio, I am struggling to work out how to do N requests at once, using URLs from a vector, and creating an iterator of the response HTML for each URL as a string. How could this be done? As of reqwest 0.9: use futures::{stream, Future, Stream}; // 0.1.26 use reqwest::r#async::Client; // 0.9.14 use tokio; // 0.1.18 type Result<T> = std::result::Result<T, Box<std::error::Error>>; const PARALLEL_REQUESTS: usize = 2; fn main() -> Result<()> { let client = Client::new(); let urls = vec!["https://api.ipify.org", "https://api.ipify.org"]; let

How to read subprocess output asynchronously

眉间皱痕 提交于 2019-11-29 15:35:45
I want to implement a futures::Stream for reading and parsing the standard output of a child subprocess. What I'm doing at the moment: spawn subprocess and obtain its stdout via std::process methods: let child = Command::new(...).stdout(Stdio.pipe()).spawn().expect(...) add AsyncRead and BufRead to stdout: let stdout = BufReader::new(tokio_io::io::AllowStdIo::new( child.stdout.expect("Failed to open stdout"), )); declare a wrapper struct for stdout: struct MyStream<Io: AsyncRead + BufRead> { io: Io, } implement Stream : impl<Io: AsyncRead + BufRead> Stream for MyStream<Io> { type Item =

How to read subprocess output asynchronously

风流意气都作罢 提交于 2019-11-28 09:17:37
问题 I want to implement a futures::Stream for reading and parsing the standard output of a child subprocess. What I'm doing at the moment: spawn subprocess and obtain its stdout via std::process methods: let child = Command::new(...).stdout(Stdio.pipe()).spawn().expect(...) add AsyncRead and BufRead to stdout: let stdout = BufReader::new(tokio_io::io::AllowStdIo::new( child.stdout.expect("Failed to open stdout"), )); declare a wrapper struct for stdout: struct MyStream<Io: AsyncRead + BufRead> {

How do I read the entire body of a Tokio-based Hyper request?

自作多情 提交于 2019-11-26 14:51:42
问题 I want to write a server using the current master branch of Hyper that saves a message that is delivered by a POST request and sends this message to every incoming GET request. I have this, mostly copied from the Hyper examples directory: extern crate futures; extern crate hyper; extern crate pretty_env_logger; use futures::future::FutureResult; use hyper::{Get, Post, StatusCode}; use hyper::header::{ContentLength}; use hyper::server::{Http, Service, Request, Response}; use futures::Stream;