Skip to content

Commit

Permalink
api: make the memchr iterators implement Send/Sync
Browse files Browse the repository at this point in the history
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
  • Loading branch information
conradludgate authored and BurntSushi committed Aug 30, 2023
1 parent 3a570a9 commit a12d405
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/arch/generic/memchr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,16 @@ pub(crate) struct Iter<'h> {
haystack: core::marker::PhantomData<&'h [u8]>,
}

// SAFETY: Iter contains no shared references to anything that performs any
// interior mutations. Also, the lifetime guarantees that Iter will not outlive
// the haystack.
unsafe impl<'h> Send for Iter<'h> {}

// SAFETY: Iter perform no interior mutations, therefore no explicit
// synchronization is necessary. Also, the lifetime guarantees that Iter will
// not outlive the haystack.
unsafe impl<'h> Sync for Iter<'h> {}

impl<'h> Iter<'h> {
/// Create a new generic memchr iterator.
#[inline(always)]
Expand Down
16 changes: 16 additions & 0 deletions src/memchr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,4 +884,20 @@ mod tests {
},
)
}

// Prior to memchr 2.6, the memchr iterators both implemented Send and
// Sync. But in memchr 2.6, the iterator changed to use raw pointers
// internally and I didn't add explicit Send/Sync impls. This ended up
// regressing the API. This test ensures we don't do that again.
//
// See: https://github.com/BurntSushi/memchr/issues/133
#[test]
fn sync_regression() {
use core::panic::{RefUnwindSafe, UnwindSafe};

fn assert_send_sync<T: Send + Sync + UnwindSafe + RefUnwindSafe>() {}
assert_send_sync::<Memchr>();
assert_send_sync::<Memchr2>();
assert_send_sync::<Memchr3>()
}
}

0 comments on commit a12d405

Please sign in to comment.