// Decode a message if there is enough data in the buffer.
Decode a message if there is enough data in the buffer.
Decode a message if there is enough data in the buffer .
pub fn try_decode<'de, T: Deserialize<'de>>(&'de mut self) -> Option<T> {
        let avail = self.buf.len() - self.offset;
        if avail < size_of::<usize>() {
            return None;
        }
        let payload_size = &self.buf[self.offset..self.offset + size_of::<usize>()];
        let payload_size = usize::from_be_bytes(payload_size.try_into().unwrap());
        if avail < size_of::<usize>() + payload_size {
            return None;
        }
        let payload_offset = self.offset + size_of::<usize>();
        let payload = &self.buf[payload_offset..payload_offset + payload_size];
        let res: T =
            bincode::deserialize(payload).expect("failed to deserialize a bincode message");
        // Update the offset
        self.offset = payload_offset + payload_size;
        Some(res)
    }
pub fn try_decode < ' de , T : Deserialize < ' de > > ( & ' de mut self ) -> Option < T > { let avail = self . buf . len ( ) - self . offset ; if avail < size_of :: < usize > ( ) { return None ; } let payload_size = & self . buf [ self . offset .. self . offset + size_of :: < usize > ( ) ] ; let payload_size = usize :: from_be_bytes ( payload_size . try_into ( ) . unwrap ( ) ) ; if avail < size_of :: < usize > ( ) + payload_size { return None ; } let payload_offset = self . offset + size_of :: < usize > ( ) ; let payload = & self . buf [ payload_offset .. payload_offset + payload_size ] ; let res : T = bincode :: deserialize ( payload ) . expect ( " " ) ; // Update the offset self . offset = payload_offset + payload_size ; Some ( res ) }
Decode a message if there is enough data in the buffer.
Decode a message if there is enough data in the buffer .
&'de mut self