diff --git a/src/lib.rs b/src/lib.rs index 21791034..8542195f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,7 @@ enum InboundProtocolState { enum OutboundProtocolState { GotFunctionNeedSubstream(OutboundProtocolFn), - GotSubstreamNeedFunction(OutboundSubstream), + GotFunctionRequestedSubstream(OutboundProtocolFn), Executing(Protocol), } @@ -45,11 +45,6 @@ enum ProtocolState { pub struct NMessageHandler { state: ProtocolState, - - // TODO: See if it can be included in OutboundProtocolState. - // Or it can be inferred from OutboundProtocolState current variant. - substream_request: Option>, - info: &'static [u8], } @@ -57,7 +52,6 @@ impl NMessageHandler Self { Self { state: ProtocolState::None, - substream_request: None, info, } } @@ -171,18 +165,17 @@ where _: Self::OutboundOpenInfo, ) { match mem::replace(&mut self.state, ProtocolState::Poisoned) { - ProtocolState::None => { - self.state = ProtocolState::Outbound( - OutboundProtocolState::GotSubstreamNeedFunction(substream), - ); - } - ProtocolState::Outbound(OutboundProtocolState::GotFunctionNeedSubstream( + ProtocolState::Outbound(OutboundProtocolState::GotFunctionRequestedSubstream( protocol_fn, )) => { self.state = ProtocolState::Outbound(OutboundProtocolState::Executing( protocol_fn(substream), )); } + ProtocolState::None + | ProtocolState::Outbound(OutboundProtocolState::GotFunctionNeedSubstream(_)) => { + panic!("Illegal state, receiving substream means it was requested."); + } ProtocolState::Outbound(_) | ProtocolState::Done => { panic!("Illegal state, substream is already present."); } @@ -223,22 +216,12 @@ where } } ProtocolInEvent::ExecuteOutbound(protocol_fn) => { - self.substream_request = - Some(SubstreamProtocol::new(NMessageProtocol::new(self.info), ())); - match mem::replace(&mut self.state, ProtocolState::Poisoned) { ProtocolState::None => { self.state = ProtocolState::Outbound( OutboundProtocolState::GotFunctionNeedSubstream(protocol_fn), ); } - ProtocolState::Outbound(OutboundProtocolState::GotSubstreamNeedFunction( - substream, - )) => { - self.state = ProtocolState::Outbound(OutboundProtocolState::Executing( - protocol_fn(substream), - )); - } ProtocolState::Outbound(_) | ProtocolState::Done => { panic!("Illegal state, protocol fn is already present."); } @@ -276,10 +259,6 @@ where Self::Error, >, > { - if let Some(protocol) = self.substream_request.take() { - return Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol }); - } - match mem::replace(&mut self.state, ProtocolState::Poisoned) { ProtocolState::Inbound(InboundProtocolState::Executing(mut protocol)) => match protocol .poll_unpin(cx) @@ -322,6 +301,14 @@ where } } } + ProtocolState::Outbound(OutboundProtocolState::GotFunctionNeedSubstream(protocol)) => { + self.state = ProtocolState::Outbound( + OutboundProtocolState::GotFunctionRequestedSubstream(protocol), + ); + Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { + protocol: SubstreamProtocol::new(NMessageProtocol::new(self.info), ()), + }) + } ProtocolState::Poisoned => { unreachable!("Protocol is poisoned (transient state)") }