This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/master by this push:
new f085647 Document safety justification of some uses of
`from_trusted_len_iter` (#1148)
f085647 is described below
commit f0856473937ca1526b7f2bd140c865f00973fcdb
Author: Andrew Lamb <[email protected]>
AuthorDate: Tue Jan 11 14:10:01 2022 -0500
Document safety justification of some uses of `from_trusted_len_iter`
(#1148)
---
arrow/src/array/array_primitive.rs | 2 +-
arrow/src/buffer/ops.rs | 3 ++-
arrow/src/compute/kernels/arithmetic.rs | 6 +++++-
arrow/src/compute/kernels/length.rs | 2 +-
4 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/arrow/src/array/array_primitive.rs
b/arrow/src/array/array_primitive.rs
index a9e1ba8..e2272ed 100644
--- a/arrow/src/array/array_primitive.rs
+++ b/arrow/src/array/array_primitive.rs
@@ -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(
diff --git a/arrow/src/buffer/ops.rs b/arrow/src/buffer/ops.rs
index 14d3811..279c85e 100644
--- a/arrow/src/buffer/ops.rs
+++ b/arrow/src/buffer/ops.rs
@@ -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);
diff --git a/arrow/src/compute/kernels/arithmetic.rs
b/arrow/src/compute/kernels/arithmetic.rs
index 267c40f..0ee0608 100644
--- a/arrow/src/compute/kernels/arithmetic.rs
+++ b/arrow/src/compute/kernels/arithmetic.rs
@@ -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 {
@@ -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
@@ -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) }
}?;
@@ -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
@@ -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) }
}?;
diff --git a/arrow/src/compute/kernels/length.rs
b/arrow/src/compute/kernels/length.rs
index b0f3d9a..d6d554f 100644
--- a/arrow/src/compute/kernels/length.rs
+++ b/arrow/src/compute/kernels/length.rs
@@ -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