pub fn decode(data: &'a [u8], dcid_decoder: &dyn ConnectionIdDecoder) -> Res<(Self, &'a [u8])> {
        let mut decoder = Decoder::new(data);
        let first = Self::opt(decoder.decode_byte())?;

        if first & 0x80 == PACKET_BIT_SHORT {
            // Conveniently, this also guarantees that there is enough space
            // for a connection ID of any size.
            if decoder.remaining() < SAMPLE_OFFSET + SAMPLE_SIZE {
                return Err(Error::InvalidPacket);
            }
            let dcid = Self::opt(dcid_decoder.decode_cid(&mut decoder))?;
            if decoder.remaining() < SAMPLE_OFFSET + SAMPLE_SIZE {
                return Err(Error::InvalidPacket);
            }
            let header_len = decoder.offset();
            return Ok((
                Self {
                    packet_type: PacketType::Short,
                    dcid,
                    scid: None,
                    token: &[],
                    header_len,
                    version: None,
                    data,
                },
                &[],
            ));
        }

        // Generic long header.
        let version = WireVersion::try_from(Self::opt(decoder.decode_uint(4))?).unwrap();
        let dcid = ConnectionIdRef::from(Self::opt(decoder.decode_vec(1))?);
        let scid = ConnectionIdRef::from(Self::opt(decoder.decode_vec(1))?);

        // Version negotiation.
        if version == 0 {
            return Ok((
                Self {
                    packet_type: PacketType::VersionNegotiation,
                    dcid,
                    scid: Some(scid),
                    token: &[],
                    header_len: decoder.offset(),
                    version: None,
                    data,
                },
                &[],
            ));
        }

        // Check that this is a long header from a supported version.
        let version = if let Ok(v) = Version::try_from(version) {
            v
        } else {
            return Ok((
                Self {
                    packet_type: PacketType::OtherVersion,
                    dcid,
                    scid: Some(scid),
                    token: &[],
                    header_len: decoder.offset(),
                    version: None,
                    data,
                },
                &[],
            ));
        };

        if dcid.len() > MAX_CONNECTION_ID_LEN || scid.len() > MAX_CONNECTION_ID_LEN {
            return Err(Error::InvalidPacket);
        }
        let packet_type = PacketType::from_byte((first >> 4) & 3, version);

        // The type-specific code includes a token.  This consumes the remainder of the packet.
        let (token, header_len) = Self::decode_long(&mut decoder, packet_type, version)?;
        let end = data.len() - decoder.remaining();
        let (data, remainder) = data.split_at(end);
        Ok((
            Self {
                packet_type,
                dcid,
                scid: Some(scid),
                token,
                header_len,
                version: Some(version),
                data,
            },
            remainder,
        ))
    }
/// Decode the common parts of a packet.  This provides minimal parsing and validation.
/// Returns a tuple of a `PublicPacket` and a slice with any remainder from the datagram.
Decode the common parts of a packet.  This provides minimal parsing and validation.
Returns a tuple of a `PublicPacket` and a slice with any remainder from the datagram.
Decode the common parts of a packet . This provides minimal parsing and validation . Returns a tuple of a ` PublicPacket ` and a slice with any remainder from the datagram .
pub fn decode(data: &'a [u8], dcid_decoder: &dyn ConnectionIdDecoder) -> Res<(Self, &'a [u8])> {
        let mut decoder = Decoder::new(data);
        let first = Self::opt(decoder.decode_byte())?;
        if first & 0x80 == PACKET_BIT_SHORT {
            // Conveniently, this also guarantees that there is enough space
            // for a connection ID of any size.
            if decoder.remaining() < SAMPLE_OFFSET + SAMPLE_SIZE {
                return Err(Error::InvalidPacket);
            }
            let dcid = Self::opt(dcid_decoder.decode_cid(&mut decoder))?;
            if decoder.remaining() < SAMPLE_OFFSET + SAMPLE_SIZE {
                return Err(Error::InvalidPacket);
            }
            let header_len = decoder.offset();
            return Ok((
                Self {
                    packet_type: PacketType::Short,
                    dcid,
                    scid: None,
                    token: &[],
                    header_len,
                    version: None,
                    data,
                },
                &[],
            ));
        }
        // Generic long header.
        let version = WireVersion::try_from(Self::opt(decoder.decode_uint(4))?).unwrap();
        let dcid = ConnectionIdRef::from(Self::opt(decoder.decode_vec(1))?);
        let scid = ConnectionIdRef::from(Self::opt(decoder.decode_vec(1))?);
        // Version negotiation.
        if version == 0 {
            return Ok((
                Self {
                    packet_type: PacketType::VersionNegotiation,
                    dcid,
                    scid: Some(scid),
                    token: &[],
                    header_len: decoder.offset(),
                    version: None,
                    data,
                },
                &[],
            ));
        }
        // Check that this is a long header from a supported version.
        let version = if let Ok(v) = Version::try_from(version) {
            v
        } else {
            return Ok((
                Self {
                    packet_type: PacketType::OtherVersion,
                    dcid,
                    scid: Some(scid),
                    token: &[],
                    header_len: decoder.offset(),
                    version: None,
                    data,
                },
                &[],
            ));
        };
        if dcid.len() > MAX_CONNECTION_ID_LEN || scid.len() > MAX_CONNECTION_ID_LEN {
            return Err(Error::InvalidPacket);
        }
        let packet_type = PacketType::from_byte((first >> 4) & 3, version);
        // The type-specific code includes a token.  This consumes the remainder of the packet.
        let (token, header_len) = Self::decode_long(&mut decoder, packet_type, version)?;
        let end = data.len() - decoder.remaining();
        let (data, remainder) = data.split_at(end);
        Ok((
            Self {
                packet_type,
                dcid,
                scid: Some(scid),
                token,
                header_len,
                version: Some(version),
                data,
            },
            remainder,
        ))
    }
