vortex_buffer/bit/
meta.rs1use std::ops::Bound;
5use std::ops::RangeBounds;
6
7use vortex_error::VortexExpect;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub struct BitBufferMeta {
13 offset: usize,
14 len: usize,
15}
16
17impl BitBufferMeta {
18 pub fn new(offset: usize, len: usize) -> Self {
23 assert!(offset < 8, "BitBufferMeta offset must be < 8, got {offset}");
24 Self { offset, len }
25 }
26
27 pub fn from_raw_offset(offset: usize, len: usize) -> (usize, Self) {
33 (
34 offset / 8,
35 Self {
36 offset: offset % 8,
37 len,
38 },
39 )
40 }
41
42 pub fn slice(&self, range: impl RangeBounds<usize>) -> (usize, Self) {
48 let start = match range.start_bound() {
49 Bound::Included(&start) => start,
50 Bound::Excluded(&start) => start
51 .checked_add(1)
52 .vortex_expect("excluded slice start must not overflow"),
53 Bound::Unbounded => 0,
54 };
55 let end = match range.end_bound() {
56 Bound::Included(&end) => end
57 .checked_add(1)
58 .vortex_expect("included slice end must not overflow"),
59 Bound::Excluded(&end) => end,
60 Bound::Unbounded => self.len,
61 };
62
63 assert!(start <= end);
64 assert!(start <= self.len);
65 assert!(end <= self.len);
66
67 Self::from_raw_offset(self.offset + start, end - start)
68 }
69
70 #[inline(always)]
72 pub fn offset(&self) -> usize {
73 self.offset
74 }
75
76 #[inline(always)]
78 pub fn len(&self) -> usize {
79 self.len
80 }
81
82 #[inline(always)]
84 pub fn is_empty(&self) -> bool {
85 self.len == 0
86 }
87
88 #[inline]
90 pub fn byte_len(&self) -> usize {
91 (self.offset + self.len).div_ceil(8)
92 }
93}