bits_io/buf/
bit_buf_mut_impls.rs

1use bitvec::view::BitView;
2use bytes::BufMut;
3
4use super::{bit_buf_mut::BitBufMut, bits_mut::BitsMut};
5use crate::{buf::util::bytes_needed, prelude::*};
6
7impl BitBufMut for BitsMut {
8    fn remaining_mut_bits(&self) -> usize {
9        usize::MAX - self.bit_len
10    }
11
12    fn chunk_mut_bits(&mut self) -> &mut BitSlice {
13        if self.capacity == self.bit_len {
14            self.reserve_bits(64);
15        }
16        self.spare_capacity_mut()
17    }
18
19    fn chunk_mut_bytes(&mut self) -> &mut bytes::buf::UninitSlice {
20        assert!(self.byte_aligned_mut());
21        if self.capacity == self.bit_len {
22            self.reserve_bits(64);
23        }
24        self.inner.chunk_mut()
25    }
26
27    fn advance_mut_bits(&mut self, cnt: usize) {
28        assert!(cnt <= self.remaining_mut_bits(), "advance_mut past end");
29        let current_byte_len = bytes_needed(self.bit_len);
30        self.bit_len += cnt;
31        let new_byte_len = bytes_needed(self.bit_len);
32        // Every time we cross into a new byte, we need to advance the underlying instance's
33        // position as well.
34        if new_byte_len > current_byte_len {
35            unsafe {
36                self.inner.advance_mut(new_byte_len - current_byte_len);
37            }
38        }
39    }
40
41    fn byte_aligned_mut(&self) -> bool {
42        self.bit_start % 8 == 0 && self.bit_len % 8 == 0
43    }
44}
45
46impl BitBufMut for &mut [u8] {
47    fn advance_mut_bits(&mut self, count: usize) {
48        assert!(
49            count <= self.remaining_mut_bits(),
50            "advance_mut_bits past end"
51        );
52        let byte_count = bytes_needed(count);
53
54        let (_, b) = std::mem::take(self).split_at_mut(byte_count);
55        *self = b;
56    }
57
58    fn chunk_mut_bits(&mut self) -> &mut BitSlice {
59        self.view_bits_mut()
60    }
61
62    fn chunk_mut_bytes(&mut self) -> &mut bytes::buf::UninitSlice {
63        bytes::buf::UninitSlice::new(self)
64    }
65
66    fn remaining_mut_bits(&self) -> usize {
67        self.len() * 8
68    }
69
70    fn byte_aligned_mut(&self) -> bool {
71        true
72    }
73}
74
75impl BitBufMut for &mut BitSlice {
76    fn advance_mut_bits(&mut self, count: usize) {
77        assert!(count <= self.len(), "advance_mut_bits past end");
78        *self = &mut std::mem::take(self)[count..];
79    }
80
81    fn chunk_mut_bits(&mut self) -> &mut BitSlice {
82        self
83    }
84
85    fn chunk_mut_bytes(&mut self) -> &mut bytes::buf::UninitSlice {
86        assert!(self.byte_aligned_mut());
87        let bitvec::domain::Domain::Region { body, .. } = self.domain_mut() else {
88            unreachable!("Verified by the assert above");
89        };
90        bytes::buf::UninitSlice::new(body)
91    }
92
93    fn remaining_mut_bits(&self) -> usize {
94        self.len()
95    }
96
97    fn byte_aligned_mut(&self) -> bool {
98        matches!(
99            self.domain(),
100            bitvec::domain::Domain::Region {
101                head: None,
102                tail: None,
103                ..
104            }
105        )
106    }
107}