Add an error associated type to ProtocolsHandler (#795)

This commit is contained in:
Pierre Krieger
2018-12-28 15:11:35 +01:00
committed by GitHub
parent d10cafa804
commit 7798e23e78
13 changed files with 36 additions and 27 deletions

View File

@ -27,7 +27,7 @@ use crate::{
}
};
use futures::prelude::*;
use std::{io, marker::PhantomData};
use std::marker::PhantomData;
use tokio_io::{AsyncRead, AsyncWrite};
use void::Void;
@ -53,6 +53,7 @@ where
{
type InEvent = Void;
type OutEvent = Void;
type Error = Void;
type Substream = TSubstream;
type InboundProtocol = DeniedUpgrade;
type OutboundProtocol = DeniedUpgrade;
@ -97,7 +98,7 @@ where
&mut self,
) -> Poll<
Option<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>>,
io::Error,
Void,
> {
if self.shutting_down {
Ok(Async::Ready(None))

View File

@ -26,7 +26,7 @@ use crate::{
}
};
use futures::prelude::*;
use std::{io, marker::PhantomData};
use std::marker::PhantomData;
/// Wrapper around a protocol handler that turns the input event into something else.
pub struct MapInEvent<TProtoHandler, TNewIn, TMap> {
@ -54,6 +54,7 @@ where
{
type InEvent = TNewIn;
type OutEvent = TProtoHandler::OutEvent;
type Error = TProtoHandler::Error;
type Substream = TProtoHandler::Substream;
type InboundProtocol = TProtoHandler::InboundProtocol;
type OutboundProtocol = TProtoHandler::OutboundProtocol;
@ -108,7 +109,7 @@ where
&mut self,
) -> Poll<
Option<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>>,
io::Error,
Self::Error,
> {
self.inner.poll()
}

View File

@ -26,7 +26,6 @@ use crate::{
}
};
use futures::prelude::*;
use std::io;
/// Wrapper around a protocol handler that turns the output event into something else.
pub struct MapOutEvent<TProtoHandler, TMap> {
@ -52,6 +51,7 @@ where
{
type InEvent = TProtoHandler::InEvent;
type OutEvent = TNewOut;
type Error = TProtoHandler::Error;
type Substream = TProtoHandler::Substream;
type InboundProtocol = TProtoHandler::InboundProtocol;
type OutboundProtocol = TProtoHandler::OutboundProtocol;
@ -104,7 +104,7 @@ where
&mut self,
) -> Poll<
Option<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>>,
io::Error,
Self::Error,
> {
Ok(self.inner.poll()?.map(|ev| {
ev.map(|ev| match ev {

View File

@ -24,7 +24,7 @@ use crate::upgrade::{
UpgradeError,
};
use futures::prelude::*;
use std::{error, fmt, io, time::Duration};
use std::{error, fmt, time::Duration};
use tokio_io::{AsyncRead, AsyncWrite};
pub use self::dummy::DummyProtocolsHandler;
@ -89,6 +89,8 @@ pub trait ProtocolsHandler {
type InEvent;
/// Custom event that can be produced by the handler and that will be returned to the outside.
type OutEvent;
/// Error that can happen when polling.
type Error: error::Error;
/// The type of the substream that contains the raw data.
type Substream: AsyncRead + AsyncWrite;
/// The upgrade for the protocol or protocols handled by this handler.
@ -144,7 +146,7 @@ pub trait ProtocolsHandler {
/// > **Note**: If this handler is combined with other handlers, as soon as `poll()` returns
/// > `Ok(Async::Ready(None))`, all the other handlers will receive a call to
/// > `shutdown()` and will eventually be closed and destroyed.
fn poll(&mut self) -> Poll<Option<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>>, io::Error>;
fn poll(&mut self) -> Poll<Option<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>>, Self::Error>;
/// Adds a closure that turns the input event into something else.
#[inline]

View File

@ -29,7 +29,7 @@ use crate::{
}
};
use futures::prelude::*;
use std::{io, time::Duration};
use std::time::Duration;
use tokio_timer::Timeout;
/// Prototype for a `NodeHandlerWrapper`.
@ -123,7 +123,7 @@ where
{
type InEvent = TProtoHandler::InEvent;
type OutEvent = TProtoHandler::OutEvent;
type Error = io::Error; // TODO: better error type
type Error = TProtoHandler::Error;
type Substream = TProtoHandler::Substream;
// The first element of the tuple is the unique upgrade identifier
// (see `unique_dial_upgrade_id`).
@ -198,7 +198,7 @@ where
self.handler.shutdown();
}
fn poll(&mut self) -> Poll<Option<NodeHandlerEvent<Self::OutboundOpenInfo, Self::OutEvent>>, io::Error> {
fn poll(&mut self) -> Poll<Option<NodeHandlerEvent<Self::OutboundOpenInfo, Self::OutEvent>>, Self::Error> {
// Continue negotiation of newly-opened substreams on the listening side.
// We remove each element from `negotiating_in` one by one and add them back if not ready.
for n in (0..self.negotiating_in.len()).rev() {

View File

@ -31,7 +31,6 @@ use crate::{
}
};
use futures::prelude::*;
use std::io;
use tokio_io::{AsyncRead, AsyncWrite};
/// Implementation of `ProtocolsHandler` that combines two protocols into one.
@ -65,6 +64,7 @@ where
{
type InEvent = EitherOutput<TProto1::InEvent, TProto2::InEvent>;
type OutEvent = EitherOutput<TProto1::OutEvent, TProto2::OutEvent>;
type Error = EitherError<TProto1::Error, TProto2::Error>;
type Substream = TSubstream;
type InboundProtocol = SelectUpgrade<TProto1::InboundProtocol, TProto2::InboundProtocol>;
type OutboundProtocol = EitherUpgrade<TProto1::OutboundProtocol, TProto2::OutboundProtocol>;
@ -161,8 +161,8 @@ where
self.proto2.shutdown();
}
fn poll(&mut self) -> Poll<Option<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>>, io::Error> {
match self.proto1.poll()? {
fn poll(&mut self) -> Poll<Option<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>>, Self::Error> {
match self.proto1.poll().map_err(EitherError::A)? {
Async::Ready(Some(ProtocolsHandlerEvent::Custom(event))) => {
return Ok(Async::Ready(Some(ProtocolsHandlerEvent::Custom(EitherOutput::First(event)))));
},
@ -176,7 +176,7 @@ where
Async::NotReady => ()
};
match self.proto2.poll()? {
match self.proto2.poll().map_err(EitherError::B)? {
Async::Ready(Some(ProtocolsHandlerEvent::Custom(event))) => {
return Ok(Async::Ready(Some(ProtocolsHandlerEvent::Custom(EitherOutput::Second(event)))));
},