Reduce number of variants in out events by using Result

This commit is contained in:
Franck Royer 2021-01-19 11:32:12 +11:00
parent 827fe5d39e
commit c44aa3e035
No known key found for this signature in database
GPG key ID: A82ED75A8DFC50A4

View file

@ -105,12 +105,9 @@ pub enum ProtocolInEvent<I, O, E> {
ExecuteOutbound(OutboundProtocolFn<O, E>), ExecuteOutbound(OutboundProtocolFn<O, E>),
} }
// TODO: Remove Finished/Failed and just wrap a Result
pub enum ProtocolOutEvent<I, O, E> { pub enum ProtocolOutEvent<I, O, E> {
InboundFinished(I), Inbound(Result<I, E>),
OutboundFinished(O), Outbound(Result<O, E>),
InboundFailed(E),
OutboundFailed(E),
} }
impl<TInboundOut, TOutboundOut, TErr> ProtocolsHandler impl<TInboundOut, TOutboundOut, TErr> ProtocolsHandler
@ -264,17 +261,11 @@ where
ProtocolState::Inbound(InboundProtocolState::Executing(mut protocol)) => match protocol ProtocolState::Inbound(InboundProtocolState::Executing(mut protocol)) => match protocol
.poll_unpin(cx) .poll_unpin(cx)
{ {
Poll::Ready(Ok(value)) => { Poll::Ready(res) => {
self.state = ProtocolState::Done; self.state = ProtocolState::Done;
Poll::Ready(ProtocolsHandlerEvent::Custom( Poll::Ready(ProtocolsHandlerEvent::Custom(ProtocolOutEvent::Inbound(
ProtocolOutEvent::InboundFinished(value), res,
)) )))
}
Poll::Ready(Err(e)) => {
self.state = ProtocolState::Done;
Poll::Ready(ProtocolsHandlerEvent::Custom(
ProtocolOutEvent::InboundFailed(e),
))
} }
Poll::Pending => { Poll::Pending => {
self.state = ProtocolState::Inbound(InboundProtocolState::Executing(protocol)); self.state = ProtocolState::Inbound(InboundProtocolState::Executing(protocol));
@ -283,17 +274,11 @@ where
}, },
ProtocolState::Outbound(OutboundProtocolState::Executing(mut protocol)) => { ProtocolState::Outbound(OutboundProtocolState::Executing(mut protocol)) => {
match protocol.poll_unpin(cx) { match protocol.poll_unpin(cx) {
Poll::Ready(Ok(value)) => { Poll::Ready(res) => {
self.state = ProtocolState::Done; self.state = ProtocolState::Done;
Poll::Ready(ProtocolsHandlerEvent::Custom( Poll::Ready(ProtocolsHandlerEvent::Custom(ProtocolOutEvent::Outbound(
ProtocolOutEvent::OutboundFinished(value), res,
)) )))
}
Poll::Ready(Err(e)) => {
self.state = ProtocolState::Done;
Poll::Ready(ProtocolsHandlerEvent::Custom(
ProtocolOutEvent::OutboundFailed(e),
))
} }
Poll::Pending => { Poll::Pending => {
self.state = self.state =
@ -382,10 +367,8 @@ impl<I, O, E> NMessageBehaviour<I, O, E> {
#[derive(Clone)] #[derive(Clone)]
pub enum BehaviourOutEvent<I, O, E> { pub enum BehaviourOutEvent<I, O, E> {
InboundFinished(PeerId, I), Inbound(PeerId, Result<I, E>),
OutboundFinished(PeerId, O), Outbound(PeerId, Result<O, E>),
InboundFailed(PeerId, E),
OutboundFailed(PeerId, E),
} }
impl<I, O, E> NetworkBehaviour for NMessageBehaviour<I, O, E> impl<I, O, E> NetworkBehaviour for NMessageBehaviour<I, O, E>
@ -460,14 +443,8 @@ where
if let Some((peer, event)) = self.protocol_out_events.pop_front() { if let Some((peer, event)) = self.protocol_out_events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(match event { return Poll::Ready(NetworkBehaviourAction::GenerateEvent(match event {
ProtocolOutEvent::InboundFinished(event) => { ProtocolOutEvent::Inbound(res) => BehaviourOutEvent::Inbound(peer, res),
BehaviourOutEvent::InboundFinished(peer, event) ProtocolOutEvent::Outbound(res) => BehaviourOutEvent::Outbound(peer, res),
}
ProtocolOutEvent::OutboundFinished(event) => {
BehaviourOutEvent::OutboundFinished(peer, event)
}
ProtocolOutEvent::InboundFailed(e) => BehaviourOutEvent::InboundFailed(peer, e),
ProtocolOutEvent::OutboundFailed(e) => BehaviourOutEvent::OutboundFailed(peer, e),
})); }));
} }
@ -519,10 +496,11 @@ mod tests {
impl From<BehaviourOutEvent<BobResult, AliceResult, anyhow::Error>> for MyOutEvent { impl From<BehaviourOutEvent<BobResult, AliceResult, anyhow::Error>> for MyOutEvent {
fn from(event: BehaviourOutEvent<BobResult, AliceResult, Error>) -> Self { fn from(event: BehaviourOutEvent<BobResult, AliceResult, Error>) -> Self {
match event { match event {
BehaviourOutEvent::InboundFinished(_, bob) => MyOutEvent::Bob(bob), BehaviourOutEvent::Inbound(_, Ok(bob)) => MyOutEvent::Bob(bob),
BehaviourOutEvent::OutboundFinished(_, alice) => MyOutEvent::Alice(alice), BehaviourOutEvent::Outbound(_, Ok(alice)) => MyOutEvent::Alice(alice),
BehaviourOutEvent::InboundFailed(_, e) BehaviourOutEvent::Inbound(_, Err(e)) | BehaviourOutEvent::Outbound(_, Err(e)) => {
| BehaviourOutEvent::OutboundFailed(_, e) => MyOutEvent::Failed(e), MyOutEvent::Failed(e)
}
} }
} }
} }