add stream and sink proxy macros

This commit is contained in:
Oliver Gould
2017-07-24 16:40:29 +00:00
parent 9e4464e385
commit 9a95fc5eef
6 changed files with 51 additions and 130 deletions

View File

@@ -6,20 +6,6 @@ pub trait ControlPing {
fn take_pong(&mut self) -> Option<PingPayload>;
}
// macro_rules! proxy_control_ping {
// ($outer:ident) => (
// impl<T: ControlPing> ControlPing for $outer<T> {
// fn start_ping(&mut self, body: PingPayload) -> StartSend<PingPayload, ConnectionError> {
// self.inner.start_ping(body)
// }
// fn take_pong(&mut self) -> Option<PingPayload> {
// self.inner.take_pong()
// }
// }
// )
// }
macro_rules! proxy_control_ping {
($struct:ident $(, $targs:ident)*) => (
impl<T: ControlPing$(, $targs)*> ControlPing for $struct<T$(, $targs)*> {

View File

@@ -19,6 +19,48 @@ mod control_settings;
#[macro_use]
mod control_streams;
macro_rules! proxy_stream {
($struct:ident $(, $targs:ident)*) => (
impl<T: Stream$(, $targs)*> Stream for $struct<T$(, $targs)*> {
type Item = T::Item;
type Error = T::Error;
fn poll(&mut self) -> Poll<Option<T::Item>, T::Error> {
self.inner.poll()
}
}
)
}
macro_rules! proxy_sink {
($struct:ident $(, $targs:ident)*) => (
impl<T, U$(, $targs)*> Sink for $struct<T$(, $targs)*>
where T: Sink<SinkItem = frame::Frame<U>, SinkError = ConnectionError>
{
type SinkItem = frame::Frame<U>;
type SinkError = ConnectionError;
fn start_send(&mut self, it: T::SinkItem) -> StartSend<T::SinkItem, T::SinkError> {
self.inner.start_send(it)
}
fn poll_complete(&mut self) -> Poll<(), T::SinkError> {
self.inner.poll_complete()
}
}
)
}
macro_rules! proxy_ready_sink {
($struct:ident $(, $targs:ident)*$(; $constraint:ident)*) => (
impl<T, U$(, $targs)*> ReadySink for $struct<T$(, $targs)*>
where T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,
T: ReadySink $(+ $constraint)*
{
fn poll_ready(&mut self) -> Poll<(), T::SinkError> {
self.inner.poll_ready()
}
}
)
}
use self::apply_settings::ApplySettings;
use self::control_flow::ControlFlow;
use self::control_ping::ControlPing;

View File

@@ -51,35 +51,9 @@ impl<T> Stream for StreamRecvClose<T>
}
}
// Proxy.
impl<T, U> Sink for StreamRecvClose<T>
where T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,
T: ControlStreams,
{
type SinkItem = Frame<U>;
type SinkError = ConnectionError;
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Frame<U>, ConnectionError> {
self.inner.start_send(item)
}
fn poll_complete(&mut self) -> Poll<(), ConnectionError> {
self.inner.poll_complete()
}
}
// Proxy.
impl<T, U> ReadySink for StreamRecvClose<T>
where T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,
T: ReadySink,
T: ControlStreams,
{
fn poll_ready(&mut self) -> Poll<(), ConnectionError> {
self.inner.poll_ready()
}
}
proxy_apply_settings!(StreamRecvClose);
proxy_control_flow!(StreamRecvClose);
proxy_control_streams!(StreamRecvClose);
proxy_control_ping!(StreamRecvClose);
proxy_sink!(StreamRecvClose);
proxy_ready_sink!(StreamRecvClose);

View File

@@ -19,19 +19,6 @@ impl<T, U> StreamSendClose<T>
}
}
/// Proxy.
impl<T> Stream for StreamSendClose<T>
where T: Stream<Item = Frame, Error = ConnectionError>,
T: ControlStreams,
{
type Item = Frame;
type Error = ConnectionError;
fn poll(&mut self) -> Poll<Option<Frame>, ConnectionError> {
self.inner.poll()
}
}
/// Tracks END_STREAM frames sent from the local peer.
impl<T, U> Sink for StreamSendClose<T>
where T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,
@@ -63,18 +50,9 @@ impl<T, U> Sink for StreamSendClose<T>
}
}
/// Proxy.
impl<T, U> ReadySink for StreamSendClose<T>
where T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,
T: ReadySink,
T: ControlStreams,
{
fn poll_ready(&mut self) -> Poll<(), ConnectionError> {
self.inner.poll_ready()
}
}
proxy_apply_settings!(StreamSendClose);
proxy_control_flow!(StreamSendClose);
proxy_control_streams!(StreamSendClose);
proxy_control_ping!(StreamSendClose);
proxy_stream!(StreamSendClose);
proxy_ready_sink!(StreamSendClose; ControlStreams);

View File

@@ -45,20 +45,6 @@ impl<T: ApplySettings> ApplySettings for StreamSendOpen<T> {
}
}
/// Proxy.
impl<T> Stream for StreamSendOpen<T>
where T: Stream<Item = Frame, Error = ConnectionError>,
T: ControlStreams,
{
type Item = Frame;
type Error = ConnectionError;
fn poll(&mut self) -> Poll<Option<Frame>, ConnectionError> {
trace!("poll");
self.inner.poll()
}
}
/// Helper.
impl<T: ControlStreams> StreamSendOpen<T> {
fn check_not_reset(&self, id: StreamId) -> Result<(), ConnectionError> {
@@ -144,18 +130,8 @@ impl<T, U> Sink for StreamSendOpen<T>
}
}
/// Proxy.
impl<T, U> ReadySink for StreamSendOpen<T>
where T: Stream<Item = Frame, Error = ConnectionError>,
T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,
T: ControlStreams,
T: ReadySink,
{
fn poll_ready(&mut self) -> Poll<(), ConnectionError> {
self.inner.poll_ready()
}
}
proxy_control_flow!(StreamSendOpen);
proxy_control_streams!(StreamSendOpen);
proxy_control_ping!(StreamSendOpen);
proxy_stream!(StreamSendOpen);
proxy_ready_sink!(StreamSendOpen; ControlStreams);

View File

@@ -279,43 +279,8 @@ impl<T, P: Peer> ControlStreams for StreamStates<T, P> {
}
}
/// Proxy.
impl<T, P> Stream for StreamStates<T, P>
where T: Stream<Item = Frame, Error = ConnectionError>,
{
type Item = Frame;
type Error = ConnectionError;
fn poll(&mut self) -> Poll<Option<Frame>, ConnectionError> {
self.inner.poll()
}
}
/// Proxy.
impl<T, P, U> Sink for StreamStates<T, P>
where T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,
{
type SinkItem = Frame<U>;
type SinkError = ConnectionError;
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Frame<U>, ConnectionError> {
self.inner.start_send(item)
}
fn poll_complete(&mut self) -> Poll<(), ConnectionError> {
self.inner.poll_complete()
}
}
/// Proxy.
impl<T, P, U> ReadySink for StreamStates<T, P>
where T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,
T: ReadySink,
{
fn poll_ready(&mut self) -> Poll<(), ConnectionError> {
self.inner.poll_ready()
}
}
proxy_apply_settings!(StreamStates, P);
proxy_control_ping!(StreamStates, P);
proxy_stream!(StreamStates, P);
proxy_sink!(StreamStates, P);
proxy_ready_sink!(StreamStates, P);