style(lib): run rustfmt and enforce in CI

This commit is contained in:
Sean McArthur
2019-12-05 13:30:53 -08:00
parent b0060f277e
commit 0dc89680cd
69 changed files with 2982 additions and 2499 deletions

View File

@@ -1,9 +1,9 @@
use std::mem;
use tokio::sync::{mpsc, watch};
use pin_project::pin_project;
use tokio::sync::{mpsc, watch};
use super::{Future, Never, Poll, Pin, task};
use super::{task, Future, Never, Pin, Poll};
// Sentinel value signaling that the watch is still open
#[derive(Clone, Copy)]
@@ -21,10 +21,7 @@ pub fn channel() -> (Signal, Watch) {
drained_rx,
_tx: tx,
},
Watch {
drained_tx,
rx,
},
Watch { drained_tx, rx },
)
}
@@ -107,17 +104,14 @@ where
Poll::Ready(None) => {
// Drain has been triggered!
on_drain(me.future.as_mut());
},
Poll::Ready(Some(_/*State::Open*/)) |
Poll::Pending => {
}
Poll::Ready(Some(_ /*State::Open*/)) | Poll::Pending => {
*me.state = State::Watch(on_drain);
return me.future.poll(cx);
},
}
}
},
State::Draining => {
return me.future.poll(cx)
},
}
State::Draining => return me.future.poll(cx),
}
}
}
@@ -236,4 +230,3 @@ mod tests {
});
}
}

View File

