fn begin_packet<'a>(
        &mut self,
        space_id: SpaceId,
        initial_padding: bool,
        buffer: &'a mut Vec<u8>,
    ) -> PacketBuilder<'a> {
        let space = &mut self.spaces[space_id];
        space.loss_probes = space.loss_probes.saturating_sub(1);
        let exact_number = space.get_tx_number();

        let span = trace_span!("send", space = ?space_id, pn = exact_number);
        span.with_subscriber(|(id, dispatch)| dispatch.enter(id));

        let number = PacketNumber::new(exact_number, space.largest_acked_packet.unwrap_or(0));
        let header = match space_id {
            SpaceId::Data if space.crypto.is_some() => Header::Short {
                dst_cid: self.rem_cid,
                number,
                spin: if self.spin_enabled {
                    self.spin
                } else {
                    self.rng.gen()
                },
                key_phase: self.key_phase,
            },
            SpaceId::Data => Header::Long {
                ty: LongType::ZeroRtt,
                src_cid: self.handshake_cid,
                dst_cid: self.rem_cid,
                number,
            },
            SpaceId::Handshake => Header::Long {
                ty: LongType::Handshake,
                src_cid: self.handshake_cid,
                dst_cid: self.rem_cid,
                number,
            },
            SpaceId::Initial => Header::Initial {
                src_cid: self.handshake_cid,
                dst_cid: self.rem_cid,
                token: match self.state {
                    State::Handshake(ref state) => state.token.clone().unwrap_or_else(Bytes::new),
                    _ => Bytes::new(),
                },
                number,
            },
        };
        let partial_encode = header.encode(buffer);
        let (sample_size, tag_len) = if let Some(ref crypto) = space.crypto {
            (
                crypto.header.local.sample_size(),
                crypto.packet.local.tag_len(),
            )
        } else if space_id == SpaceId::Data {
            let zero_rtt = self.zero_rtt_crypto.as_ref().unwrap();
            (zero_rtt.header.sample_size(), zero_rtt.packet.tag_len())
        } else {
            unreachable!("tried to send {:?} packet without keys", space_id);
        };
        let min_size = if initial_padding {
            // Initial packet, must be padded to mitigate amplification attacks
            MIN_INITIAL_SIZE - tag_len
        } else {
            // Regular packet, must be large enough for header protection sampling, i.e. the
            // combined lengths of the encoded packet number and protected payload must be at
            // least 4 bytes longer than the sample required for header protection

            // pn_len + payload_len + tag_len >= sample_size + 4
            // payload_len >= sample_size + 4 - pn_len - tag_len
            buffer.len() + (sample_size + 4).saturating_sub(number.len() + tag_len)
        };
        let max_size =
            buffer.capacity() - partial_encode.start - partial_encode.header_len - tag_len;
        PacketBuilder {
            buffer,
            space: space_id,
            partial_encode,
            exact_number,
            short_header: header.is_short(),
            min_size,
            max_size,
            span,
        }
    }
/// Write a new packet header to `buffer` and determine the packet's properties
Write a new packet header to `buffer` and determine the packet's properties
Write a new packet header to ` buffer ` and determine the packet ' s properties
fn begin_packet<'a>(
        &mut self,
        space_id: SpaceId,
        initial_padding: bool,
        buffer: &'a mut Vec<u8>,
    ) -> PacketBuilder<'a> {
        let space = &mut self.spaces[space_id];
        space.loss_probes = space.loss_probes.saturating_sub(1);
        let exact_number = space.get_tx_number();
        let span = trace_span!("send", space = ?space_id, pn = exact_number);
        span.with_subscriber(|(id, dispatch)| dispatch.enter(id));
        let number = PacketNumber::new(exact_number, space.largest_acked_packet.unwrap_or(0));
        let header = match space_id {
            SpaceId::Data if space.crypto.is_some() => Header::Short {
                dst_cid: self.rem_cid,
                number,
                spin: if self.spin_enabled {
                    self.spin
                } else {
                    self.rng.gen()
                },
                key_phase: self.key_phase,
            },
            SpaceId::Data => Header::Long {
                ty: LongType::ZeroRtt,
                src_cid: self.handshake_cid,
                dst_cid: self.rem_cid,
                number,
            },
            SpaceId::Handshake => Header::Long {
                ty: LongType::Handshake,
                src_cid: self.handshake_cid,
                dst_cid: self.rem_cid,
                number,
            },
            SpaceId::Initial => Header::Initial {
                src_cid: self.handshake_cid,
                dst_cid: self.rem_cid,
                token: match self.state {
                    State::Handshake(ref state) => state.token.clone().unwrap_or_else(Bytes::new),
                    _ => Bytes::new(),
                },
                number,
            },
        };
        let partial_encode = header.encode(buffer);
        let (sample_size, tag_len) = if let Some(ref crypto) = space.crypto {
            (
                crypto.header.local.sample_size(),
                crypto.packet.local.tag_len(),
            )
        } else if space_id == SpaceId::Data {
            let zero_rtt = self.zero_rtt_crypto.as_ref().unwrap();
            (zero_rtt.header.sample_size(), zero_rtt.packet.tag_len())
        } else {
            unreachable!("tried to send {:?} packet without keys", space_id);
        };
        let min_size = if initial_padding {
            // Initial packet, must be padded to mitigate amplification attacks
            MIN_INITIAL_SIZE - tag_len
        } else {
            // Regular packet, must be large enough for header protection sampling, i.e. the
            // combined lengths of the encoded packet number and protected payload must be at
            // least 4 bytes longer than the sample required for header protection
            // pn_len + payload_len + tag_len >= sample_size + 4
            // payload_len >= sample_size + 4 - pn_len - tag_len
            buffer.len() + (sample_size + 4).saturating_sub(number.len() + tag_len)
        };
        let max_size =
            buffer.capacity() - partial_encode.start - partial_encode.header_len - tag_len;
        PacketBuilder {
            buffer,
            space: space_id,
            partial_encode,
            exact_number,
            short_header: header.is_short(),
            min_size,
            max_size,
            span,
        }
    }
