From 22c260ed9cdc96a5288934f543e946f494a0c811 Mon Sep 17 00:00:00 2001 From: Euan Rochester Date: Mon, 3 Jul 2017 18:47:36 +0100 Subject: [PATCH] Make Error an associated type of UdpCodec --- examples/udp-codec.rs | 1 + src/net/udp/frame.rs | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/examples/udp-codec.rs b/examples/udp-codec.rs index e7456f71..00663280 100644 --- a/examples/udp-codec.rs +++ b/examples/udp-codec.rs @@ -23,6 +23,7 @@ pub struct LineCodec; impl UdpCodec for LineCodec { type In = (SocketAddr, Vec); type Out = (SocketAddr, Vec); + type Error = io::Error; fn decode(&mut self, addr: &SocketAddr, buf: &[u8]) -> io::Result { Ok((*addr, buf.to_vec())) diff --git a/src/net/udp/frame.rs b/src/net/udp/frame.rs index 28705f95..c41acfb7 100644 --- a/src/net/udp/frame.rs +++ b/src/net/udp/frame.rs @@ -26,6 +26,19 @@ pub trait UdpCodec { /// The type of frames to be encoded. type Out; + /// The type of unrecoverable frame decoding errors. + /// + /// If an individual message is ill-formed but can be ignored without + /// interfering with the processing of future messages, it may be more + /// useful to report the failure as an `Item`. + /// + /// `From` is required in the interest of making `Error` suitable + /// for returning directly from a `Framed`.alloc + /// + /// Note that implementors of this trait can simply indicate `type Error = + /// io::Error` to use I/O errors as this type. + type Error: From; + /// Attempts to decode a frame from the provided buffer of bytes. /// /// This method is called by `UdpFramed` on a single datagram which has been @@ -37,7 +50,7 @@ pub trait UdpCodec { /// Finally, if the bytes in the buffer are malformed then an error is /// returned indicating why. This informs `Framed` that the stream is now /// corrupt and should be terminated. - fn decode(&mut self, src: &SocketAddr, buf: &[u8]) -> io::Result; + fn decode(&mut self, src: &SocketAddr, buf: &[u8]) -> Result; /// Encodes a frame into the buffer provided. /// @@ -65,9 +78,9 @@ pub struct UdpFramed { impl Stream for UdpFramed { type Item = C::In; - type Error = io::Error; + type Error = C::Error; - fn poll(&mut self) -> Poll, io::Error> { + fn poll(&mut self) -> Poll, C::Error> { let (n, addr) = try_nb!(self.socket.recv_from(&mut self.rd)); trace!("received {} bytes, decoding", n); let frame = try!(self.codec.decode(&addr, &self.rd[..n]));