diff --git a/vortex-buffer/src/buffer.rs b/vortex-buffer/src/buffer.rs index a59fff825f8..ae7224d7860 100644 --- a/vortex-buffer/src/buffer.rs +++ b/vortex-buffer/src/buffer.rs @@ -335,8 +335,8 @@ impl Buffer { /// /// # 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, @@ -374,6 +374,12 @@ impl Buffer { let begin_byte = begin * size_of::(); let end_byte = end * size_of::(); + 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 {}", @@ -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::()); + + buf.slice_with_alignment(0..1, Alignment::new(256)); + } + #[test] fn bytes_buf() { let mut buf = ByteBuffer::copy_from("helloworld".as_bytes()); diff --git a/vortex-buffer/src/buffer_mut.rs b/vortex-buffer/src/buffer_mut.rs index e5cb03c558b..2f006e3e75e 100644 --- a/vortex-buffer/src/buffer_mut.rs +++ b/vortex-buffer/src/buffer_mut.rs @@ -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::(); + let buf = BufferMut::::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;