Trait tk_bufstream::Decode [] [src]

pub trait Decode: Sized {
    type Item;
    fn decode(&mut self, buf: &mut Buf) -> Result<Option<Self::Item>, Error>;

    fn done(&mut self, buf: &mut Buf) -> Result<Self::Item> { ... }
}

Decoding of a frame from an internal buffer.

This trait is used when constructing an instance of Framed. It defines how to decode the incoming bytes on a stream to the specified type of frame for that framed I/O stream.

The primary method of this trait, decode, attempts to decode a frame from a buffer of bytes. It has the option of returning NotReady, indicating that more bytes need to be read before decoding can continue.

Associated Types

Decoded message

Required Methods

Attempts to decode a frame from the provided buffer of bytes.

This method is called by Framed whenever bytes are ready to be parsed. The provided buffer of bytes is what's been read so far, and this instance of Decode can determine whether an entire frame is in the buffer and is ready to be returned.

If an entire frame is available, then this instance will remove those bytes from the buffer provided and return them as a decoded frame. Note that removing bytes from the provided buffer doesn't always necessarily copy the bytes, so this should be an efficient operation in most circumstances.

If the bytes look valid, but a frame isn't fully available yet, then Ok(None) is returned. This indicates to the Framed instance that it needs to read some more bytes before calling this method again.

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.

Provided Methods

A default method available to be called when there are no more bytes available to be read from the underlying I/O.

This method defaults to calling decode and returns an error if Ok(None) is returned. Typically this doesn't need to be implemented unless the framing protocol differs near the end of the stream.

Implementors