| 1 | #[cfg(feature = "use_std")] |
| 2 | use craterust-analyzer cargo cobs 0.2.3 crate/::max_encoding_lengthrust-analyzer cargo cobs 0.2.3 max_encoding_length().; |
| 3 | |
| 4 | /// The [`CobsEncoder`] type is used to encode a stream of bytes to a |
| 5 | /// given mutable output slice. This is often useful when heap data |
| 6 | /// structures are not available, or when not all message bytes are |
| 7 | /// received at a single point in time. |
| 8 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 9 | pub struct CobsEncoderrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#<'a> { |
| 10 | destrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest.: &'a mut [u8], |
| 11 | dest_idxrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest_idx.: usize, |
| 12 | staterust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#state.: EncoderStaterust-analyzer cargo cobs 0.2.3 enc/EncoderState#, |
| 13 | } |
| 14 | |
| 15 | /// The [`EncoderState`] is used to track the current state of a |
| 16 | /// streaming encoder. This struct does not contain the output buffer |
| 17 | /// (or a reference to one), and can be used when streaming the encoded |
| 18 | /// output to a custom data type |
| 19 | /// |
| 20 | /// **IMPORTANT NOTE**: When implementing a custom streaming encoder, |
| 21 | /// the [`EncoderState`] state machine assumes that the output buffer |
| 22 | /// **ALREADY** contains a single placeholder byte, and no other bytes. |
| 23 | /// This placeholder byte will be later modified with the first distance |
| 24 | /// to the next header/zero byte. |
| 25 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 26 | pub struct EncoderStaterust-analyzer cargo cobs 0.2.3 enc/EncoderState# { |
| 27 | code_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#code_idx.: usize, |
| 28 | num_bt_sentrust-analyzer cargo cobs 0.2.3 enc/EncoderState#num_bt_sent.: u8, |
| 29 | offset_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#offset_idx.: u8, |
| 30 | } |
| 31 | |
| 32 | /// [`PushResult`] is used to represent the changes to an (encoded) |
| 33 | /// output data buffer when an unencoded byte is pushed into [`EncoderState`]. |
| 34 | pub enum PushResultrust-analyzer cargo cobs 0.2.3 enc/PushResult# { |
| 35 | /// The returned byte should be placed at the current end of the data buffer |
| 36 | AddSinglerust-analyzer cargo cobs 0.2.3 enc/PushResult#AddSingle#(u8), |
| 37 | |
| 38 | /// The byte at the given index should be replaced with the given byte. |
| 39 | /// Additionally, a placeholder byte should be inserted at the current |
| 40 | /// end of the output buffer to be later modified |
| 41 | ModifyFromStartAndSkiprust-analyzer cargo cobs 0.2.3 enc/PushResult#ModifyFromStartAndSkip#((usize, u8)), |
| 42 | |
| 43 | /// The byte at the given index should be replaced with the given byte. |
| 44 | /// Then, the last u8 in this tuple should be inserted at the end of the |
| 45 | /// current output buffer. Finally, a placeholder byte should be inserted at |
| 46 | /// the current end of the output buffer to be later modified |
| 47 | ModifyFromStartAndPushAndSkiprust-analyzer cargo cobs 0.2.3 enc/PushResult#ModifyFromStartAndPushAndSkip#((usize, u8, u8)) |
| 48 | } |
| 49 | |
| 50 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for EncoderStaterust-analyzer cargo cobs 0.2.3 enc/EncoderState# { |
| 51 | /// Create a default initial state representation for a COBS encoder |
| 52 | fn defaultrust-analyzer cargo cobs 0.2.3 enc/impl#[EncoderState][Default]default().() -> Selfrust-analyzer cargo cobs 0.2.3 enc/impl#[EncoderState][Default] { |
| 53 | Selfrust-analyzer cargo cobs 0.2.3 enc/impl#[EncoderState][Default] { |
| 54 | code_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#code_idx.: 0, |
| 55 | num_bt_sentrust-analyzer cargo cobs 0.2.3 enc/EncoderState#num_bt_sent.: 1, |
| 56 | offset_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#offset_idx.: 1, |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl EncoderStaterust-analyzer cargo cobs 0.2.3 enc/EncoderState# { |
| 62 | /// Push a single unencoded byte into the encoder state machine |
| 63 | pub fn pushrust-analyzer cargo cobs 0.2.3 enc/impl#[EncoderState]push().(&mut self, data: u8) -> PushResultrust-analyzer cargo cobs 0.2.3 enc/PushResult# { |
| 64 | if data rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0 { |
| 65 | let ret = PushResultrust-analyzer cargo cobs 0.2.3 enc/PushResult#::ModifyFromStartAndSkiprust-analyzer cargo cobs 0.2.3 enc/PushResult#ModifyFromStartAndSkip#((self.code_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#code_idx., self.num_bt_sentrust-analyzer cargo cobs 0.2.3 enc/EncoderState#num_bt_sent.)); |
| 66 | self.code_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#code_idx. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().usize::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[usize][`From<u8>`]from().(self.offset_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#offset_idx.); |
| 67 | self.num_bt_sentrust-analyzer cargo cobs 0.2.3 enc/EncoderState#num_bt_sent. = 1; |
| 68 | self.offset_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#offset_idx. = 1; |
| 69 | ret |
| 70 | } else { |
| 71 | self.num_bt_sentrust-analyzer cargo cobs 0.2.3 enc/EncoderState#num_bt_sent. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`AddAssign<Self>`]add_assign().1; |
| 72 | self.offset_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#offset_idx. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`AddAssign<Self>`]add_assign().1; |
| 73 | |
| 74 | if 0xFF rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().self.num_bt_sentrust-analyzer cargo cobs 0.2.3 enc/EncoderState#num_bt_sent. { |
| 75 | let ret = PushResultrust-analyzer cargo cobs 0.2.3 enc/PushResult#::ModifyFromStartAndPushAndSkiprust-analyzer cargo cobs 0.2.3 enc/PushResult#ModifyFromStartAndPushAndSkip#((self.code_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#code_idx., self.num_bt_sentrust-analyzer cargo cobs 0.2.3 enc/EncoderState#num_bt_sent., data)); |
| 76 | self.num_bt_sentrust-analyzer cargo cobs 0.2.3 enc/EncoderState#num_bt_sent. = 1; |
| 77 | self.code_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#code_idx. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().usize::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[usize][`From<u8>`]from().(self.offset_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#offset_idx.); |
| 78 | self.offset_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#offset_idx. = 1; |
| 79 | ret |
| 80 | } else { |
| 81 | PushResultrust-analyzer cargo cobs 0.2.3 enc/PushResult#::AddSinglerust-analyzer cargo cobs 0.2.3 enc/PushResult#AddSingle#(data) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// Finalize the encoding process for a single message. |
| 87 | /// The byte at the given index should be replaced with the given value, |
| 88 | /// and the sentinel value (typically 0u8) must be inserted at the current |
| 89 | /// end of the output buffer, serving as a framing byte. |
| 90 | pub fn finalizerust-analyzer cargo cobs 0.2.3 enc/impl#[EncoderState]finalize().(self) -> (usize, u8) { |
| 91 | (self.code_idxrust-analyzer cargo cobs 0.2.3 enc/EncoderState#code_idx., self.num_bt_sentrust-analyzer cargo cobs 0.2.3 enc/EncoderState#num_bt_sent.) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | impl<'a> CobsEncoderrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#<'a> { |
| 96 | |
| 97 | /// Create a new streaming Cobs Encoder |
| 98 | pub fn newrust-analyzer cargo cobs 0.2.3 enc/impl#[`CobsEncoder<'a>`]new().(out_buf: &'a mut [u8]) -> CobsEncoderrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#<'a> { |
| 99 | CobsEncoderrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder# { |
| 100 | destrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest.: out_buf, |
| 101 | dest_idxrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest_idx.: 1, |
| 102 | staterust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#state.: EncoderStaterust-analyzer cargo cobs 0.2.3 enc/EncoderState#::defaultrust-analyzer cargo cobs 0.2.3 enc/impl#[EncoderState][Default]default().(), |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /// Push a slice of data to be encoded |
| 107 | pub fn pushrust-analyzer cargo cobs 0.2.3 enc/impl#[`CobsEncoder<'a>`]push().(&mut self, data: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), ()> { |
| 108 | // TODO: could probably check if this would fit without |
| 109 | // iterating through all data |
| 110 | for x in data { |
| 111 | use PushResultrust-analyzer cargo cobs 0.2.3 enc/PushResult#::*; |
| 112 | match self.staterust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#state..pushrust-analyzer cargo cobs 0.2.3 enc/impl#[EncoderState]push().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().x) { |
| 113 | AddSinglerust-analyzer cargo cobs 0.2.3 enc/PushResult#AddSingle#(y) => { |
| 114 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self.destrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest..get_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get_mut().(self.dest_idxrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest_idx.) |
| 115 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| ())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). = y; |
| 116 | } |
| 117 | ModifyFromStartAndSkiprust-analyzer cargo cobs 0.2.3 enc/PushResult#ModifyFromStartAndSkip#((idx, mval)) => { |
| 118 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self.destrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest..get_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get_mut().(idx) |
| 119 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| ())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). = mval; |
| 120 | } |
| 121 | ModifyFromStartAndPushAndSkiprust-analyzer cargo cobs 0.2.3 enc/PushResult#ModifyFromStartAndPushAndSkip#((idx, mval, nval1)) => { |
| 122 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self.destrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest..get_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get_mut().(idx) |
| 123 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| ())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). = mval; |
| 124 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self.destrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest..get_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get_mut().(self.dest_idxrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest_idx.) |
| 125 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| ())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). = nval1; |
| 126 | self.dest_idxrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest_idx. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // All branches above require advancing the pointer at least once |
| 131 | self.dest_idxrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest_idx. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 132 | } |
| 133 | |
| 134 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 135 | } |
| 136 | |
| 137 | /// Complete encoding of the output message. Does NOT terminate |
| 138 | /// the message with the sentinel value |
| 139 | pub fn finalizerust-analyzer cargo cobs 0.2.3 enc/impl#[`CobsEncoder<'a>`]finalize().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, ()> { |
| 140 | if self.dest_idxrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest_idx. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 { |
| 141 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0); |
| 142 | } |
| 143 | |
| 144 | // Get the last index that needs to be fixed |
| 145 | let (idx, mval) = self.staterust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#state..finalizerust-analyzer cargo cobs 0.2.3 enc/impl#[EncoderState]finalize().(); |
| 146 | |
| 147 | // If the current code index is outside of the destination slice, |
| 148 | // we do not need to write it out |
| 149 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(i) = self.destrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest..get_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get_mut().(idx) { |
| 150 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().i = mval; |
| 151 | } |
| 152 | |
| 153 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.dest_idxrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#dest_idx.); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /// Encodes the `source` buffer into the `dest` buffer. |
| 158 | /// |
| 159 | /// This function uses the typical sentinel value of 0. It returns the number of bytes |
| 160 | /// written to in the `dest` buffer. |
| 161 | /// |
| 162 | /// # Panics |
| 163 | /// |
| 164 | /// This function will panic if the `dest` buffer is not large enough for the |
| 165 | /// encoded message. You can calculate the size the `dest` buffer needs to be with |
| 166 | /// the `max_encoding_length` function. |
| 167 | pub fn encoderust-analyzer cargo cobs 0.2.3 enc/encode().(source: &[u8], dest: &mut[u8]) -> usize { |
| 168 | let mut enc = CobsEncoderrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#::newrust-analyzer cargo cobs 0.2.3 enc/impl#[`CobsEncoder<'a>`]new().(dest); |
| 169 | enc.pushrust-analyzer cargo cobs 0.2.3 enc/impl#[`CobsEncoder<'a>`]push().(source).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 170 | enc.finalizerust-analyzer cargo cobs 0.2.3 enc/impl#[`CobsEncoder<'a>`]finalize().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 171 | } |
| 172 | |
| 173 | /// Attempts to encode the `source` buffer into the `dest` buffer. |
| 174 | /// |
| 175 | /// This function uses the typical sentinel value of 0. It returns the number of bytes |
| 176 | /// written to in the `dest` buffer. |
| 177 | /// |
| 178 | /// If the destination buffer does not have enough room, an error will be returned |
| 179 | pub fn try_encoderust-analyzer cargo cobs 0.2.3 enc/try_encode().(source: &[u8], dest: &mut[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, ()> { |
| 180 | let mut enc = CobsEncoderrust-analyzer cargo cobs 0.2.3 enc/CobsEncoder#::newrust-analyzer cargo cobs 0.2.3 enc/impl#[`CobsEncoder<'a>`]new().(dest); |
| 181 | enc.pushrust-analyzer cargo cobs 0.2.3 enc/impl#[`CobsEncoder<'a>`]push().(source)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 182 | enc.finalizerust-analyzer cargo cobs 0.2.3 enc/impl#[`CobsEncoder<'a>`]finalize().() |
| 183 | } |
| 184 | |
| 185 | /// Encodes the `source` buffer into the `dest` buffer using an |
| 186 | /// arbitrary sentinel value. |
| 187 | /// |
| 188 | /// This is done by first encoding the message with the typical sentinel value |
| 189 | /// of 0, then XOR-ing each byte of the encoded message with the chosen sentinel |
| 190 | /// value. This will ensure that the sentinel value doesn't show up in the encoded |
| 191 | /// message. See the paper "Consistent Overhead Byte Stuffing" for details. |
| 192 | pub fn encode_with_sentinelrust-analyzer cargo cobs 0.2.3 enc/encode_with_sentinel().(source: &[u8], dest: &mut[u8], sentinel: u8) -> usize { |
| 193 | let encoded_size = encoderust-analyzer cargo cobs 0.2.3 enc/encode().(source, dest); |
| 194 | for x in &mut dest[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#encoded_size]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). { |
| 195 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitXorAssign<Self>`]bitxor_assign().^=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitXorAssign<Self>`]bitxor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitXorAssign<Self>`]bitxor_assign().sentinel; |
| 196 | } |
| 197 | return encoded_size; |
| 198 | } |
| 199 | |
| 200 | #[cfg(feature = "use_std")] |
| 201 | /// Encodes the `source` buffer into a vector. |
| 202 | pub fn encode_vecrust-analyzer cargo cobs 0.2.3 enc/encode_vec().(source: &[u8]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> { |
| 203 | let mut encoded = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; max_encoding_lengthrust-analyzer cargo cobs 0.2.3 max_encoding_length().(source.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().())]; |
| 204 | let encoded_len = encoderust-analyzer cargo cobs 0.2.3 enc/encode().(source, &mut encoded[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().); |
| 205 | encoded.truncaterust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]truncate().(encoded_len); |
| 206 | return encoded; |
| 207 | } |
| 208 | |
| 209 | #[cfg(feature = "use_std")] |
| 210 | /// Encodes the `source` buffer into a vector with an arbitrary sentinel value. |
| 211 | pub fn encode_vec_with_sentinelrust-analyzer cargo cobs 0.2.3 enc/encode_vec_with_sentinel().(source: &[u8], sentinel: u8) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> { |
| 212 | let mut encoded = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; max_encoding_lengthrust-analyzer cargo cobs 0.2.3 max_encoding_length().(source.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().())]; |
| 213 | let encoded_len = encode_with_sentinelrust-analyzer cargo cobs 0.2.3 enc/encode_with_sentinel().(source, &mut encoded[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()., sentinel); |
| 214 | encoded.truncaterust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]truncate().(encoded_len); |
| 215 | return encoded; |
| 216 | } |