fn begin_packet < ' a > ( & mut self , space_id : SpaceId , initial_padding : bool , buffer : & ' a mut Vec < u8 > , ) -> PacketBuilder < ' a > { let space = & mut self . spaces [ space_id ] ; space . loss_probes = space . loss_probes . saturating_sub ( 1 ) ; let exact_number = space . get_tx_number ( ) ; let span = trace_span ! ( " " space space_id pn exact_number ) ; span . with_subscriber ( | ( id , dispatch ) | dispatch . enter ( id ) ) ; let number = PacketNumber :: new ( exact_number , space . largest_acked_packet . unwrap_or ( 0 ) ) ; let header = match space_id { SpaceId :: Data if space . crypto . is_some ( ) => Header :: Short { dst_cid : self . rem_cid , number , spin : if self . spin_enabled { self . spin } else { self . rng . gen ( ) } , key_phase : self . key_phase , } , SpaceId :: Data => Header :: Long { ty : LongType :: ZeroRtt , src_cid : self . handshake_cid , dst_cid : self . rem_cid , number , } , SpaceId :: Handshake => Header :: Long { ty : LongType :: Handshake , src_cid : self . handshake_cid , dst_cid : self . rem_cid , number , } , SpaceId :: Initial => Header :: Initial { src_cid : self . handshake_cid , dst_cid : self . rem_cid , token : match self . state { State :: Handshake ( ref state ) => state . token . clone ( ) . unwrap_or_else ( Bytes :: new ) , _ => Bytes :: new ( ) , } , number , } , } ; let partial_encode = header . encode ( buffer ) ; let ( sample_size , tag_len ) = if let Some ( ref crypto ) = space . crypto { ( crypto . header . local . sample_size ( ) , crypto . packet . local . tag_len ( ) , ) } else if space_id == SpaceId :: Data { let zero_rtt = self . zero_rtt_crypto . as_ref ( ) . unwrap ( ) ; ( zero_rtt . header . sample_size ( ) , zero_rtt . packet . tag_len ( ) ) } else { unreachable ! ( " " space_id ) ; } ; let min_size = if initial_padding { // Initial packet, must be padded to mitigate amplification attacks MIN_INITIAL_SIZE - tag_len } else { // Regular packet, must be large enough for header protection sampling, i.e. the // combined lengths of the encoded packet number and protected payload must be at // least 4 bytes longer than the sample required for header protection // pn_len + payload_len + tag_len >= sample_size + 4 // payload_len >= sample_size + 4 - pn_len - tag_len buffer . len ( ) + ( sample_size + 4 ) . saturating_sub ( number . len ( ) + tag_len ) } ; let max_size = buffer . capacity ( ) - partial_encode . start - partial_encode . header_len - tag_len ; PacketBuilder { buffer , space : space_id , partial_encode , exact_number , short_header : header . is_short ( ) , min_size , max_size , span , } }
Write a new packet header to `buffer` and determine the packet's properties
Write a new packet header to ` buffer ` and determine the packet ' s properties
&mut self
space_id
SpaceId
initial_padding
bool
buffer
&'a mut Vec<u8>