From c44aa3e0351c19b6c60688078a9a627c35d56b6e Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Tue, 19 Jan 2021 11:32:12 +1100 Subject: [PATCH] Reduce number of variants in out events by using `Result` --- src/lib.rs | 60 +++++++++++++++++------------------------------------- 1 file changed, 19 insertions(+), 41 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index feac164e..75731a4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,12 +105,9 @@ pub enum ProtocolInEvent { ExecuteOutbound(OutboundProtocolFn), } -// TODO: Remove Finished/Failed and just wrap a Result pub enum ProtocolOutEvent { - InboundFinished(I), - OutboundFinished(O), - InboundFailed(E), - OutboundFailed(E), + Inbound(Result), + Outbound(Result), } impl ProtocolsHandler @@ -264,17 +261,11 @@ where ProtocolState::Inbound(InboundProtocolState::Executing(mut protocol)) => match protocol .poll_unpin(cx) { - Poll::Ready(Ok(value)) => { + Poll::Ready(res) => { self.state = ProtocolState::Done; - Poll::Ready(ProtocolsHandlerEvent::Custom( - ProtocolOutEvent::InboundFinished(value), - )) - } - Poll::Ready(Err(e)) => { - self.state = ProtocolState::Done; - Poll::Ready(ProtocolsHandlerEvent::Custom( - ProtocolOutEvent::InboundFailed(e), - )) + Poll::Ready(ProtocolsHandlerEvent::Custom(ProtocolOutEvent::Inbound( + res, + ))) } Poll::Pending => { self.state = ProtocolState::Inbound(InboundProtocolState::Executing(protocol)); @@ -283,17 +274,11 @@ where }, ProtocolState::Outbound(OutboundProtocolState::Executing(mut protocol)) => { match protocol.poll_unpin(cx) { - Poll::Ready(Ok(value)) => { + Poll::Ready(res) => { self.state = ProtocolState::Done; - Poll::Ready(ProtocolsHandlerEvent::Custom( - ProtocolOutEvent::OutboundFinished(value), - )) - } - Poll::Ready(Err(e)) => { - self.state = ProtocolState::Done; - Poll::Ready(ProtocolsHandlerEvent::Custom( - ProtocolOutEvent::OutboundFailed(e), - )) + Poll::Ready(ProtocolsHandlerEvent::Custom(ProtocolOutEvent::Outbound( + res, + ))) } Poll::Pending => { self.state = @@ -382,10 +367,8 @@ impl NMessageBehaviour { #[derive(Clone)] pub enum BehaviourOutEvent { - InboundFinished(PeerId, I), - OutboundFinished(PeerId, O), - InboundFailed(PeerId, E), - OutboundFailed(PeerId, E), + Inbound(PeerId, Result), + Outbound(PeerId, Result), } impl NetworkBehaviour for NMessageBehaviour @@ -460,14 +443,8 @@ where if let Some((peer, event)) = self.protocol_out_events.pop_front() { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(match event { - ProtocolOutEvent::InboundFinished(event) => { - BehaviourOutEvent::InboundFinished(peer, event) - } - ProtocolOutEvent::OutboundFinished(event) => { - BehaviourOutEvent::OutboundFinished(peer, event) - } - ProtocolOutEvent::InboundFailed(e) => BehaviourOutEvent::InboundFailed(peer, e), - ProtocolOutEvent::OutboundFailed(e) => BehaviourOutEvent::OutboundFailed(peer, e), + ProtocolOutEvent::Inbound(res) => BehaviourOutEvent::Inbound(peer, res), + ProtocolOutEvent::Outbound(res) => BehaviourOutEvent::Outbound(peer, res), })); } @@ -519,10 +496,11 @@ mod tests { impl From> for MyOutEvent { fn from(event: BehaviourOutEvent) -> Self { match event { - BehaviourOutEvent::InboundFinished(_, bob) => MyOutEvent::Bob(bob), - BehaviourOutEvent::OutboundFinished(_, alice) => MyOutEvent::Alice(alice), - BehaviourOutEvent::InboundFailed(_, e) - | BehaviourOutEvent::OutboundFailed(_, e) => MyOutEvent::Failed(e), + BehaviourOutEvent::Inbound(_, Ok(bob)) => MyOutEvent::Bob(bob), + BehaviourOutEvent::Outbound(_, Ok(alice)) => MyOutEvent::Alice(alice), + BehaviourOutEvent::Inbound(_, Err(e)) | BehaviourOutEvent::Outbound(_, Err(e)) => { + MyOutEvent::Failed(e) + } } } }