Skip to content

Commit a12d405

Browse files
conradludgateBurntSushi
authored andcommitted
api: make the memchr iterators implement Send/Sync
They did this previously, but due to some internally re-shuffling, these impls got dropped accidentally. (Because the iterator switched to using raw pointers.) They are still Send/Sync, we just need to explicitly opt into it now. We also add a regression test to ensure we don't make this mistake again. Fixes #133, Closes #134
1 parent 3a570a9 commit a12d405

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/arch/generic/memchr.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,16 @@ pub(crate) struct Iter<'h> {
10111011
haystack: core::marker::PhantomData<&'h [u8]>,
10121012
}
10131013

1014+
// SAFETY: Iter contains no shared references to anything that performs any
1015+
// interior mutations. Also, the lifetime guarantees that Iter will not outlive
1016+
// the haystack.
1017+
unsafe impl<'h> Send for Iter<'h> {}
1018+
1019+
// SAFETY: Iter perform no interior mutations, therefore no explicit
1020+
// synchronization is necessary. Also, the lifetime guarantees that Iter will
1021+
// not outlive the haystack.
1022+
unsafe impl<'h> Sync for Iter<'h> {}
1023+
10141024
impl<'h> Iter<'h> {
10151025
/// Create a new generic memchr iterator.
10161026
#[inline(always)]

src/memchr.rs

+16
Original file line numberDiff line numberDiff line change
@@ -884,4 +884,20 @@ mod tests {
884884
},
885885
)
886886
}
887+
888+
// Prior to memchr 2.6, the memchr iterators both implemented Send and
889+
// Sync. But in memchr 2.6, the iterator changed to use raw pointers
890+
// internally and I didn't add explicit Send/Sync impls. This ended up
891+
// regressing the API. This test ensures we don't do that again.
892+
//
893+
// See: https://github.com/BurntSushi/memchr/issues/133
894+
#[test]
895+
fn sync_regression() {
896+
use core::panic::{RefUnwindSafe, UnwindSafe};
897+
898+
fn assert_send_sync<T: Send + Sync + UnwindSafe + RefUnwindSafe>() {}
899+
assert_send_sync::<Memchr>();
900+
assert_send_sync::<Memchr2>();
901+
assert_send_sync::<Memchr3>()
902+
}
887903
}

0 commit comments

Comments
 (0)