Created
November 28, 2018 19:34
-
-
Save jonhoo/2cd16383489b6a97df6b7d79af78c4bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env run-cargo-script | |
//! ```cargo | |
//! [dependencies] | |
//! tokio = "0.1" | |
//! hyper = "0.12" | |
//! futures = "0.1" | |
//! tower-service = { git = "https://github.com/tower-rs/tower.git" } | |
//! tower-buffer = { git = "https://github.com/tower-rs/tower.git" } | |
//! ``` | |
extern crate futures; | |
extern crate hyper; | |
extern crate tokio; | |
extern crate tower_buffer; | |
extern crate tower_service; | |
use tokio::prelude::*; | |
use tower_service::Service; | |
struct Svc(hyper::Client<hyper::client::HttpConnector>); | |
impl Service<hyper::Request<hyper::Body>> for Svc { | |
type Error = hyper::Error; | |
type Response = hyper::Response<hyper::Body>; | |
type Future = hyper::client::ResponseFuture; | |
fn call(&mut self, r: hyper::Request<hyper::Body>) -> Self::Future { | |
self.0.request(r) | |
} | |
fn poll_ready(&mut self) -> Poll<(), Self::Error> { | |
Ok(().into()) | |
} | |
} | |
fn main() { | |
let mut rt = tokio::runtime::Runtime::new().unwrap(); | |
let mut b = rt | |
.block_on(future::lazy(move || { | |
tower_buffer::Buffer::new(Svc(hyper::Client::new())) | |
})) | |
.unwrap_or_else(|_| { | |
panic!("failed to spawn Svc"); | |
}); | |
b.call( | |
hyper::Request::builder() | |
.uri("http://www.rust-lang.org/") | |
.body(hyper::Body::empty()) | |
.unwrap(), | |
) | |
.wait() | |
.unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment