Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ jobs:
- name: install miri
run: rustup install nightly && rustup +nightly component add miri
- name: miri
run: cargo +nightly miri test --all-features --many-seeds
run: cargo +nightly miri test --all-features
env:
MIRIFLAGS: "-Zmiri-many-seeds"
- name: loom
run: cargo test --release --lib
env:
Expand Down
8 changes: 4 additions & 4 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ where
dequeuing: DequeuingLength,
notify_enqueue: impl Fn(),
resize: Option<impl FnOnce(&mut B) -> (bool, usize)>,
) -> Result<BufferSlice<B, N>, TryDequeueError> {
) -> Result<BufferSlice<'_, B, N>, TryDequeueError> {
// If dequeuing length is greater than zero, it means than previous dequeuing is still
// ongoing, either because previous `try_dequeue` operation returns pending error,
// or because requeuing (after partial draining for example).
Expand Down Expand Up @@ -450,7 +450,7 @@ where
&self,
buffer_index: usize,
length: NonZeroUsize,
) -> Option<BufferSlice<B, N>> {
) -> Option<BufferSlice<'_, B, N>> {
for _ in 0..SPIN_LIMIT {
// Buffers having been swapped, no more enqueuing can happen, we still need to wait
// for ongoing one. They will be finished when the buffer length (updated after
Expand Down Expand Up @@ -645,7 +645,7 @@ where
/// }
/// assert_eq!(queue.try_dequeue().unwrap_err(), TryDequeueError::Closed)
/// ```
pub fn try_dequeue(&self) -> Result<BufferSlice<B, N>, TryDequeueError> {
pub fn try_dequeue(&self) -> Result<BufferSlice<'_, B, N>, TryDequeueError> {
self.try_dequeue_internal(
self.lock_dequeuing()?,
|| self.notify.notify_enqueue(),
Expand Down Expand Up @@ -788,7 +788,7 @@ where
&self,
capacity: impl Into<Option<usize>>,
insert: Option<impl FnOnce() -> I>,
) -> Result<BufferSlice<B, N>, TryDequeueError>
) -> Result<BufferSlice<'_, B, N>, TryDequeueError>
where
I: IntoIterator,
I::Item: InsertIntoBuffer<B>,
Expand Down
8 changes: 4 additions & 4 deletions src/synchronized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ where
fn dequeue_sync(
&self,
deadline: Option<Instant>,
) -> Result<BufferSlice<B, SynchronizedNotifier>, TryDequeueError> {
) -> Result<BufferSlice<'_, B, SynchronizedNotifier>, TryDequeueError> {
loop {
if let Some(res) = try_dequeue(self, None) {
return res;
Expand Down Expand Up @@ -268,7 +268,7 @@ where
/// queue.close();
/// assert_eq!(task.join().unwrap().unwrap_err(), DequeueError::Closed);
/// ```
pub fn dequeue(&self) -> Result<BufferSlice<B, SynchronizedNotifier>, DequeueError> {
pub fn dequeue(&self) -> Result<BufferSlice<'_, B, SynchronizedNotifier>, DequeueError> {
self.dequeue_sync(None).map_err(dequeue_err)
}

Expand Down Expand Up @@ -308,7 +308,7 @@ where
pub fn dequeue_timeout(
&self,
timeout: Duration,
) -> Result<BufferSlice<B, SynchronizedNotifier>, TryDequeueError> {
) -> Result<BufferSlice<'_, B, SynchronizedNotifier>, TryDequeueError> {
self.dequeue_sync(Some(Instant::now() + timeout))
}

Expand Down Expand Up @@ -360,7 +360,7 @@ where
/// ```
pub async fn dequeue_async(
&self,
) -> Result<BufferSlice<B, SynchronizedNotifier>, DequeueError> {
) -> Result<BufferSlice<'_, B, SynchronizedNotifier>, DequeueError> {
poll_fn(|cx| {
if let Some(res) = try_dequeue(self, Some(cx)) {
return Poll::Ready(res.map_err(dequeue_err));
Expand Down
28 changes: 17 additions & 11 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use core::{
mem,
mem::MaybeUninit,
ops::{Deref, DerefMut},
slice,
};
use core::{mem, mem::MaybeUninit};

pub(crate) fn init_array<T, const N: usize>(default: impl Fn() -> T) -> [T; N] {
// SAFETY: common MaybeUninit pattern, used in unstable `MaybeUninit::uninit_array`
Expand All @@ -12,11 +7,12 @@ pub(crate) fn init_array<T, const N: usize>(default: impl Fn() -> T) -> [T; N] {
elem.write(default());
}
// SAFETY: all elements have been initialized
// I used `std::mem::transmute_copy` because `transmute` doesn't work here
// I used `core::mem::transmute_copy` because `transmute` doesn't work here
// see https://users.rust-lang.org/t/transmuting-a-generic-array/45645
unsafe { mem::transmute_copy(&array) }
}

#[cfg(feature = "write")]
/// A hack for const-expression-sized array, as discussed here:
/// https://users.rust-lang.org/t/is-slice-from-raw-parts-unsound-in-case-of-a-repr-c-struct-with-consecutive-arrays/88368
#[repr(C)]
Expand All @@ -31,29 +27,38 @@ pub(crate) struct ArrayWithHeaderAndTrailer<
trailer: [T; TRAILER_SIZE],
}

impl<T, const HEADER_SIZE: usize, const N: usize, const TRAILER_SIZE: usize> Deref
#[cfg(feature = "write")]
impl<T, const HEADER_SIZE: usize, const N: usize, const TRAILER_SIZE: usize> core::ops::Deref
for ArrayWithHeaderAndTrailer<T, HEADER_SIZE, N, TRAILER_SIZE>
{
type Target = [T];
fn deref(&self) -> &Self::Target {
// SAFETY: see struct documentation
unsafe {
slice::from_raw_parts(self as *const _ as *const T, HEADER_SIZE + N + TRAILER_SIZE)
core::slice::from_raw_parts(
self as *const _ as *const T,
HEADER_SIZE + N + TRAILER_SIZE,
)
}
}
}

impl<T, const HEADER_SIZE: usize, const N: usize, const TRAILER_SIZE: usize> DerefMut
#[cfg(feature = "write")]
impl<T, const HEADER_SIZE: usize, const N: usize, const TRAILER_SIZE: usize> core::ops::DerefMut
for ArrayWithHeaderAndTrailer<T, HEADER_SIZE, N, TRAILER_SIZE>
{
fn deref_mut(&mut self) -> &mut Self::Target {
// SAFETY: see struct documentation
unsafe {
slice::from_raw_parts_mut(self as *mut _ as *mut T, HEADER_SIZE + N + TRAILER_SIZE)
core::slice::from_raw_parts_mut(
self as *mut _ as *mut T,
HEADER_SIZE + N + TRAILER_SIZE,
)
}
}
}

#[cfg(feature = "write")]
impl<T, const HEADER_SIZE: usize, const N: usize, const TRAILER_SIZE: usize>
ArrayWithHeaderAndTrailer<T, HEADER_SIZE, N, TRAILER_SIZE>
{
Expand All @@ -66,6 +71,7 @@ impl<T, const HEADER_SIZE: usize, const N: usize, const TRAILER_SIZE: usize>
}
}

#[cfg(feature = "write")]
impl<T, const HEADER_SIZE: usize, const N: usize, const TRAILER_SIZE: usize> Default
for ArrayWithHeaderAndTrailer<T, HEADER_SIZE, N, TRAILER_SIZE>
where
Expand Down