fn write_to_bytes(&self, buf: &mut BytesMut) {
        let len_len = 4;
        let value_len = self.len();
        let len_slice = u32::to_be_bytes(value_len as u32);
        buf.reserve(len_len + value_len);
        buf.extend_from_slice(&len_slice);

        match self {
            Message::Keepalive => (),
            Message::Choke => {
                buf.put_u8(CHOKE);
            }
            Message::Unchoke => {
                buf.put_u8(UNCHOKE);
            }
            Message::Interested => {
                buf.put_u8(INTERESTED);
            }
            Message::NotInterested => {
                buf.put_u8(NOT_INTERESTED);
            }
            Message::Have { index } => {
                buf.put_u8(HAVE);
                buf.extend_from_slice(&index.to_be_bytes());
            }
            Message::Bitfield { bitfield } => {
                let bitfield_as_bytes = bitfield.as_raw_slice();
                buf.put_u8(BITFIELD);
                buf.extend_from_slice(bitfield_as_bytes);
            }
            Message::Request {
                index,
                begin,
                length,
            } => {
                buf.put_u8(REQUEST);
                buf.extend_from_slice(&encode_number(*index));
                buf.extend_from_slice(&encode_number(*begin));
                buf.extend_from_slice(&encode_number(*length));
            }
            Message::Piece {
                index,
                begin,
                chunk,
            } => {
                buf.put_u8(PIECE);
                buf.extend_from_slice(&encode_number(*index));
                buf.extend_from_slice(&encode_number(*begin));
                buf.extend_from_slice(chunk);
            }
            Message::Cancel {
                index,
                begin,
                length,
            } => {
                buf.put_u8(CANCEL);
                buf.extend_from_slice(&encode_number(*index));
                buf.extend_from_slice(&encode_number(*begin));
                buf.extend_from_slice(&encode_number(*length));
            }
        }
    }
/// Length Type Value aka "LTV"
/// 4 length bytes, 1 type byte, value bytes
Length Type Value aka "LTV"
4 length bytes, 1 type byte, value bytes
Length Type Value aka " LTV " 4 length bytes 1 type byte value bytes
fn write_to_bytes(&self, buf: &mut BytesMut) {
        let len_len = 4;
        let value_len = self.len();
        let len_slice = u32::to_be_bytes(value_len as u32);
        buf.reserve(len_len + value_len);
        buf.extend_from_slice(&len_slice);
        match self {
            Message::Keepalive => (),
            Message::Choke => {
                buf.put_u8(CHOKE);
            }
            Message::Unchoke => {
                buf.put_u8(UNCHOKE);
            }
            Message::Interested => {
                buf.put_u8(INTERESTED);
            }
            Message::NotInterested => {
                buf.put_u8(NOT_INTERESTED);
            }
            Message::Have { index } => {
                buf.put_u8(HAVE);
                buf.extend_from_slice(&index.to_be_bytes());
            }
            Message::Bitfield { bitfield } => {
                let bitfield_as_bytes = bitfield.as_raw_slice();
                buf.put_u8(BITFIELD);
                buf.extend_from_slice(bitfield_as_bytes);
            }
            Message::Request {
                index,
                begin,
                length,
            } => {
                buf.put_u8(REQUEST);
                buf.extend_from_slice(&encode_number(*index));
                buf.extend_from_slice(&encode_number(*begin));
                buf.extend_from_slice(&encode_number(*length));
            }
            Message::Piece {
                index,
                begin,
                chunk,
            } => {
                buf.put_u8(PIECE);
                buf.extend_from_slice(&encode_number(*index));
                buf.extend_from_slice(&encode_number(*begin));
                buf.extend_from_slice(chunk);
            }
            Message::Cancel {
                index,
                begin,
                length,
            } => {
                buf.put_u8(CANCEL);
                buf.extend_from_slice(&encode_number(*index));
                buf.extend_from_slice(&encode_number(*begin));
                buf.extend_from_slice(&encode_number(*length));
            }
        }
    }
fn write_to_bytes ( & self , buf : & mut BytesMut ) { let len_len = 4 ; let value_len = self . len ( ) ; let len_slice = u32 :: to_be_bytes ( value_len as u32 ) ; buf . reserve ( len_len + value_len ) ; buf . extend_from_slice ( & len_slice ) ; match self { Message :: Keepalive => ( ) , Message :: Choke => { buf . put_u8 ( CHOKE ) ; } Message :: Unchoke => { buf . put_u8 ( UNCHOKE ) ; } Message :: Interested => { buf . put_u8 ( INTERESTED ) ; } Message :: NotInterested => { buf . put_u8 ( NOT_INTERESTED ) ; } Message :: Have { index } => { buf . put_u8 ( HAVE ) ; buf . extend_from_slice ( & index . to_be_bytes ( ) ) ; } Message :: Bitfield { bitfield } => { let bitfield_as_bytes = bitfield . as_raw_slice ( ) ; buf . put_u8 ( BITFIELD ) ; buf . extend_from_slice ( bitfield_as_bytes ) ; } Message :: Request { index , begin , length , } => { buf . put_u8 ( REQUEST ) ; buf . extend_from_slice ( & encode_number ( * index ) ) ; buf . extend_from_slice ( & encode_number ( * begin ) ) ; buf . extend_from_slice ( & encode_number ( * length ) ) ; } Message :: Piece { index , begin , chunk , } => { buf . put_u8 ( PIECE ) ; buf . extend_from_slice ( & encode_number ( * index ) ) ; buf . extend_from_slice ( & encode_number ( * begin ) ) ; buf . extend_from_slice ( chunk ) ; } Message :: Cancel { index , begin , length , } => { buf . put_u8 ( CANCEL ) ; buf . extend_from_slice ( & encode_number ( * index ) ) ; buf . extend_from_slice ( & encode_number ( * begin ) ) ; buf . extend_from_slice ( & encode_number ( * length ) ) ; } } }
Length Type Value aka "LTV"
4 length bytes, 1 type byte, value bytes
Length Type Value aka " LTV " 4 length bytes 1 type byte value bytes
&self
buf
&mut BytesMut
&self
buf
&mut BytesMut