Decodes a standard span-batch bitlist from a reader. The bitlist is encoded as big-endian integer, left-padded with zeroes to a multiple of 8 bits. The encoded bitlist cannot be longer than `bit_length`.
(b: &mut &[u8], bit_length: usize)
| 28 | /// The bitlist is encoded as big-endian integer, left-padded with zeroes to a multiple of 8 |
| 29 | /// bits. The encoded bitlist cannot be longer than `bit_length`. |
| 30 | pub fn decode(b: &mut &[u8], bit_length: usize) -> Result<Self, SpanBatchError> { |
| 31 | let buffer_len = bit_length / 8 + if !bit_length.is_multiple_of(8) { 1 } else { 0 }; |
| 32 | let bits = if b.len() < buffer_len { |
| 33 | let mut bits = vec![0; buffer_len]; |
| 34 | bits[..b.len()].copy_from_slice(b); |
| 35 | b.advance(b.len()); |
| 36 | bits |
| 37 | } else { |
| 38 | let v = b[..buffer_len].to_vec(); |
| 39 | b.advance(buffer_len); |
| 40 | v |
| 41 | }; |
| 42 | let sb_bits = Self(bits); |
| 43 | |
| 44 | if sb_bits.bit_len() > bit_length { |
| 45 | return Err(SpanBatchError::BitfieldTooLong); |
| 46 | } |
| 47 | |
| 48 | Ok(sb_bits) |
| 49 | } |
| 50 | |
| 51 | /// Encodes a standard span-batch bitlist. |
| 52 | /// The bitlist is encoded as big-endian integer, left-padded with zeroes to a multiple of 8 |