Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document safety justification of some uses of from_trusted_len_iter #1148

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion arrow/src/array/array_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> {

/// Creates a PrimitiveArray based on a constant value with `count` elements
pub fn from_value(value: T::Native, count: usize) -> Self {
// # Safety: length is known
// # Safety: iterator (0..count) correctly reports its length
let val_buf = unsafe { Buffer::from_trusted_len_iter((0..count).map(|_| value)) };
let data = unsafe {
ArrayData::new_unchecked(
Expand Down
3 changes: 2 additions & 1 deletion arrow/src/buffer/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ where
.iter()
.zip(right_chunks.iter())
.map(|(left, right)| op(left, right));
// Soundness: `BitChunks` is a trusted len iterator
// Soundness: `BitChunks` is a `BitChunks` iterator which
// correctly reports its upper bound
let mut buffer = unsafe { MutableBuffer::from_trusted_len_iter(chunks) };

let remainder_bytes = ceil(left_chunks.remainder_len(), 8);
Expand Down
6 changes: 5 additions & 1 deletion arrow/src/compute/kernels/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ where
// Benefit
// ~60% speedup
// Soundness
// `values` is an iterator with a known size.
// `values` is an iterator with a known size from a PrimitiveArray
let buffer = unsafe { Buffer::from_trusted_len_iter(values) };

let data = unsafe {
Expand Down Expand Up @@ -241,6 +241,7 @@ where
}
},
);
// Safety: Iterator comes from a PrimitiveArray which reports its size correctly
unsafe { Buffer::try_from_trusted_len_iter(values) }
} else {
// no value is null
Expand All @@ -255,6 +256,7 @@ where
Ok(*left % *right)
}
});
// Safety: Iterator comes from a PrimitiveArray which reports its size correctly
unsafe { Buffer::try_from_trusted_len_iter(values) }
}?;

Expand Down Expand Up @@ -311,6 +313,7 @@ where
}
},
);
// Safety: Iterator comes from a PrimitiveArray which reports its size correctly
unsafe { Buffer::try_from_trusted_len_iter(values) }
} else {
// no value is null
Expand All @@ -325,6 +328,7 @@ where
Ok(*left / *right)
}
});
// Safety: Iterator comes from a PrimitiveArray which reports its size correctly
unsafe { Buffer::try_from_trusted_len_iter(values) }
}?;

Expand Down
2 changes: 1 addition & 1 deletion arrow/src/compute/kernels/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ where
// Benefit
// ~60% speedup
// Soundness
// `values` is an iterator with a known size.
// `values` come from a slice iterator with a known size.
let buffer = unsafe { Buffer::from_trusted_len_iter(lengths) };

let null_bit_buffer = array
Expand Down