Skip to content

Commit

Permalink
Document safety justification of some uses of from_trusted_len_iter
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Jan 9, 2022
1 parent 719096b commit 2984b99
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
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

0 comments on commit 2984b99

Please sign in to comment.