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
4 changes: 2 additions & 2 deletions vortex-buffer/benches/vortex_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<T: ArrowNativeType, R: ArrowNativeType> MapEach<T, R> for Arrow<ScalarBuffe
}
}

impl<T: Copy, R> MapEach<T, R> for Buffer<T> {
impl<T: Copy, R: Copy> MapEach<T, R> for Buffer<T> {
type Output = BufferMut<R>;

fn map_each<F>(self, f: F) -> Self::Output
Expand All @@ -86,7 +86,7 @@ impl<T: Copy, R> MapEach<T, R> for Buffer<T> {
}
}

impl<T: Copy, R> MapEach<T, R> for BufferMut<T> {
impl<T: Copy, R: Copy> MapEach<T, R> for BufferMut<T> {
type Output = BufferMut<R>;

fn map_each<F>(self, f: F) -> Self::Output
Expand Down
1 change: 1 addition & 0 deletions vortex-buffer/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ impl<T> Buffer<T> {
pub fn map_each_in_place<R, F>(self, mut f: F) -> BufferMut<R>
where
T: Copy,
R: Copy,
F: FnMut(T) -> R,
{
match self.try_into_mut() {
Expand Down
16 changes: 15 additions & 1 deletion vortex-buffer/src/buffer_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ impl<T> BufferMut<T> {
pub fn map_each_in_place<R, F>(self, mut f: F) -> BufferMut<R>
where
T: Copy,
R: Copy,
F: FnMut(T) -> R,
{
assert_eq!(
Expand All @@ -796,7 +797,9 @@ impl<T> BufferMut<T> {
let mut buf: BufferMut<R> = unsafe { std::mem::transmute(self) };
buf.iter_mut()
.for_each(|item| *item = f(unsafe { std::mem::transmute_copy(item) }));
buf
// `transmute` preserves `T`'s alignment, which can be weaker than `R`'s.
let alignment = buf.alignment().max(Alignment::of::<R>());
buf.aligned(alignment)
}

/// Return a `BufferMut<T>` with the same data as this one with the given alignment.
Expand Down Expand Up @@ -1074,6 +1077,7 @@ fn misaligned_scalar_type(alignment: Alignment, scalar_align: Alignment) -> ! {

#[cfg(test)]
mod tests {
use std::alloc::Layout;
use std::cell::Cell;

use allocator_api2::alloc::Global;
Expand Down Expand Up @@ -1298,6 +1302,16 @@ mod tests {
assert_eq!(buf.as_slice(), &[1u32, 2, 3]);
}

#[test]
fn map_each_in_place_keeps_output_alignment_valid() {
let bytes = BufferMut::<[u8; 4]>::copy_from_aligned([[1u8, 0, 0, 0]], Alignment::new(1));
assert_eq!(1, Layout::new::<[u8; 4]>().align());
let words = bytes.map_each_in_place(u32::from_ne_bytes);

assert_eq!(words.as_slice(), [1]);
assert!(words.alignment().is_aligned_to(Alignment::of::<u32>()));
}

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