Skip to content

Commit 0390dbd

Browse files
committed
add into_mut methods on mask and buffer
Signed-off-by: Connor Tsui <[email protected]>
1 parent 423b93f commit 0390dbd

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

vortex-buffer/src/bit/buf.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use crate::{Alignment, BitBufferMut, Buffer, BufferMut, ByteBuffer, buffer};
1414
#[derive(Debug, Clone, Eq)]
1515
pub struct BitBuffer {
1616
buffer: ByteBuffer,
17+
/// Represents the offset of the bit buffer into the first byte.
18+
///
19+
/// This is always less than 8 (for when the bit buffer is not aligned to a byte).
1720
offset: usize,
1821
len: usize,
1922
}

vortex-buffer/src/bit/buf_mut.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ impl BitBufferMut {
6666
}
6767
}
6868

69+
/// Creates a `BitBufferMut` from a [`BitBuffer`] by copying all of the data over.
70+
pub fn copy_from(bit_buffer: &BitBuffer) -> Self {
71+
Self {
72+
buffer: ByteBufferMut::copy_from(bit_buffer.inner()),
73+
offset: bit_buffer.offset(),
74+
len: bit_buffer.len(),
75+
}
76+
}
77+
6978
/// Create a new empty mutable bit buffer with requested capacity (in bits).
7079
pub fn with_capacity(capacity: usize) -> Self {
7180
Self {

vortex-buffer/src/buffer.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::hash::{Hash, Hasher};
99
use std::marker::PhantomData;
1010
use std::ops::{Deref, RangeBounds};
1111

12-
use bytes::{Buf, Bytes};
12+
use bytes::{Buf, Bytes, BytesMut};
1313
use vortex_error::{VortexExpect, vortex_panic};
1414

1515
use crate::debug::TruncatedDebug;
@@ -399,12 +399,6 @@ impl<T> Buffer<T> {
399399
}
400400
}
401401

402-
/// Convert self into `BufferMut<T>`, copying if there are multiple strong references.
403-
pub fn into_mut(self) -> BufferMut<T> {
404-
self.try_into_mut()
405-
.unwrap_or_else(|buffer| BufferMut::<T>::copy_from(&buffer))
406-
}
407-
408402
/// Try to convert self into `BufferMut<T>` if there is only a single strong reference.
409403
pub fn try_into_mut(self) -> Result<BufferMut<T>, Self> {
410404
self.bytes
@@ -423,6 +417,18 @@ impl<T> Buffer<T> {
423417
})
424418
}
425419

420+
/// Convert self into `BufferMut<T>`, cloning the data if there are multiple strong references.
421+
pub fn into_mut(self) -> BufferMut<T> {
422+
let new_bytes = self.bytes.try_into_mut().unwrap_or_else(BytesMut::from);
423+
424+
BufferMut {
425+
bytes: new_bytes,
426+
length: self.length,
427+
alignment: self.alignment,
428+
_marker: Default::default(),
429+
}
430+
}
431+
426432
/// Returns whether a `Buffer<T>` is aligned to the given alignment.
427433
pub fn is_aligned(&self, alignment: Alignment) -> bool {
428434
self.bytes.as_ptr().align_offset(*alignment) == 0

vortex-mask/src/mask_mut.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ impl MaskMut {
300300
}
301301

302302
impl Mask {
303-
/// Attempts to convert an immutable mask into a mutable one.
303+
/// Attempts to convert an immutable mask into a mutable one, returning an error of `Self` if
304+
/// the underlying [`BitBuffer`] data if there are any other references.
304305
pub fn try_into_mut(self) -> Result<MaskMut, Self> {
305306
match self {
306307
Mask::AllTrue(len) => Ok(MaskMut::new_true(len)),
@@ -316,6 +317,29 @@ impl Mask {
316317
}
317318
}
318319
}
320+
321+
/// Convert an immutable mask into a mutable one, cloning the underlying [`BitBuffer`] data if
322+
/// there are any other references.
323+
pub fn into_mut(self) -> MaskMut {
324+
match self {
325+
Mask::AllTrue(len) => MaskMut::new_true(len),
326+
Mask::AllFalse(len) => MaskMut::new_false(len),
327+
Mask::Values(values) => {
328+
let bit_buffer_mut = match Arc::try_unwrap(values) {
329+
Ok(mask_values) => {
330+
let bit_buffer = mask_values.into_buffer();
331+
bit_buffer.into_mut()
332+
}
333+
Err(arc_mask_values) => {
334+
let bit_buffer = arc_mask_values.bit_buffer();
335+
BitBufferMut::copy_from(bit_buffer)
336+
}
337+
};
338+
339+
MaskMut(Inner::Builder(bit_buffer_mut))
340+
}
341+
}
342+
}
319343
}
320344

321345
#[cfg(test)]

0 commit comments

Comments
 (0)