-
Notifications
You must be signed in to change notification settings - Fork 191
BufferIterator can be Send + Sync #8873
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -713,6 +713,11 @@ pub struct BufferIterator<T: Copy> { | |
| end: *const T, | ||
| } | ||
|
|
||
| // SAFETY: `BufferIterator` is a `Buffer<T>` plus two cursors into it, so it can safely be | ||
| // `Send`/`Sync` exactly when `Buffer<T>` is. Same bounds as `std::vec::IntoIter`. | ||
| unsafe impl<T: Copy + Send> Send for BufferIterator<T> {} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @claude Can't we copy the iterator and share across threads such that it aliases and races?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Claude finished @0ax1's task in 57s —— View job Re: "Can't we copy the iterator and share across threads such that it aliases and races?"Short answer: no, this can't cause a data race. A few reasons stack up: 1. 2. Even if it were cloneable, there'd still be no race. 3. 4. The bounds match the standard library. So the impls are sound: the cursors are owned per-iterator, the buffer is read-only during iteration, and mutation is gated behind
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @claude this is sound then?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Claude finished @0ax1's task in 30s —— View job Yes — this is sound. ✅I re-read the current code at 1. The bounds match unsafe impl<T: Copy + Send> Send for BufferIterator<T> {}
unsafe impl<T: Copy + Sync> Sync for BufferIterator<T> {}These are the same bounds 2. No aliasing is possible. 3. Iteration is read-only. 4. One optional nit (not a soundness issue): So: the impls are sound, and equivalent to why |
||
| unsafe impl<T: Copy + Sync> Sync for BufferIterator<T> {} | ||
|
|
||
| impl<T: Copy> Iterator for BufferIterator<T> { | ||
| type Item = T; | ||
|
|
||
|
|
@@ -777,6 +782,17 @@ mod test { | |
| assert_eq!(aligned.as_slice(), &[0, 1, 2]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn buffer_iterator_send_sync() { | ||
| fn assert_send_sync<T: Send + Sync>(_: &T) {} | ||
|
|
||
| let mut iter = buffer![0i32, 1, 2, 3].into_iter(); | ||
| assert_send_sync(&iter); | ||
| iter.next(); | ||
| let remaining: Vec<i32> = std::thread::spawn(move || iter.collect()).join().unwrap(); | ||
| assert_eq!(remaining, vec![1, 2, 3]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn slice() { | ||
| let buf = buffer![0, 1, 2, 3, 4]; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't the compiler auto-derive Send + Sync if T is so?Right bc of the pointer