Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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
You can’t perform that action at this time.