Apply rustfmt to test/support crate (#116)

This commit is contained in:
Oliver Gould
2017-09-25 08:29:54 -07:00
committed by Sean McArthur
parent dad113e17b
commit b99c513334
6 changed files with 175 additions and 169 deletions

View File

@@ -3,8 +3,8 @@ use std::fmt;
use bytes::{Bytes, IntoBuf};
use http::{self, HeaderMap, HttpTryFrom};
use h2::frame::{self, Frame, StreamId};
use super::SendFrame;
use h2::frame::{self, Frame, StreamId};
pub const SETTINGS: &'static [u8] = &[0, 0, 0, 4, 0, 0, 0, 0, 0];
pub const SETTINGS_ACK: &'static [u8] = &[0, 0, 0, 4, 1, 0, 0, 0, 0];
@@ -12,7 +12,8 @@ pub const SETTINGS_ACK: &'static [u8] = &[0, 0, 0, 4, 1, 0, 0, 0, 0];
// ==== helper functions to easily construct h2 Frames ====
pub fn headers<T>(id: T) -> Mock<frame::Headers>
where T: Into<StreamId>,
where
T: Into<StreamId>,
{
Mock(frame::Headers::new(
id.into(),
@@ -22,15 +23,17 @@ pub fn headers<T>(id: T) -> Mock<frame::Headers>
}
pub fn data<T, B>(id: T, buf: B) -> Mock<frame::Data>
where T: Into<StreamId>,
B: Into<Bytes>,
where
T: Into<StreamId>,
B: Into<Bytes>,
{
Mock(frame::Data::new(id.into(), buf.into()))
}
pub fn push_promise<T1, T2>(id: T1, promised: T2) -> Mock<frame::PushPromise>
where T1: Into<StreamId>,
T2: Into<StreamId>,
where
T1: Into<StreamId>,
T2: Into<StreamId>,
{
Mock(frame::PushPromise::new(
id.into(),
@@ -41,19 +44,22 @@ where T1: Into<StreamId>,
}
pub fn window_update<T>(id: T, sz: u32) -> frame::WindowUpdate
where T: Into<StreamId>,
where
T: Into<StreamId>,
{
frame::WindowUpdate::new(id.into(), sz)
}
pub fn go_away<T>(id: T) -> Mock<frame::GoAway>
where T: Into<StreamId>,
where
T: Into<StreamId>,
{
Mock(frame::GoAway::new(id.into(), frame::Reason::NoError))
}
pub fn reset<T>(id: T) -> Mock<frame::Reset>
where T: Into<StreamId>,
where
T: Into<StreamId>,
{
Mock(frame::Reset::new(id.into(), frame::Reason::NoError))
}
@@ -73,7 +79,9 @@ impl<T: fmt::Debug> fmt::Debug for Mock<T> {
}
impl<T> From<Mock<T>> for Frame
where T: Into<Frame> {
where
T: Into<Frame>,
{
fn from(src: Mock<T>) -> Self {
src.0.into()
}
@@ -83,30 +91,24 @@ where T: Into<Frame> {
impl Mock<frame::Headers> {
pub fn request<M, U>(self, method: M, uri: U) -> Self
where M: HttpTryInto<http::Method>,
U: HttpTryInto<http::Uri>,
where
M: HttpTryInto<http::Method>,
U: HttpTryInto<http::Uri>,
{
let method = method.try_into().unwrap();
let uri = uri.try_into().unwrap();
let (id, _, fields) = self.into_parts();
let frame = frame::Headers::new(
id,
frame::Pseudo::request(method, uri),
fields
);
let frame = frame::Headers::new(id, frame::Pseudo::request(method, uri), fields);
Mock(frame)
}
pub fn response<S>(self, status: S) -> Self
where S: HttpTryInto<http::StatusCode>,
where
S: HttpTryInto<http::StatusCode>,
{
let status = status.try_into().unwrap();
let (id, _, fields) = self.into_parts();
let frame = frame::Headers::new(
id,
frame::Pseudo::response(status),
fields
);
let frame = frame::Headers::new(id, frame::Pseudo::response(status), fields);
Mock(frame)
}
@@ -161,18 +163,15 @@ impl From<Mock<frame::Data>> for SendFrame {
impl Mock<frame::PushPromise> {
pub fn request<M, U>(self, method: M, uri: U) -> Self
where M: HttpTryInto<http::Method>,
U: HttpTryInto<http::Uri>,
where
M: HttpTryInto<http::Method>,
U: HttpTryInto<http::Uri>,
{
let method = method.try_into().unwrap();
let uri = uri.try_into().unwrap();
let (id, promised, _, fields) = self.into_parts();
let frame = frame::PushPromise::new(
id,
promised,
frame::Pseudo::request(method, uri),
fields
);
let frame =
frame::PushPromise::new(id, promised, frame::Pseudo::request(method, uri), fields);
Mock(frame)
}
@@ -201,15 +200,24 @@ impl From<Mock<frame::PushPromise>> for SendFrame {
impl Mock<frame::GoAway> {
pub fn protocol_error(self) -> Self {
Mock(frame::GoAway::new(self.0.last_stream_id(), frame::Reason::ProtocolError))
Mock(frame::GoAway::new(
self.0.last_stream_id(),
frame::Reason::ProtocolError,
))
}
pub fn flow_control(self) -> Self {
Mock(frame::GoAway::new(self.0.last_stream_id(), frame::Reason::FlowControlError))
Mock(frame::GoAway::new(
self.0.last_stream_id(),
frame::Reason::FlowControlError,
))
}
pub fn frame_size(self) -> Self {
Mock(frame::GoAway::new(self.0.last_stream_id(), frame::Reason::FrameSizeError))
Mock(frame::GoAway::new(
self.0.last_stream_id(),
frame::Reason::FrameSizeError,
))
}
}
@@ -264,8 +272,9 @@ pub trait HttpTryInto<T> {
}
impl<T, U> HttpTryInto<T> for U
where T: HttpTryFrom<U>,
T::Error: fmt::Debug,
where
T: HttpTryFrom<U>,
T::Error: fmt::Debug,
{
type Error = T::Error;

View File

@@ -1,4 +1,4 @@
use futures::{Future, Async, Poll};
use futures::{Async, Future, Poll};
use std::fmt;
@@ -6,17 +6,21 @@ use std::fmt;
pub trait FutureExt: Future {
/// Panic on error
fn unwrap(self) -> Unwrap<Self>
where Self: Sized,
Self::Error: fmt::Debug,
where
Self: Sized,
Self::Error: fmt::Debug,
{
Unwrap { inner: self }
Unwrap {
inner: self,
}
}
/// Panic on error, with a message.
fn expect<T>(self, msg: T) -> Expect<Self>
where Self: Sized,
Self::Error: fmt::Debug,
T: fmt::Display,
where
Self: Sized,
Self::Error: fmt::Debug,
T: fmt::Display,
{
Expect {
inner: self,
@@ -28,10 +32,11 @@ pub trait FutureExt: Future {
///
/// `self` must not resolve before `other` does.
fn drive<T>(self, other: T) -> Drive<Self, T>
where T: Future,
T::Error: fmt::Debug,
Self: Future<Item = ()> + Sized,
Self::Error: fmt::Debug,
where
T: Future,
T::Error: fmt::Debug,
Self: Future<Item = ()> + Sized,
Self::Error: fmt::Debug,
{
Drive {
driver: Some(self),
@@ -40,8 +45,7 @@ pub trait FutureExt: Future {
}
}
impl<T: Future> FutureExt for T {
}
impl<T: Future> FutureExt for T {}
// ===== Unwrap ======
@@ -51,9 +55,10 @@ pub struct Unwrap<T> {
}
impl<T> Future for Unwrap<T>
where T: Future,
T::Item: fmt::Debug,
T::Error: fmt::Debug,
where
T: Future,
T::Item: fmt::Debug,
T::Error: fmt::Debug,
{
type Item = T::Item;
type Error = ();
@@ -73,9 +78,10 @@ pub struct Expect<T> {
}
impl<T> Future for Expect<T>
where T: Future,
T::Item: fmt::Debug,
T::Error: fmt::Debug,
where
T: Future,
T::Item: fmt::Debug,
T::Error: fmt::Debug,
{
type Item = T::Item;
type Error = ();
@@ -96,10 +102,11 @@ pub struct Drive<T, U> {
}
impl<T, U> Future for Drive<T, U>
where T: Future<Item = ()>,
U: Future,
T::Error: fmt::Debug,
U::Error: fmt::Debug,
where
T: Future<Item = ()>,
U: Future,
T::Error: fmt::Debug,
U::Error: fmt::Debug,
{
type Item = (T, U::Item);
type Error = ();
@@ -113,9 +120,9 @@ impl<T, U> Future for Drive<T, U>
// Get the driver
let driver = self.driver.take().unwrap();
return Ok((driver, val).into())
}
Ok(_) => {}
return Ok((driver, val).into());
},
Ok(_) => {},
Err(e) => panic!("unexpected error; {:?}", e),
}
@@ -128,8 +135,8 @@ impl<T, U> Future for Drive<T, U>
looped = true;
continue;
}
}
Ok(Async::NotReady) => {}
},
Ok(Async::NotReady) => {},
Err(e) => panic!("unexpected error; {:?}", e),
}

View File

@@ -7,10 +7,10 @@ pub extern crate http;
#[macro_use]
pub extern crate tokio_io;
pub extern crate env_logger;
#[macro_use]
pub extern crate futures;
pub extern crate mock_io;
pub extern crate env_logger;
#[macro_use]
mod assert;

View File

@@ -1,11 +1,11 @@
use {FutureExt, SendFrame};
use h2::{self, SendError, RecvError};
use h2::{self, RecvError, SendError};
use h2::frame::{self, Frame};
use futures::{Async, Future, Stream, Poll};
use futures::task::{self, Task};
use futures::{Async, Future, Poll, Stream};
use futures::sync::oneshot;
use futures::task::{self, Task};
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_io::io::read_exact;
@@ -58,7 +58,9 @@ pub fn new() -> (Mock, Handle) {
};
let handle = Handle {
codec: h2::Codec::new(Pipe { inner }),
codec: h2::Codec::new(Pipe {
inner,
}),
};
(mock, handle)
@@ -92,36 +94,37 @@ impl Handle {
}
/// Read the client preface
pub fn read_preface(self)
-> Box<Future<Item = Self, Error = io::Error>>
{
pub fn read_preface(self) -> Box<Future<Item = Self, Error = io::Error>> {
let buf = vec![0; PREFACE.len()];
let ret = read_exact(self, buf)
.and_then(|(me, buf)| {
assert_eq!(buf, PREFACE);
Ok(me)
});
let ret = read_exact(self, buf).and_then(|(me, buf)| {
assert_eq!(buf, PREFACE);
Ok(me)
});
Box::new(ret)
}
/// Perform the H2 handshake
pub fn assert_client_handshake(self)
-> Box<Future<Item = (frame::Settings, Self), Error = h2::Error>>
{
pub fn assert_client_handshake(
self,
) -> Box<Future<Item = (frame::Settings, Self), Error = h2::Error>> {
self.assert_client_handshake_with_settings(frame::Settings::default())
}
/// Perform the H2 handshake
pub fn assert_client_handshake_with_settings<T>(mut self, settings: T)
-> Box<Future<Item = (frame::Settings, Self), Error = h2::Error>>
where T: Into<frame::Settings>,
pub fn assert_client_handshake_with_settings<T>(
mut self,
settings: T,
) -> Box<Future<Item = (frame::Settings, Self), Error = h2::Error>>
where
T: Into<frame::Settings>,
{
let settings = settings.into();
// Send a settings frame
self.send(settings.into()).unwrap();
let ret = self.read_preface().unwrap()
let ret = self.read_preface()
.unwrap()
.and_then(|me| me.into_future().unwrap())
.map(|(frame, mut me)| {
match frame {
@@ -133,13 +136,13 @@ impl Handle {
me.send(ack.into()).unwrap();
(settings, me)
}
},
Some(frame) => {
panic!("unexpected frame; frame={:?}", frame);
}
},
None => {
panic!("unexpected EOF");
}
},
}
})
.then(|res| {
@@ -155,8 +158,7 @@ impl Handle {
(settings, me)
})
})
;
});
Box::new(ret)
}
@@ -177,8 +179,7 @@ impl io::Read for Handle {
}
}
impl AsyncRead for Handle {
}
impl AsyncRead for Handle {}
impl io::Write for Handle {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
@@ -215,7 +216,10 @@ impl Drop for Handle {
impl io::Read for Mock {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
assert!(buf.len() > 0, "attempted read with zero length buffer... wut?");
assert!(
buf.len() > 0,
"attempted read with zero length buffer... wut?"
);
let mut me = self.pipe.inner.lock().unwrap();
@@ -236,8 +240,7 @@ impl io::Read for Mock {
}
}
impl AsyncRead for Mock {
}
impl AsyncRead for Mock {}
impl io::Write for Mock {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
@@ -273,7 +276,10 @@ impl AsyncWrite for Mock {
impl io::Read for Pipe {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
assert!(buf.len() > 0, "attempted read with zero length buffer... wut?");
assert!(
buf.len() > 0,
"attempted read with zero length buffer... wut?"
);
let mut me = self.inner.lock().unwrap();
@@ -290,8 +296,7 @@ impl io::Read for Pipe {
}
}
impl AsyncRead for Pipe {
}
impl AsyncRead for Pipe {}
impl io::Write for Pipe {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
@@ -319,38 +324,43 @@ impl AsyncWrite for Pipe {
}
pub trait HandleFutureExt {
fn recv_settings(self) -> RecvFrame<Box<Future<Item=(Option<Frame>, Handle), Error=()>>>
where Self: Sized + 'static,
Self: Future<Item=(frame::Settings, Handle)>,
Self::Error: fmt::Debug,
fn recv_settings(self) -> RecvFrame<Box<Future<Item = (Option<Frame>, Handle), Error = ()>>>
where
Self: Sized + 'static,
Self: Future<Item = (frame::Settings, Handle)>,
Self::Error: fmt::Debug,
{
let map = self.map(|(settings, handle)| (Some(settings.into()), handle)).unwrap();
let map = self.map(|(settings, handle)| (Some(settings.into()), handle))
.unwrap();
let boxed: Box<Future<Item=(Option<Frame>, Handle), Error=()>> = Box::new(map);
let boxed: Box<Future<Item = (Option<Frame>, Handle), Error = ()>> = Box::new(map);
RecvFrame {
inner: boxed,
frame: frame::Settings::default().into(),
}
}
fn ignore_settings(self) -> Box<Future<Item=Handle, Error=()>>
where Self: Sized + 'static,
Self: Future<Item=(frame::Settings, Handle)>,
Self::Error: fmt::Debug,
fn ignore_settings(self) -> Box<Future<Item = Handle, Error = ()>>
where
Self: Sized + 'static,
Self: Future<Item = (frame::Settings, Handle)>,
Self::Error: fmt::Debug,
{
Box::new(self.map(|(_settings, handle)| handle).unwrap())
}
fn recv_frame<T>(self, frame: T) -> RecvFrame<<Self as IntoRecvFrame>::Future>
where Self: IntoRecvFrame + Sized,
T: Into<Frame>,
where
Self: IntoRecvFrame + Sized,
T: Into<Frame>,
{
self.into_recv_frame(frame.into())
}
fn send_frame<T>(self, frame: T) -> SendFrameFut<Self>
where Self: Sized,
T: Into<SendFrame>,
where
Self: Sized,
T: Into<SendFrame>,
{
SendFrameFut {
inner: self,
@@ -358,10 +368,11 @@ pub trait HandleFutureExt {
}
}
fn idle_ms(self, ms: usize) -> Box<Future<Item=Handle, Error=Self::Error>>
where Self: Sized + 'static,
Self: Future<Item=Handle>,
Self::Error: fmt::Debug,
fn idle_ms(self, ms: usize) -> Box<Future<Item = Handle, Error = Self::Error>>
where
Self: Sized + 'static,
Self: Future<Item = Handle>,
Self::Error: fmt::Debug,
{
use std::thread;
use std::time::Duration;
@@ -383,8 +394,9 @@ pub trait HandleFutureExt {
}))
}
fn close(self) -> Box<Future<Item=(), Error=()>>
where Self: Future<Error = ()> + Sized + 'static,
fn close(self) -> Box<Future<Item = (), Error = ()>>
where
Self: Future<Error = ()> + Sized + 'static,
{
Box::new(self.map(drop))
}
@@ -396,8 +408,9 @@ pub struct RecvFrame<T> {
}
impl<T> Future for RecvFrame<T>
where T: Future<Item=(Option<Frame>, Handle)>,
T::Error: fmt::Debug,
where
T: Future<Item = (Option<Frame>, Handle)>,
T::Error: fmt::Debug,
{
type Item = Handle;
type Error = ();
@@ -418,8 +431,9 @@ pub struct SendFrameFut<T> {
}
impl<T> Future for SendFrameFut<T>
where T: Future<Item=Handle>,
T::Error: fmt::Debug,
where
T: Future<Item = Handle>,
T::Error: fmt::Debug,
{
type Item = Handle;
type Error = ();
@@ -452,13 +466,14 @@ impl Future for Idle {
Ok(Async::NotReady) => Ok(Async::NotReady),
res => {
panic!("Received unexpected frame on handle; frame={:?}", res);
}
},
}
}
}
impl<T> HandleFutureExt for T
where T: Future + 'static,
where
T: Future + 'static,
{
}
@@ -479,14 +494,16 @@ impl IntoRecvFrame for Handle {
}
impl<T> IntoRecvFrame for T
where T: Future<Item=Handle> + 'static,
T::Error: fmt::Debug,
where
T: Future<Item = Handle> + 'static,
T::Error: fmt::Debug,
{
type Future = Box<Future<Item=(Option<Frame>, Handle), Error=()>>;
type Future = Box<Future<Item = (Option<Frame>, Handle), Error = ()>>;
fn into_recv_frame(self, frame: Frame) -> RecvFrame<Self::Future> {
let into_fut = Box::new(self.unwrap()
.and_then(|handle| handle.into_future().unwrap())
let into_fut = Box::new(
self.unwrap()
.and_then(|handle| handle.into_future().unwrap()),
);
RecvFrame {
inner: into_fut,

View File

@@ -3,8 +3,8 @@
pub use super::h2;
pub use self::h2::*;
pub use self::h2::frame::StreamId;
pub use self::h2::client::{self, Client};
pub use self::h2::frame::StreamId;
pub use self::h2::server::{self, Server};
// Re-export mock
@@ -20,41 +20,18 @@ pub use super::util;
pub use super::{Codec, SendFrame};
// Re-export useful crates
pub use super::{
http,
bytes,
tokio_io,
futures,
mock_io,
env_logger,
};
pub use super::{bytes, env_logger, futures, http, mock_io, tokio_io};
// Re-export primary future types
pub use self::futures::{
Future,
Sink,
Stream,
};
pub use self::futures::{Future, Sink, Stream};
// And our Future extensions
pub use future_ext::{FutureExt, Unwrap};
// Re-export HTTP types
pub use self::http::{
Request,
Response,
Method,
HeaderMap,
StatusCode,
};
pub use self::http::{HeaderMap, Method, Request, Response, StatusCode};
pub use self::bytes::{
Buf,
BufMut,
Bytes,
BytesMut,
IntoBuf,
};
pub use self::bytes::{Buf, BufMut, Bytes, BytesMut, IntoBuf};
pub use tokio_io::{AsyncRead, AsyncWrite};
@@ -63,7 +40,7 @@ pub use std::time::Duration;
// ===== Everything under here shouldn't be used =====
// TODO: work on deleting this code
pub use ::futures::future::poll_fn;
pub use futures::future::poll_fn;
pub trait MockH2 {
fn handshake(&mut self) -> &mut Self;
@@ -84,25 +61,24 @@ pub trait ClientExt {
}
impl<T, B> ClientExt for Client<T, B>
where T: AsyncRead + AsyncWrite + 'static,
B: IntoBuf + 'static,
where
T: AsyncRead + AsyncWrite + 'static,
B: IntoBuf + 'static,
{
fn run<F: Future>(&mut self, f: F) -> Result<F::Item, F::Error> {
use futures::future::{self, Future};
use futures::future::Either::*;
let res = future::poll_fn(|| self.poll())
.select2(f).wait();
let res = future::poll_fn(|| self.poll()).select2(f).wait();
match res {
Ok(A((_, b))) => {
// Connection is done...
b.wait()
}
},
Ok(B((v, _))) => return Ok(v),
Err(A((e, _))) => panic!("err: {:?}", e),
Err(B((e, _))) => return Err(e),
}
}
}

View File

@@ -1,12 +1,9 @@
use h2::client;
use futures::{Poll, Async, Future};
use bytes::Bytes;
use futures::{Async, Future, Poll};
pub fn wait_for_capacity(stream: client::Stream<Bytes>,
target: usize)
-> WaitForCapacity
{
pub fn wait_for_capacity(stream: client::Stream<Bytes>, target: usize) -> WaitForCapacity {
WaitForCapacity {
stream: Some(stream),
target: target,
@@ -36,7 +33,7 @@ impl Future for WaitForCapacity {
println!("CAP={:?}", act);
if act >= self.target {
return Ok(self.stream.take().unwrap().into())
return Ok(self.stream.take().unwrap().into());
}
Ok(Async::NotReady)