pub fn write_bits(
        &self,
        writer: &mut BitWriter<&mut Vec<u8>, BigEndian>,
    ) -> Result<(), PacketError> {
        // VVV
        writer.write(3, self.version)?;
        // TTT
        writer.write::<u8>(3, u8::from(&self.kind))?;

        match &self.kind {
            PacketKind::Literal(value) => {
                // Convert value to binbary string representation
                let value = format!("{:b}", value);
                // Zero-pad bits to multiple of 3
                let mut value = std::iter::repeat('0')
                    .take((4 - value.len() % 4) % 4)
                    .chain(value.chars())
                    .map(|c| if c == '0' { 0 } else { 1 })
                    .collect_vec();
                value.reverse();

                while !value.is_empty() {
                    writer.write(1, if value.len() > 4 { 1 } else { 0 })?;
                    let bits: Vec<u8> = std::iter::repeat_with(|| value.pop())
                        .take(4)
                        .collect::<Option<Vec<_>>>()
                        .expect("value is a multiple of 4");
                    let n: u8 = bits[0] << 3 | bits[1] << 2 | bits[2] << 1 | bits[3];
                    writer.write(4, n)?;
                }
            }
            PacketKind::Operator {
                packets, length, ..
            } => {
                match length {
                    Length::TotalBits(bits) => {
                        // I
                        writer.write(1, 0)?;
                        // L
                        writer.write(15, *bits)?;
                    }
                    Length::PacketCount(packets) => {
                        // I
                        writer.write(1, 1)?;
                        // L
                        writer.write(11, *packets as u16)?;
                    }
                }

                for packet in packets {
                    packet.write_bits(writer)?;
                }
            }
        };

        Ok(())
    }
/// Serializes packet as bits into writer.
///
/// # Errors
///
/// Will return `Err` if write fails.
Serializes packet as bits into writer.
Serializes packet as bits into writer .
pub fn write_bits(
        &self,
        writer: &mut BitWriter<&mut Vec<u8>, BigEndian>,
    ) -> Result<(), PacketError> {
        // VVV
        writer.write(3, self.version)?;
        // TTT
        writer.write::<u8>(3, u8::from(&self.kind))?;
        match &self.kind {
            PacketKind::Literal(value) => {
                // Convert value to binbary string representation
                let value = format!("{:b}", value);
                // Zero-pad bits to multiple of 3
                let mut value = std::iter::repeat('0')
                    .take((4 - value.len() % 4) % 4)
                    .chain(value.chars())
                    .map(|c| if c == '0' { 0 } else { 1 })
                    .collect_vec();
                value.reverse();
                while !value.is_empty() {
                    writer.write(1, if value.len() > 4 { 1 } else { 0 })?;
                    let bits: Vec<u8> = std::iter::repeat_with(|| value.pop())
                        .take(4)
                        .collect::<Option<Vec<_>>>()
                        .expect("value is a multiple of 4");
                    let n: u8 = bits[0] << 3 | bits[1] << 2 | bits[2] << 1 | bits[3];
                    writer.write(4, n)?;
                }
            }
            PacketKind::Operator {
                packets, length, ..
            } => {
                match length {
                    Length::TotalBits(bits) => {
                        // I
                        writer.write(1, 0)?;
                        // L
                        writer.write(15, *bits)?;
                    }
                    Length::PacketCount(packets) => {
                        // I
                        writer.write(1, 1)?;
                        // L
                        writer.write(11, *packets as u16)?;
                    }
                }
                for packet in packets {
                    packet.write_bits(writer)?;
                }
            }
        };
        Ok(())
    }
pub fn write_bits ( & self , writer : & mut BitWriter < & mut Vec < u8 > , BigEndian > , ) -> Result < ( ) , PacketError > { // VVV writer . write ( 3 , self . version ) ? ; // TTT writer . write :: < u8 > ( 3 , u8 :: from ( & self . kind ) ) ? ; match & self . kind { PacketKind :: Literal ( value ) => { // Convert value to binbary string representation let value = format ! ( " " value ) ; // Zero-pad bits to multiple of 3 let mut value = std :: iter :: repeat ( '0' ) . take ( ( 4 - value . len ( ) % 4 ) % 4 ) . chain ( value . chars ( ) ) . map ( | c | if c == '0' { 0 } else { 1 } ) . collect_vec ( ) ; value . reverse ( ) ; while ! value . is_empty ( ) { writer . write ( 1 , if value . len ( ) > 4 { 1 } else { 0 } ) ? ; let bits : Vec < u8 > = std :: iter :: repeat_with ( | | value . pop ( ) ) . take ( 4 ) . collect :: < Option < Vec < _ > > > ( ) . expect ( " " ) ; let n : u8 = bits [ 0 ] << 3 | bits [ 1 ] << 2 | bits [ 2 ] << 1 | bits [ 3 ] ; writer . write ( 4 , n ) ? ; } } PacketKind :: Operator { packets , length , .. } => { match length { Length :: TotalBits ( bits ) => { // I writer . write ( 1 , 0 ) ? ; // L writer . write ( 15 , * bits ) ? ; } Length :: PacketCount ( packets ) => { // I writer . write ( 1 , 1 ) ? ; // L writer . write ( 11 , * packets as u16 ) ? ; } } for packet in packets { packet . write_bits ( writer ) ? ; } } } ; Ok ( ( ) ) }
Serializes packet as bits into writer.
Serializes packet as bits into writer .
&self
writer
&mut BitWriter<&mut Vec<u8>, BigEndian>
&self
writer
&mut BitWriter<&mut Vec<u8>, BigEndian>
errors
Will return `Err` if write fails.
Will return ` Err ` if write fails .