pub fn decode ( data : & ' a [ u8 ] , dcid_decoder : & dyn ConnectionIdDecoder ) -> Res < ( Self , & ' a [ u8 ] ) > { let mut decoder = Decoder :: new ( data ) ; let first = Self :: opt ( decoder . decode_byte ( ) ) ? ; if first & 0x80 == PACKET_BIT_SHORT { // Conveniently, this also guarantees that there is enough space // for a connection ID of any size. if decoder . remaining ( ) < SAMPLE_OFFSET + SAMPLE_SIZE { return Err ( Error :: InvalidPacket ) ; } let dcid = Self :: opt ( dcid_decoder . decode_cid ( & mut decoder ) ) ? ; if decoder . remaining ( ) < SAMPLE_OFFSET + SAMPLE_SIZE { return Err ( Error :: InvalidPacket ) ; } let header_len = decoder . offset ( ) ; return Ok ( ( Self { packet_type : PacketType :: Short , dcid , scid : None , token : & [ ] , header_len , version : None , data , } , & [ ] , ) ) ; } // Generic long header. let version = WireVersion :: try_from ( Self :: opt ( decoder . decode_uint ( 4 ) ) ? ) . unwrap ( ) ; let dcid = ConnectionIdRef :: from ( Self :: opt ( decoder . decode_vec ( 1 ) ) ? ) ; let scid = ConnectionIdRef :: from ( Self :: opt ( decoder . decode_vec ( 1 ) ) ? ) ; // Version negotiation. if version == 0 { return Ok ( ( Self { packet_type : PacketType :: VersionNegotiation , dcid , scid : Some ( scid ) , token : & [ ] , header_len : decoder . offset ( ) , version : None , data , } , & [ ] , ) ) ; } // Check that this is a long header from a supported version. let version = if let Ok ( v ) = Version :: try_from ( version ) { v } else { return Ok ( ( Self { packet_type : PacketType :: OtherVersion , dcid , scid : Some ( scid ) , token : & [ ] , header_len : decoder . offset ( ) , version : None , data , } , & [ ] , ) ) ; } ; if dcid . len ( ) > MAX_CONNECTION_ID_LEN || scid . len ( ) > MAX_CONNECTION_ID_LEN { return Err ( Error :: InvalidPacket ) ; } let packet_type = PacketType :: from_byte ( ( first >> 4 ) & 3 , version ) ; // The type-specific code includes a token.  This consumes the remainder of the packet. let ( token , header_len ) = Self :: decode_long ( & mut decoder , packet_type , version ) ? ; let end = data . len ( ) - decoder . remaining ( ) ; let ( data , remainder ) = data . split_at ( end ) ; Ok ( ( Self { packet_type , dcid , scid : Some ( scid ) , token , header_len , version : Some ( version ) , data , } , remainder , ) ) }
Decode the common parts of a packet.
Decode the common parts of a packet .
data
&'a [u8]
dcid_decoder
&dyn ConnectionIdDecoder