From 9a95fc5eef9b88df6b6b095aaca81aeeb470d01e Mon Sep 17 00:00:00 2001 From: Oliver Gould Date: Mon, 24 Jul 2017 16:40:29 +0000 Subject: [PATCH] add stream and sink proxy macros --- src/proto/control_ping.rs | 14 ------------ src/proto/mod.rs | 42 ++++++++++++++++++++++++++++++++++ src/proto/stream_recv_close.rs | 30 ++---------------------- src/proto/stream_send_close.rs | 26 ++------------------- src/proto/stream_send_open.rs | 28 ++--------------------- src/proto/stream_states.rs | 41 +++------------------------------ 6 files changed, 51 insertions(+), 130 deletions(-) diff --git a/src/proto/control_ping.rs b/src/proto/control_ping.rs index 907231a..13e2ba8 100644 --- a/src/proto/control_ping.rs +++ b/src/proto/control_ping.rs @@ -6,20 +6,6 @@ pub trait ControlPing { fn take_pong(&mut self) -> Option; } -// macro_rules! proxy_control_ping { -// ($outer:ident) => ( -// impl ControlPing for $outer { -// fn start_ping(&mut self, body: PingPayload) -> StartSend { -// self.inner.start_ping(body) -// } - -// fn take_pong(&mut self) -> Option { -// self.inner.take_pong() -// } -// } -// ) -// } - macro_rules! proxy_control_ping { ($struct:ident $(, $targs:ident)*) => ( impl ControlPing for $struct { diff --git a/src/proto/mod.rs b/src/proto/mod.rs index ecfa346..025a822 100644 --- a/src/proto/mod.rs +++ b/src/proto/mod.rs @@ -19,6 +19,48 @@ mod control_settings; #[macro_use] mod control_streams; +macro_rules! proxy_stream { + ($struct:ident $(, $targs:ident)*) => ( + impl Stream for $struct { + type Item = T::Item; + type Error = T::Error; + fn poll(&mut self) -> Poll, T::Error> { + self.inner.poll() + } + } + ) +} + +macro_rules! proxy_sink { + ($struct:ident $(, $targs:ident)*) => ( + impl Sink for $struct + where T: Sink, SinkError = ConnectionError> + { + type SinkItem = frame::Frame; + type SinkError = ConnectionError; + fn start_send(&mut self, it: T::SinkItem) -> StartSend { + 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 ReadySink for $struct + where T: Sink, 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; diff --git a/src/proto/stream_recv_close.rs b/src/proto/stream_recv_close.rs index 41839c8..9a8c586 100644 --- a/src/proto/stream_recv_close.rs +++ b/src/proto/stream_recv_close.rs @@ -51,35 +51,9 @@ impl Stream for StreamRecvClose } } -// Proxy. -impl Sink for StreamRecvClose - where T: Sink, SinkError = ConnectionError>, - T: ControlStreams, -{ - type SinkItem = Frame; - type SinkError = ConnectionError; - - fn start_send(&mut self, item: Self::SinkItem) -> StartSend, ConnectionError> { - self.inner.start_send(item) - } - - fn poll_complete(&mut self) -> Poll<(), ConnectionError> { - self.inner.poll_complete() - } -} - -// Proxy. -impl ReadySink for StreamRecvClose - where T: Sink, 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); diff --git a/src/proto/stream_send_close.rs b/src/proto/stream_send_close.rs index bc75757..c2094b1 100644 --- a/src/proto/stream_send_close.rs +++ b/src/proto/stream_send_close.rs @@ -19,19 +19,6 @@ impl StreamSendClose } } -/// Proxy. -impl Stream for StreamSendClose - where T: Stream, - T: ControlStreams, -{ - type Item = Frame; - type Error = ConnectionError; - - fn poll(&mut self) -> Poll, ConnectionError> { - self.inner.poll() - } -} - /// Tracks END_STREAM frames sent from the local peer. impl Sink for StreamSendClose where T: Sink, SinkError = ConnectionError>, @@ -63,18 +50,9 @@ impl Sink for StreamSendClose } } -/// Proxy. -impl ReadySink for StreamSendClose - where T: Sink, 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); diff --git a/src/proto/stream_send_open.rs b/src/proto/stream_send_open.rs index d4046d5..20e4395 100644 --- a/src/proto/stream_send_open.rs +++ b/src/proto/stream_send_open.rs @@ -45,20 +45,6 @@ impl ApplySettings for StreamSendOpen { } } -/// Proxy. -impl Stream for StreamSendOpen - where T: Stream, - T: ControlStreams, -{ - type Item = Frame; - type Error = ConnectionError; - - fn poll(&mut self) -> Poll, ConnectionError> { - trace!("poll"); - self.inner.poll() - } -} - /// Helper. impl StreamSendOpen { fn check_not_reset(&self, id: StreamId) -> Result<(), ConnectionError> { @@ -144,18 +130,8 @@ impl Sink for StreamSendOpen } } -/// Proxy. -impl ReadySink for StreamSendOpen - where T: Stream, - T: Sink, 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); diff --git a/src/proto/stream_states.rs b/src/proto/stream_states.rs index 7feb908..b2e7e42 100644 --- a/src/proto/stream_states.rs +++ b/src/proto/stream_states.rs @@ -279,43 +279,8 @@ impl ControlStreams for StreamStates { } } -/// Proxy. -impl Stream for StreamStates - where T: Stream, -{ - type Item = Frame; - type Error = ConnectionError; - - fn poll(&mut self) -> Poll, ConnectionError> { - self.inner.poll() - } -} - -/// Proxy. -impl Sink for StreamStates - where T: Sink, SinkError = ConnectionError>, -{ - type SinkItem = Frame; - type SinkError = ConnectionError; - - fn start_send(&mut self, item: Self::SinkItem) -> StartSend, ConnectionError> { - self.inner.start_send(item) - } - - fn poll_complete(&mut self) -> Poll<(), ConnectionError> { - self.inner.poll_complete() - } -} - -/// Proxy. -impl ReadySink for StreamStates - where T: Sink, 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);