Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions vortex-buffer/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ impl<T> Buffer<T> {
///
/// # Panics
///
/// Requires that `begin <= end` and `end <= self.len()`.
/// Also requires that both `begin` and `end` are aligned to the given alignment.
/// Requires that `begin <= end` and `end <= self.len()`. The buffer and the byte offset at
/// `begin` must be aligned to `alignment`.
pub fn slice_with_alignment(
&self,
range: impl RangeBounds<usize>,
Expand Down Expand Up @@ -374,6 +374,12 @@ impl<T> Buffer<T> {
let begin_byte = begin * size_of::<T>();
let end_byte = end * size_of::<T>();

if !self.alignment.is_aligned_to(alignment) {
vortex_panic!(
"Slice alignment {alignment} must be wider than buffer alignment {}",
self.alignment
);
}
if !alignment.is_offset_aligned(begin_byte) {
vortex_panic!(
"range start must be aligned to {alignment:?}, byte {}",
Expand Down Expand Up @@ -815,6 +821,15 @@ mod test {
buf.slice(1..2);
}

#[test]
#[should_panic(expected = "must not be stronger than buffer alignment")]
fn test_slice_with_alignment_cannot_strengthen_alignment() {
let buf = ByteBuffer::from(vec![0u8, 1]);
assert_eq!(buf.alignment(), Alignment::of::<u8>());

buf.slice_with_alignment(0..1, Alignment::new(256));
}

#[test]
fn bytes_buf() {
let mut buf = ByteBuffer::copy_from("helloworld".as_bytes());
Expand Down
13 changes: 13 additions & 0 deletions vortex-buffer/src/buffer_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,19 @@ mod test {
assert_eq!(buf.as_slice(), b"helloworld");
}

#[test]
fn test_preferred_alignment_is_not_reported() {
let alignment = Alignment::of::<u32>();
let buf = BufferMut::<u32>::with_capacity_preferred_aligned(
1,
alignment,
Some(Alignment::DEFAULT_ALIGNMENT),
);

assert_eq!(buf.alignment(), alignment);
assert_eq!(buf.freeze().alignment(), alignment);
}

#[test]
fn buffer_mut_zeroed() {
const LEN: usize = 17;
Expand Down
Loading