Skip to content

Commit 3816cb3

Browse files
authored
BufferIterator can be Send + Sync (#8873)
BufferIterator is Send and Sync just like Vec IntoIter, fix #8869 Signed-off-by: Robert Kruszewski <github@robertk.io>
1 parent 061b6cb commit 3816cb3

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

vortex-buffer/src/buffer.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,11 @@ pub struct BufferIterator<T: Copy> {
713713
end: *const T,
714714
}
715715

716+
// SAFETY: `BufferIterator` is a `Buffer<T>` plus two cursors into it, so it can safely be
717+
// `Send`/`Sync` exactly when `Buffer<T>` is. Same bounds as `std::vec::IntoIter`.
718+
unsafe impl<T: Copy + Send> Send for BufferIterator<T> {}
719+
unsafe impl<T: Copy + Sync> Sync for BufferIterator<T> {}
720+
716721
impl<T: Copy> Iterator for BufferIterator<T> {
717722
type Item = T;
718723

@@ -777,6 +782,17 @@ mod test {
777782
assert_eq!(aligned.as_slice(), &[0, 1, 2]);
778783
}
779784

785+
#[test]
786+
fn buffer_iterator_send_sync() {
787+
fn assert_send_sync<T: Send + Sync>(_: &T) {}
788+
789+
let mut iter = buffer![0i32, 1, 2, 3].into_iter();
790+
assert_send_sync(&iter);
791+
iter.next();
792+
let remaining: Vec<i32> = std::thread::spawn(move || iter.collect()).join().unwrap();
793+
assert_eq!(remaining, vec![1, 2, 3]);
794+
}
795+
780796
#[test]
781797
fn slice() {
782798
let buf = buffer![0, 1, 2, 3, 4];

0 commit comments

Comments
 (0)