muxing: adds an error type to streammuxer (#1083)

* muxing: adds an error type to streammuxer

* Update examples/chat.rs

Co-Authored-By: montekki <fedor.sakharov@gmail.com>

* make the trait error type bound to io error
This commit is contained in:
Fedor Sakharov
2019-04-28 14:42:18 +03:00
committed by Pierre Krieger
parent 47a775dbce
commit 68df8c07cf
11 changed files with 91 additions and 78 deletions

View File

@ -134,11 +134,12 @@ where
{
type Substream = EitherOutput<A::Substream, B::Substream>;
type OutboundSubstream = EitherOutbound<A, B>;
type Error = IoError;
fn poll_inbound(&self) -> Poll<Self::Substream, IoError> {
fn poll_inbound(&self) -> Poll<Self::Substream, Self::Error> {
match self {
EitherOutput::First(inner) => inner.poll_inbound().map(|p| p.map(EitherOutput::First)),
EitherOutput::Second(inner) => inner.poll_inbound().map(|p| p.map(EitherOutput::Second)),
EitherOutput::First(inner) => inner.poll_inbound().map(|p| p.map(EitherOutput::First)).map_err(|e| e.into()),
EitherOutput::Second(inner) => inner.poll_inbound().map(|p| p.map(EitherOutput::Second)).map_err(|e| e.into()),
}
}
@ -149,13 +150,13 @@ where
}
}
fn poll_outbound(&self, substream: &mut Self::OutboundSubstream) -> Poll<Self::Substream, IoError> {
fn poll_outbound(&self, substream: &mut Self::OutboundSubstream) -> Poll<Self::Substream, Self::Error> {
match (self, substream) {
(EitherOutput::First(ref inner), EitherOutbound::A(ref mut substream)) => {
inner.poll_outbound(substream).map(|p| p.map(EitherOutput::First))
inner.poll_outbound(substream).map(|p| p.map(EitherOutput::First)).map_err(|e| e.into())
},
(EitherOutput::Second(ref inner), EitherOutbound::B(ref mut substream)) => {
inner.poll_outbound(substream).map(|p| p.map(EitherOutput::Second))
inner.poll_outbound(substream).map(|p| p.map(EitherOutput::Second)).map_err(|e| e.into())
},
_ => panic!("Wrong API usage")
}
@ -178,49 +179,49 @@ where
}
}
fn read_substream(&self, sub: &mut Self::Substream, buf: &mut [u8]) -> Poll<usize, IoError> {
fn read_substream(&self, sub: &mut Self::Substream, buf: &mut [u8]) -> Poll<usize, Self::Error> {
match (self, sub) {
(EitherOutput::First(ref inner), EitherOutput::First(ref mut sub)) => {
inner.read_substream(sub, buf)
inner.read_substream(sub, buf).map_err(|e| e.into())
},
(EitherOutput::Second(ref inner), EitherOutput::Second(ref mut sub)) => {
inner.read_substream(sub, buf)
inner.read_substream(sub, buf).map_err(|e| e.into())
},
_ => panic!("Wrong API usage")
}
}
fn write_substream(&self, sub: &mut Self::Substream, buf: &[u8]) -> Poll<usize, IoError> {
fn write_substream(&self, sub: &mut Self::Substream, buf: &[u8]) -> Poll<usize, Self::Error> {
match (self, sub) {
(EitherOutput::First(ref inner), EitherOutput::First(ref mut sub)) => {
inner.write_substream(sub, buf)
inner.write_substream(sub, buf).map_err(|e| e.into())
},
(EitherOutput::Second(ref inner), EitherOutput::Second(ref mut sub)) => {
inner.write_substream(sub, buf)
inner.write_substream(sub, buf).map_err(|e| e.into())
},
_ => panic!("Wrong API usage")
}
}
fn flush_substream(&self, sub: &mut Self::Substream) -> Poll<(), IoError> {
fn flush_substream(&self, sub: &mut Self::Substream) -> Poll<(), Self::Error> {
match (self, sub) {
(EitherOutput::First(ref inner), EitherOutput::First(ref mut sub)) => {
inner.flush_substream(sub)
inner.flush_substream(sub).map_err(|e| e.into())
},
(EitherOutput::Second(ref inner), EitherOutput::Second(ref mut sub)) => {
inner.flush_substream(sub)
inner.flush_substream(sub).map_err(|e| e.into())
},
_ => panic!("Wrong API usage")
}
}
fn shutdown_substream(&self, sub: &mut Self::Substream) -> Poll<(), IoError> {
fn shutdown_substream(&self, sub: &mut Self::Substream) -> Poll<(), Self::Error> {
match (self, sub) {
(EitherOutput::First(ref inner), EitherOutput::First(ref mut sub)) => {
inner.shutdown_substream(sub)
inner.shutdown_substream(sub).map_err(|e| e.into())
},
(EitherOutput::Second(ref inner), EitherOutput::Second(ref mut sub)) => {
inner.shutdown_substream(sub)
inner.shutdown_substream(sub).map_err(|e| e.into())
},
_ => panic!("Wrong API usage")
}
@ -250,17 +251,17 @@ where
}
}
fn close(&self) -> Poll<(), IoError> {
fn close(&self) -> Poll<(), Self::Error> {
match self {
EitherOutput::First(inner) => inner.close(),
EitherOutput::Second(inner) => inner.close()
EitherOutput::First(inner) => inner.close().map_err(|e| e.into()),
EitherOutput::Second(inner) => inner.close().map_err(|e| e.into()),
}
}
fn flush_all(&self) -> Poll<(), IoError> {
fn flush_all(&self) -> Poll<(), Self::Error> {
match self {
EitherOutput::First(inner) => inner.flush_all(),
EitherOutput::Second(inner) => inner.flush_all()
EitherOutput::First(inner) => inner.flush_all().map_err(|e| e.into()),
EitherOutput::Second(inner) => inner.flush_all().map_err(|e| e.into()),
}
}
}