Use rustfmt to enforce consistent formatting
This change adds a .rustfmt.toml that includes ALL supported settings, 12 of which we have overridden to attempt to cater to our own proclivities. rustfmt is checked in the rust-nightly CI job.
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use {client, server, frame, proto};
|
||||
use frame::Reason;
|
||||
use codec::{RecvError, UserError};
|
||||
use proto::*;
|
||||
use super::*;
|
||||
use {client, frame, proto, server};
|
||||
use codec::{RecvError, UserError};
|
||||
use frame::Reason;
|
||||
use proto::*;
|
||||
|
||||
use http::HeaderMap;
|
||||
|
||||
@@ -11,7 +11,8 @@ use std::marker::PhantomData;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct Recv<B, P>
|
||||
where P: Peer,
|
||||
where
|
||||
P: Peer,
|
||||
{
|
||||
/// Initial window size of remote initiated streams
|
||||
init_window_sz: WindowSize,
|
||||
@@ -54,20 +55,18 @@ struct Indices {
|
||||
}
|
||||
|
||||
impl<B, P> Recv<B, P>
|
||||
where B: Buf,
|
||||
P: Peer,
|
||||
where
|
||||
B: Buf,
|
||||
P: Peer,
|
||||
{
|
||||
pub fn new(config: &Config) -> Self {
|
||||
let next_stream_id = if P::is_server() {
|
||||
1
|
||||
} else {
|
||||
2
|
||||
};
|
||||
let next_stream_id = if P::is_server() { 1 } else { 2 };
|
||||
|
||||
let mut flow = FlowControl::new();
|
||||
|
||||
flow.inc_window(config.init_remote_window_sz)
|
||||
.ok().expect("invalid initial remote window size");
|
||||
.ok()
|
||||
.expect("invalid initial remote window size");
|
||||
flow.assign_capacity(config.init_remote_window_sz);
|
||||
|
||||
Recv {
|
||||
@@ -96,12 +95,14 @@ impl<B, P> Recv<B, P>
|
||||
/// Update state reflecting a new, remotely opened stream
|
||||
///
|
||||
/// Returns the stream state if successful. `None` if refused
|
||||
pub fn open(&mut self, id: StreamId, counts: &mut Counts<P>)
|
||||
-> Result<Option<StreamId>, RecvError>
|
||||
{
|
||||
pub fn open(
|
||||
&mut self,
|
||||
id: StreamId,
|
||||
counts: &mut Counts<P>,
|
||||
) -> Result<Option<StreamId>, RecvError> {
|
||||
assert!(self.refused.is_none());
|
||||
|
||||
try!(self.ensure_can_open(id));
|
||||
self.ensure_can_open(id)?;
|
||||
|
||||
if id < self.next_stream_id {
|
||||
return Err(RecvError::Connection(ProtocolError));
|
||||
@@ -121,12 +122,12 @@ impl<B, P> Recv<B, P>
|
||||
/// Transition the stream state based on receiving headers
|
||||
///
|
||||
/// The caller ensures that the frame represents headers and not trailers.
|
||||
pub fn recv_headers(&mut self,
|
||||
frame: frame::Headers,
|
||||
stream: &mut store::Ptr<B, P>,
|
||||
counts: &mut Counts<P>)
|
||||
-> Result<(), RecvError>
|
||||
{
|
||||
pub fn recv_headers(
|
||||
&mut self,
|
||||
frame: frame::Headers,
|
||||
stream: &mut store::Ptr<B, P>,
|
||||
counts: &mut Counts<P>,
|
||||
) -> Result<(), RecvError> {
|
||||
trace!("opening stream; init_window={}", self.init_window_sz);
|
||||
let is_initial = stream.state.recv_open(frame.is_end_stream())?;
|
||||
|
||||
@@ -159,7 +160,9 @@ impl<B, P> Recv<B, P>
|
||||
let message = P::convert_poll_message(frame)?;
|
||||
|
||||
// Push the frame onto the stream's recv buffer
|
||||
stream.pending_recv.push_back(&mut self.buffer, Event::Headers(message));
|
||||
stream
|
||||
.pending_recv
|
||||
.push_back(&mut self.buffer, Event::Headers(message));
|
||||
stream.notify_recv();
|
||||
|
||||
// Only servers can receive a headers frame that initiates the stream.
|
||||
@@ -172,37 +175,39 @@ impl<B, P> Recv<B, P>
|
||||
}
|
||||
|
||||
/// Transition the stream based on receiving trailers
|
||||
pub fn recv_trailers(&mut self,
|
||||
frame: frame::Headers,
|
||||
stream: &mut store::Ptr<B, P>)
|
||||
-> Result<(), RecvError>
|
||||
{
|
||||
pub fn recv_trailers(
|
||||
&mut self,
|
||||
frame: frame::Headers,
|
||||
stream: &mut store::Ptr<B, P>,
|
||||
) -> Result<(), RecvError> {
|
||||
// Transition the state
|
||||
stream.state.recv_close()?;
|
||||
|
||||
if stream.ensure_content_length_zero().is_err() {
|
||||
return Err(RecvError::Stream {
|
||||
id: stream.id,
|
||||
reason: ProtocolError,
|
||||
});
|
||||
id: stream.id,
|
||||
reason: ProtocolError,
|
||||
});
|
||||
}
|
||||
|
||||
let trailers = frame.into_fields();
|
||||
|
||||
// Push the frame onto the stream's recv buffer
|
||||
stream.pending_recv.push_back(&mut self.buffer, Event::Trailers(trailers));
|
||||
stream
|
||||
.pending_recv
|
||||
.push_back(&mut self.buffer, Event::Trailers(trailers));
|
||||
stream.notify_recv();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Releases capacity back to the connection
|
||||
pub fn release_capacity(&mut self,
|
||||
capacity: WindowSize,
|
||||
stream: &mut store::Ptr<B, P>,
|
||||
task: &mut Option<Task>)
|
||||
-> Result<(), UserError>
|
||||
{
|
||||
pub fn release_capacity(
|
||||
&mut self,
|
||||
capacity: WindowSize,
|
||||
stream: &mut store::Ptr<B, P>,
|
||||
task: &mut Option<Task>,
|
||||
) -> Result<(), UserError> {
|
||||
trace!("release_capacity; size={}", capacity);
|
||||
|
||||
if capacity > stream.in_flight_recv_data {
|
||||
@@ -233,16 +238,18 @@ impl<B, P> Recv<B, P>
|
||||
return false;
|
||||
}
|
||||
|
||||
stream.pending_recv.peek_front(&self.buffer)
|
||||
stream
|
||||
.pending_recv
|
||||
.peek_front(&self.buffer)
|
||||
.map(|event| !event.is_data())
|
||||
.unwrap_or(true)
|
||||
}
|
||||
|
||||
pub fn recv_data(&mut self,
|
||||
frame: frame::Data,
|
||||
stream: &mut store::Ptr<B, P>)
|
||||
-> Result<(), RecvError>
|
||||
{
|
||||
pub fn recv_data(
|
||||
&mut self,
|
||||
frame: frame::Data,
|
||||
stream: &mut store::Ptr<B, P>,
|
||||
) -> Result<(), RecvError> {
|
||||
let sz = frame.payload().len();
|
||||
|
||||
if sz > MAX_WINDOW_SIZE as usize {
|
||||
@@ -258,7 +265,9 @@ impl<B, P> Recv<B, P>
|
||||
}
|
||||
|
||||
trace!("recv_data; size={}; connection={}; stream={}",
|
||||
sz, self.flow.window_size(), stream.recv_flow.window_size());
|
||||
sz,
|
||||
self.flow.window_size(),
|
||||
stream.recv_flow.window_size());
|
||||
|
||||
// Ensure that there is enough capacity on the connection before acting
|
||||
// on the stream.
|
||||
@@ -277,17 +286,17 @@ impl<B, P> Recv<B, P>
|
||||
|
||||
if stream.dec_content_length(frame.payload().len()).is_err() {
|
||||
return Err(RecvError::Stream {
|
||||
id: stream.id,
|
||||
reason: ProtocolError,
|
||||
});
|
||||
id: stream.id,
|
||||
reason: ProtocolError,
|
||||
});
|
||||
}
|
||||
|
||||
if frame.is_end_stream() {
|
||||
if stream.ensure_content_length_zero().is_err() {
|
||||
return Err(RecvError::Stream {
|
||||
id: stream.id,
|
||||
reason: ProtocolError,
|
||||
});
|
||||
id: stream.id,
|
||||
reason: ProtocolError,
|
||||
});
|
||||
}
|
||||
|
||||
if stream.state.recv_close().is_err() {
|
||||
@@ -304,18 +313,20 @@ impl<B, P> Recv<B, P>
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn recv_push_promise(&mut self,
|
||||
frame: frame::PushPromise,
|
||||
send: &Send<B, P>,
|
||||
stream: store::Key,
|
||||
store: &mut Store<B, P>)
|
||||
-> Result<(), RecvError>
|
||||
{
|
||||
pub fn recv_push_promise(
|
||||
&mut self,
|
||||
frame: frame::PushPromise,
|
||||
send: &Send<B, P>,
|
||||
stream: store::Key,
|
||||
store: &mut Store<B, P>,
|
||||
) -> Result<(), RecvError> {
|
||||
// First, make sure that the values are legit
|
||||
self.ensure_can_reserve(frame.promised_id())?;
|
||||
|
||||
// Make sure that the stream state is valid
|
||||
store[stream].state.ensure_recv_open()
|
||||
store[stream]
|
||||
.state
|
||||
.ensure_recv_open()
|
||||
.map_err(|e| e.into_connection_recv_error())?;
|
||||
|
||||
// TODO: Streams in the reserved states do not count towards the concurrency
|
||||
@@ -332,10 +343,9 @@ impl<B, P> Recv<B, P>
|
||||
// TODO: All earlier stream IDs should be implicitly closed.
|
||||
|
||||
// Now, create a new entry for the stream
|
||||
let mut new_stream = Stream::new(
|
||||
frame.promised_id(),
|
||||
send.init_window_sz(),
|
||||
self.init_window_sz);
|
||||
let mut new_stream = Stream::new(frame.promised_id(),
|
||||
send.init_window_sz(),
|
||||
self.init_window_sz);
|
||||
|
||||
new_stream.state.reserve_remote()?;
|
||||
|
||||
@@ -343,8 +353,7 @@ impl<B, P> Recv<B, P>
|
||||
|
||||
{
|
||||
// Store the stream
|
||||
let mut new_stream = store
|
||||
.insert(frame.promised_id(), new_stream);
|
||||
let mut new_stream = store.insert(frame.promised_id(), new_stream);
|
||||
|
||||
ppp.push(&mut new_stream);
|
||||
}
|
||||
@@ -366,9 +375,11 @@ impl<B, P> Recv<B, P>
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn recv_reset(&mut self, frame: frame::Reset, stream: &mut Stream<B, P>)
|
||||
-> Result<(), RecvError>
|
||||
{
|
||||
pub fn recv_reset(
|
||||
&mut self,
|
||||
frame: frame::Reset,
|
||||
stream: &mut Stream<B, P>,
|
||||
) -> Result<(), RecvError> {
|
||||
let err = proto::Error::Proto(frame.reason());
|
||||
|
||||
// Notify the stream
|
||||
@@ -387,9 +398,7 @@ impl<B, P> Recv<B, P>
|
||||
}
|
||||
|
||||
/// Returns true if the remote peer can initiate a stream with the given ID.
|
||||
fn ensure_can_open(&self, id: StreamId)
|
||||
-> Result<(), RecvError>
|
||||
{
|
||||
fn ensure_can_open(&self, id: StreamId) -> Result<(), RecvError> {
|
||||
if !P::is_server() {
|
||||
// Remote is a server and cannot open streams. PushPromise is
|
||||
// registered by reserving, so does not go through this path.
|
||||
@@ -405,9 +414,7 @@ impl<B, P> Recv<B, P>
|
||||
}
|
||||
|
||||
/// Returns true if the remote peer can reserve a stream with the given ID.
|
||||
fn ensure_can_reserve(&self, promised_id: StreamId)
|
||||
-> Result<(), RecvError>
|
||||
{
|
||||
fn ensure_can_reserve(&self, promised_id: StreamId) -> Result<(), RecvError> {
|
||||
// TODO: Are there other rules?
|
||||
if P::is_server() {
|
||||
// The remote is a client and cannot reserve
|
||||
@@ -422,9 +429,12 @@ impl<B, P> Recv<B, P>
|
||||
}
|
||||
|
||||
/// Send any pending refusals.
|
||||
pub fn send_pending_refusal<T>(&mut self, dst: &mut Codec<T, Prioritized<B>>)
|
||||
-> Poll<(), io::Error>
|
||||
where T: AsyncWrite,
|
||||
pub fn send_pending_refusal<T>(
|
||||
&mut self,
|
||||
dst: &mut Codec<T, Prioritized<B>>,
|
||||
) -> Poll<(), io::Error>
|
||||
where
|
||||
T: AsyncWrite,
|
||||
{
|
||||
if let Some(stream_id) = self.refused {
|
||||
try_ready!(dst.poll_ready());
|
||||
@@ -433,7 +443,9 @@ impl<B, P> Recv<B, P>
|
||||
let frame = frame::Reset::new(stream_id, RefusedStream);
|
||||
|
||||
// Buffer the frame
|
||||
dst.buffer(frame.into()).ok().expect("invalid RST_STREAM frame");
|
||||
dst.buffer(frame.into())
|
||||
.ok()
|
||||
.expect("invalid RST_STREAM frame");
|
||||
}
|
||||
|
||||
self.refused = None;
|
||||
@@ -441,11 +453,13 @@ impl<B, P> Recv<B, P>
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
pub fn poll_complete<T>(&mut self,
|
||||
store: &mut Store<B, P>,
|
||||
dst: &mut Codec<T, Prioritized<B>>)
|
||||
-> Poll<(), io::Error>
|
||||
where T: AsyncWrite,
|
||||
pub fn poll_complete<T>(
|
||||
&mut self,
|
||||
store: &mut Store<B, P>,
|
||||
dst: &mut Codec<T, Prioritized<B>>,
|
||||
) -> Poll<(), io::Error>
|
||||
where
|
||||
T: AsyncWrite,
|
||||
{
|
||||
// Send any pending connection level window updates
|
||||
try_ready!(self.send_connection_window_update(dst));
|
||||
@@ -457,9 +471,12 @@ impl<B, P> Recv<B, P>
|
||||
}
|
||||
|
||||
/// Send connection level window update
|
||||
fn send_connection_window_update<T>(&mut self, dst: &mut Codec<T, Prioritized<B>>)
|
||||
-> Poll<(), io::Error>
|
||||
where T: AsyncWrite,
|
||||
fn send_connection_window_update<T>(
|
||||
&mut self,
|
||||
dst: &mut Codec<T, Prioritized<B>>,
|
||||
) -> Poll<(), io::Error>
|
||||
where
|
||||
T: AsyncWrite,
|
||||
{
|
||||
if let Some(incr) = self.flow.unclaimed_capacity() {
|
||||
let frame = frame::WindowUpdate::new(StreamId::zero(), incr);
|
||||
@@ -468,10 +485,15 @@ impl<B, P> Recv<B, P>
|
||||
try_ready!(dst.poll_ready());
|
||||
|
||||
// Buffer the WINDOW_UPDATE frame
|
||||
dst.buffer(frame.into()).ok().expect("invalid WINDOW_UPDATE frame");
|
||||
dst.buffer(frame.into())
|
||||
.ok()
|
||||
.expect("invalid WINDOW_UPDATE frame");
|
||||
|
||||
// Update flow control
|
||||
self.flow.inc_window(incr).ok().expect("unexpected flow control state");
|
||||
self.flow
|
||||
.inc_window(incr)
|
||||
.ok()
|
||||
.expect("unexpected flow control state");
|
||||
}
|
||||
|
||||
Ok(().into())
|
||||
@@ -479,11 +501,13 @@ impl<B, P> Recv<B, P>
|
||||
|
||||
|
||||
/// Send stream level window update
|
||||
pub fn send_stream_window_updates<T>(&mut self,
|
||||
store: &mut Store<B, P>,
|
||||
dst: &mut Codec<T, Prioritized<B>>)
|
||||
-> Poll<(), io::Error>
|
||||
where T: AsyncWrite,
|
||||
pub fn send_stream_window_updates<T>(
|
||||
&mut self,
|
||||
store: &mut Store<B, P>,
|
||||
dst: &mut Codec<T, Prioritized<B>>,
|
||||
) -> Poll<(), io::Error>
|
||||
where
|
||||
T: AsyncWrite,
|
||||
{
|
||||
loop {
|
||||
// Ensure the codec has capacity
|
||||
@@ -507,27 +531,28 @@ impl<B, P> Recv<B, P>
|
||||
let frame = frame::WindowUpdate::new(stream.id, incr);
|
||||
|
||||
// Buffer it
|
||||
dst.buffer(frame.into()).ok().expect("invalid WINDOW_UPDATE frame");
|
||||
dst.buffer(frame.into())
|
||||
.ok()
|
||||
.expect("invalid WINDOW_UPDATE frame");
|
||||
|
||||
// Update flow control
|
||||
stream.recv_flow.inc_window(incr).ok().expect("unexpected flow control state");
|
||||
stream
|
||||
.recv_flow
|
||||
.inc_window(incr)
|
||||
.ok()
|
||||
.expect("unexpected flow control state");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_incoming(&mut self, store: &mut Store<B, P>) -> Option<store::Key> {
|
||||
self.pending_accept.pop(store)
|
||||
.map(|ptr| ptr.key())
|
||||
self.pending_accept.pop(store).map(|ptr| ptr.key())
|
||||
}
|
||||
|
||||
pub fn poll_data(&mut self, stream: &mut Stream<B, P>)
|
||||
-> Poll<Option<Bytes>, proto::Error>
|
||||
{
|
||||
pub fn poll_data(&mut self, stream: &mut Stream<B, P>) -> Poll<Option<Bytes>, proto::Error> {
|
||||
// TODO: Return error when the stream is reset
|
||||
match stream.pending_recv.pop_front(&mut self.buffer) {
|
||||
Some(Event::Data(payload)) => {
|
||||
Ok(Some(payload).into())
|
||||
}
|
||||
Some(Event::Data(payload)) => Ok(Some(payload).into()),
|
||||
Some(event) => {
|
||||
// Frame is trailer
|
||||
stream.pending_recv.push_front(&mut self.buffer, event);
|
||||
@@ -548,13 +573,12 @@ impl<B, P> Recv<B, P>
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll_trailers(&mut self, stream: &mut Stream<B, P>)
|
||||
-> Poll<Option<HeaderMap>, proto::Error>
|
||||
{
|
||||
pub fn poll_trailers(
|
||||
&mut self,
|
||||
stream: &mut Stream<B, P>,
|
||||
) -> Poll<Option<HeaderMap>, proto::Error> {
|
||||
match stream.pending_recv.pop_front(&mut self.buffer) {
|
||||
Some(Event::Trailers(trailers)) => {
|
||||
Ok(Some(trailers).into())
|
||||
}
|
||||
Some(Event::Trailers(trailers)) => Ok(Some(trailers).into()),
|
||||
Some(_) => {
|
||||
// TODO: This is a user error. `poll_trailers` was called before
|
||||
// the entire set of data frames have been consumed. What should
|
||||
@@ -576,12 +600,11 @@ impl<B, P> Recv<B, P>
|
||||
}
|
||||
|
||||
impl<B> Recv<B, server::Peer>
|
||||
where B: Buf,
|
||||
where
|
||||
B: Buf,
|
||||
{
|
||||
/// TODO: Should this fn return `Result`?
|
||||
pub fn take_request(&mut self, stream: &mut store::Ptr<B, server::Peer>)
|
||||
-> Request<()>
|
||||
{
|
||||
pub fn take_request(&mut self, stream: &mut store::Ptr<B, server::Peer>) -> Request<()> {
|
||||
match stream.pending_recv.pop_front(&mut self.buffer) {
|
||||
Some(Event::Headers(request)) => request,
|
||||
_ => panic!(),
|
||||
@@ -590,16 +613,17 @@ impl<B> Recv<B, server::Peer>
|
||||
}
|
||||
|
||||
impl<B> Recv<B, client::Peer>
|
||||
where B: Buf,
|
||||
where
|
||||
B: Buf,
|
||||
{
|
||||
pub fn poll_response(&mut self, stream: &mut store::Ptr<B, client::Peer>)
|
||||
-> Poll<Response<()>, proto::Error> {
|
||||
pub fn poll_response(
|
||||
&mut self,
|
||||
stream: &mut store::Ptr<B, client::Peer>,
|
||||
) -> Poll<Response<()>, proto::Error> {
|
||||
// If the buffer is not empty, then the first frame must be a HEADERS
|
||||
// frame or the user violated the contract.
|
||||
match stream.pending_recv.pop_front(&mut self.buffer) {
|
||||
Some(Event::Headers(response)) => {
|
||||
Ok(response.into())
|
||||
}
|
||||
Some(Event::Headers(response)) => Ok(response.into()),
|
||||
Some(_) => unimplemented!(),
|
||||
None => {
|
||||
stream.state.ensure_recv_open()?;
|
||||
|
||||
Reference in New Issue
Block a user