@@ -3,7 +3,7 @@ use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use crate::body::{Payload, Body};
use crate::body::{Body, Payload};
use crate::proto::h2::server::H2Stream;
use crate::server::conn::spawn_all::{NewSvcTask, Watcher};
use crate::service::HttpService;
@@ -22,7 +22,7 @@ pub trait NewSvcExec<I, N, S: HttpService<Body>, E, W: Watcher<I, S, E>>: Clone
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>);
}
pub type BoxSendFuture = Pin<Box<dyn Future<Output=()> + Send>>;
pub type BoxSendFuture = Pin<Box<dyn Future<Output = ()> + Send>>;
// Either the user provides an executor for background tasks, or we use
// `tokio::spawn`.
@@ -37,7 +37,7 @@ pub enum Exec {
impl Exec {
pub(crate) fn execute<F>(&self, fut: F)
where
F: Future<Output=()> + Send + 'static,
F: Future<Output = ()> + Send + 'static,
{
match *self {
Exec::Default => {
@@ -50,22 +50,20 @@ impl Exec {
// If no runtime, we need an executor!
panic!("executor must be set")
}
},
}
Exec::Executor(ref e) => {
e.execute(Box::pin(fut));
},
}
}
}
}
impl fmt::Debug for Exec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Exec")
.finish()
f.debug_struct("Exec").finish()
}
}
impl<F, B> H2Exec<F, B> for Exec
where
H2Stream<F, B>: Future<Output = ()> + Send + 'static,
@@ -78,7 +76,7 @@ where
impl<I, N, S, E, W> NewSvcExec<I, N, S, E, W> for Exec
where
NewSvcTask<I, N, S, E, W>: Future<Output=()> + Send + 'static,
NewSvcTask<I, N, S, E, W>: Future<Output = ()> + Send + 'static,
S: HttpService<Body>,
W: Watcher<I, S, E>,
{
@@ -92,7 +90,7 @@ where
impl<E, F, B> H2Exec<F, B> for E
where
E: Executor<H2Stream<F, B>> + Clone,
H2Stream<F, B>: Future<Output=()>,
H2Stream<F, B>: Future<Output = ()>,
B: Payload,
{
fn execute_h2stream(&mut self, fut: H2Stream<F, B>) {
@@ -103,7 +101,7 @@ where
impl<I, N, S, E, W> NewSvcExec<I, N, S, E, W> for E
where
E: Executor<NewSvcTask<I, N, S, E, W>> + Clone,
NewSvcTask<I, N, S, E, W>: Future<Output=()>,
NewSvcTask<I, N, S, E, W>: Future<Output = ()>,
S: HttpService<Body>,
W: Watcher<I, S, E>,
{
@@ -111,4 +109,3 @@ where
self.execute(fut)
}
}

View File

@@ -1,10 +1,10 @@
use std::{cmp, io};
use std::marker::Unpin;
use std::{cmp, io};
use bytes::{Buf, Bytes};
use tokio::io::{AsyncRead, AsyncWrite};
use crate::common::{Pin, Poll, task};
use crate::common::{task, Pin, Poll};
/// Combine a buffer with an IO, rewinding reads to use the buffer.
#[derive(Debug)]
@@ -47,7 +47,11 @@ where
self.inner.prepare_uninitialized_buffer(buf)
}
fn poll_read(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
if let Some(mut prefix) = self.pre.take() {
// If there are no remaining bytes, let the bytes get dropped.
if prefix.len() > 0 {
@@ -69,7 +73,11 @@ impl<T> AsyncWrite for Rewind<T>
where
T: AsyncWrite + Unpin,
{
fn poll_write(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.inner).poll_write(cx, buf)
}
@@ -82,7 +90,11 @@ where
}
#[inline]
fn poll_write_buf<B: Buf>(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>, buf: &mut B) -> Poll<io::Result<usize>> {
fn poll_write_buf<B: Buf>(
mut self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
buf: &mut B,
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.inner).poll_write_buf(cx, buf)
}
}
@@ -91,36 +103,27 @@ where
mod tests {
// FIXME: re-implement tests with `async/await`, this import should
// trigger a warning to remind us
use super::Rewind;
use bytes::Bytes;
use tokio::io::AsyncReadExt;
use super::Rewind;
#[tokio::test]
async fn partial_rewind() {
let underlying = [104, 101, 108, 108, 111];
let mock = tokio_test::io::Builder::new()
.read(&underlying)
.build();
let mock = tokio_test::io::Builder::new().read(&underlying).build();
let mut stream = Rewind::new(mock);
// Read off some bytes, ensure we filled o1
let mut buf = [0; 2];
stream
.read_exact(&mut buf)
.await
.expect("read1");
stream.read_exact(&mut buf).await.expect("read1");
// Rewind the stream so that it is as if we never read in the first place.
stream.rewind(Bytes::copy_from_slice(&buf[..]));
let mut buf = [0; 5];
stream
.read_exact(&mut buf)
.await
.expect("read1");
stream.read_exact(&mut buf).await.expect("read1");
// At this point we should have read everything that was in the MockStream
assert_eq!(&buf, &underlying);
@@ -130,26 +133,17 @@ mod tests {
async fn full_rewind() {
let underlying = [104, 101, 108, 108, 111];
let mock = tokio_test::io::Builder::new()
.read(&underlying)
.build();
let mock = tokio_test::io::Builder::new().read(&underlying).build();
let mut stream = Rewind::new(mock);
let mut buf = [0; 5];
stream
.read_exact(&mut buf)
.await
.expect("read1");
stream.read_exact(&mut buf).await.expect("read1");
// Rewind the stream so that it is as if we never read in the first place.
stream.rewind(Bytes::copy_from_slice(&buf[..]));
let mut buf = [0; 5];
stream
.read_exact(&mut buf)
.await
.expect("read1");
stream.read_exact(&mut buf).await.expect("read1");
}
}

View File

@@ -1,6 +1,6 @@
use std::mem;
use super::{Future, Pin, Poll, task};
use super::{task, Future, Pin, Poll};
pub(crate) trait Started: Future {
fn started(&self) -> bool;
@@ -19,7 +19,7 @@ where
// FIXME: allow() required due to `impl Trait` leaking types to this lint
#[allow(missing_debug_implementations)]
pub(crate) struct Lazy<F, R> {
inner: Inner<F, R>
inner: Inner<F, R>,
}
enum Inner<F, R> {
@@ -36,8 +36,7 @@ where
fn started(&self) -> bool {
match self.inner {
Inner::Init(_) => false,
Inner::Fut(_) |
Inner::Empty => true,
Inner::Fut(_) | Inner::Empty => true,
}
}
}
@@ -61,7 +60,7 @@ where
let ret = Pin::new(&mut fut).poll(cx);
self.inner = Inner::Fut(fut);
ret
},
}
_ => unreachable!("lazy state wrong"),
}
}
@@ -69,4 +68,3 @@ where
// The closure `F` is never pinned
impl<F, R: Unpin> Unpin for Lazy<F, R> {}

View File

@@ -1,10 +1,10 @@
macro_rules! ready {
($e:expr) => (
($e:expr) => {
match $e {
::std::task::Poll::Ready(v) => v,
::std::task::Poll::Pending => return ::std::task::Poll::Pending,
}
)
};
}
pub(crate) mod drain;
@@ -14,16 +14,11 @@ mod lazy;
mod never;
pub(crate) mod task;
pub(crate) use self::exec::{BoxSendFuture, Exec};
pub use self::exec::Executor;
pub(crate) use self::exec::{BoxSendFuture, Exec};
pub(crate) use self::lazy::{lazy, Started as Lazy};
pub use self::never::Never;
pub(crate) use self::task::Poll;
// group up types normally needed for `Future`
pub(crate) use std::{
future::Future,
marker::Unpin,
pin::Pin,
};
pub(crate) use std::{future::Future, marker::Unpin, pin::Pin};

View File

@@ -19,4 +19,3 @@ impl Error for Never {
match *self {}
}
}

View File

@@ -1,5 +1,5 @@
pub(crate) use std::task::{Context, Poll};
use super::Never;
pub(crate) use std::task::{Context, Poll};
/// A function to help "yield" a future, such that it is re-scheduled immediately.
///