diff --git a/async/src/alias.rs b/async/src/alias.rs index 09094b8..a38a9e1 100644 --- a/async/src/alias.rs +++ b/async/src/alias.rs @@ -4,9 +4,9 @@ use crate::{ }; #[cfg(feature = "alloc")] use alloc::sync::Arc; +use ringbuf::{storage::Array, SharedRb}; #[cfg(feature = "alloc")] use ringbuf::{storage::Heap, HeapRb}; -use ringbuf::{storage::Static, SharedRb}; #[cfg(feature = "alloc")] pub type AsyncHeapRb = AsyncRb>; @@ -22,11 +22,11 @@ impl AsyncHeapRb { } } -pub type AsyncStaticRb = AsyncRb>; +pub type AsyncStaticRb = AsyncRb>; pub type AsyncStaticProd<'a, T, const N: usize> = AsyncProd<&'a AsyncStaticRb>; pub type AsyncStaticCons<'a, T, const N: usize> = AsyncCons<&'a AsyncStaticRb>; -impl Default for AsyncRb> { +impl Default for AsyncRb> { fn default() -> Self { AsyncRb::from(SharedRb::default()) } diff --git a/async/src/rb.rs b/async/src/rb.rs index 6ea480a..b904306 100644 --- a/async/src/rb.rs +++ b/async/src/rb.rs @@ -54,9 +54,12 @@ impl Observer for AsyncRb { self.base.write_index() } - unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&mut [MaybeUninit], &mut [MaybeUninit]) { + unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&[MaybeUninit], &[MaybeUninit]) { self.base.unsafe_slices(start, end) } + unsafe fn unsafe_slices_mut(&self, start: usize, end: usize) -> (&mut [MaybeUninit], &mut [MaybeUninit]) { + self.base.unsafe_slices_mut(start, end) + } #[inline] fn read_is_held(&self) -> bool { diff --git a/async/src/traits/consumer.rs b/async/src/traits/consumer.rs index 4a19b3c..5e2abdf 100644 --- a/async/src/traits/consumer.rs +++ b/async/src/traits/consumer.rs @@ -96,7 +96,7 @@ pub trait AsyncConsumer: Consumer { } } -pub struct PopFuture<'a, A: AsyncConsumer> { +pub struct PopFuture<'a, A: AsyncConsumer + ?Sized> { owner: &'a mut A, done: bool, } @@ -130,7 +130,7 @@ impl<'a, A: AsyncConsumer> Future for PopFuture<'a, A> { } } -pub struct PopSliceFuture<'a, 'b, A: AsyncConsumer> +pub struct PopSliceFuture<'a, 'b, A: AsyncConsumer + ?Sized> where A::Item: Copy, { @@ -177,7 +177,7 @@ where } } -pub struct WaitOccupiedFuture<'a, A: AsyncConsumer> { +pub struct WaitOccupiedFuture<'a, A: AsyncConsumer + ?Sized> { owner: &'a A, count: usize, done: bool, diff --git a/async/src/traits/producer.rs b/async/src/traits/producer.rs index ad118bc..dbf17a3 100644 --- a/async/src/traits/producer.rs +++ b/async/src/traits/producer.rs @@ -110,7 +110,7 @@ pub trait AsyncProducer: Producer { } } -pub struct PushFuture<'a, A: AsyncProducer> { +pub struct PushFuture<'a, A: AsyncProducer + ?Sized> { owner: &'a mut A, item: Option, } @@ -144,7 +144,7 @@ impl<'a, A: AsyncProducer> Future for PushFuture<'a, A> { } } -pub struct PushSliceFuture<'a, 'b, A: AsyncProducer> +pub struct PushSliceFuture<'a, 'b, A: AsyncProducer + ?Sized> where A::Item: Copy, { @@ -190,7 +190,7 @@ where } } -pub struct PushIterFuture<'a, A: AsyncProducer, I: Iterator> { +pub struct PushIterFuture<'a, A: AsyncProducer + ?Sized, I: Iterator> { owner: &'a mut A, iter: Option>, } @@ -224,7 +224,7 @@ impl<'a, A: AsyncProducer, I: Iterator> Future for PushIterFutur } } -pub struct WaitVacantFuture<'a, A: AsyncProducer> { +pub struct WaitVacantFuture<'a, A: AsyncProducer + ?Sized> { owner: &'a A, count: usize, done: bool, diff --git a/blocking/src/alias.rs b/blocking/src/alias.rs index 9d875a8..1b48f4d 100644 --- a/blocking/src/alias.rs +++ b/blocking/src/alias.rs @@ -1,9 +1,9 @@ #[cfg(feature = "std")] use crate::sync::StdSemaphore; use crate::{rb::BlockingRb, sync::Semaphore}; +use ringbuf::{storage::Array, SharedRb}; #[cfg(feature = "alloc")] use ringbuf::{storage::Heap, HeapRb}; -use ringbuf::{storage::Static, SharedRb}; #[cfg(feature = "std")] pub type BlockingHeapRb = BlockingRb, X>; @@ -18,11 +18,11 @@ impl BlockingHeapRb { } #[cfg(feature = "std")] -pub type BlockingStaticRb = BlockingRb, X>; +pub type BlockingStaticRb = BlockingRb, X>; #[cfg(all(feature = "alloc", not(feature = "std")))] -pub type BlockingStaticRb = BlockingRb, X>; +pub type BlockingStaticRb = BlockingRb, X>; -impl Default for BlockingRb, X> { +impl Default for BlockingRb, X> { fn default() -> Self { BlockingRb::from(SharedRb::default()) } diff --git a/blocking/src/rb.rs b/blocking/src/rb.rs index db1ef12..3ee19a9 100644 --- a/blocking/src/rb.rs +++ b/blocking/src/rb.rs @@ -53,9 +53,12 @@ impl Observer for BlockingRb { self.base.write_index() } - unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&mut [MaybeUninit], &mut [MaybeUninit]) { + unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&[MaybeUninit], &[MaybeUninit]) { self.base.unsafe_slices(start, end) } + unsafe fn unsafe_slices_mut(&self, start: usize, end: usize) -> (&mut [MaybeUninit], &mut [MaybeUninit]) { + self.base.unsafe_slices_mut(start, end) + } #[inline] fn read_is_held(&self) -> bool { diff --git a/src/benchmarks/base.rs b/src/benchmarks/base.rs index 6019237..9e56f2c 100644 --- a/src/benchmarks/base.rs +++ b/src/benchmarks/base.rs @@ -1,4 +1,4 @@ -use crate::{storage::Static, traits::*, LocalRb, SharedRb}; +use crate::{storage::Array, traits::*, LocalRb, SharedRb}; use test::{black_box, Bencher}; const RB_SIZE: usize = 256; @@ -6,7 +6,7 @@ const BATCH_SIZE: usize = 100; #[bench] fn push_pop_shared(b: &mut Bencher) { - let buf = SharedRb::>::default(); + let buf = SharedRb::>::default(); let (mut prod, mut cons) = buf.split(); prod.push_slice(&[1; RB_SIZE / 2]); b.iter(|| { @@ -17,7 +17,7 @@ fn push_pop_shared(b: &mut Bencher) { #[bench] fn push_pop_local(b: &mut Bencher) { - let buf = LocalRb::>::default(); + let buf = LocalRb::>::default(); let (mut prod, mut cons) = buf.split(); prod.push_slice(&[1; RB_SIZE / 2]); b.iter(|| { @@ -28,7 +28,7 @@ fn push_pop_local(b: &mut Bencher) { #[bench] fn push_pop_x100(b: &mut Bencher) { - let buf = SharedRb::>::default(); + let buf = SharedRb::>::default(); let (mut prod, mut cons) = buf.split(); prod.push_slice(&[1; RB_SIZE / 2]); b.iter(|| { diff --git a/src/benchmarks/parts.rs b/src/benchmarks/parts.rs index 202271f..0988fe2 100644 --- a/src/benchmarks/parts.rs +++ b/src/benchmarks/parts.rs @@ -1,11 +1,11 @@ -use crate::{storage::Static, traits::*, SharedRb}; +use crate::{storage::Array, traits::*, SharedRb}; use test::{black_box, Bencher}; const RB_SIZE: usize = 256; #[bench] fn advance(b: &mut Bencher) { - let buf = SharedRb::>::default(); + let buf = SharedRb::>::default(); let (mut prod, cons) = buf.split(); prod.push_slice(&[1; RB_SIZE / 2]); b.iter(|| { @@ -16,7 +16,7 @@ fn advance(b: &mut Bencher) { #[bench] fn get_occupied_slices(b: &mut Bencher) { - let buf = SharedRb::>::default(); + let buf = SharedRb::>::default(); let (mut prod, mut cons) = buf.split(); prod.push_slice(&[0; 3 * RB_SIZE / 4]); cons.skip(RB_SIZE); @@ -29,7 +29,7 @@ fn get_occupied_slices(b: &mut Bencher) { #[bench] fn get_vacant_slices(b: &mut Bencher) { - let buf = SharedRb::>::default(); + let buf = SharedRb::>::default(); let (mut prod, mut cons) = buf.split(); prod.push_slice(&[0; 1 * RB_SIZE / 4]); cons.skip(RB_SIZE); diff --git a/src/storage.rs b/src/storage.rs index bfe3e01..063c1e1 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,6 +1,8 @@ #[cfg(feature = "alloc")] use alloc::{boxed::Box, vec::Vec}; -use core::{cell::UnsafeCell, marker::PhantomData, mem::MaybeUninit, ops::Range, ptr, ptr::NonNull, slice}; +#[cfg(feature = "alloc")] +use core::ptr; +use core::{cell::UnsafeCell, marker::PhantomData, mem::MaybeUninit, ops::Range, ptr::NonNull, slice}; /// Abstract storage for the ring buffer. /// @@ -128,12 +130,16 @@ unsafe impl Storage for Slice { } } +#[cfg(feature = "alloc")] pub struct Heap { ptr: *mut MaybeUninit, len: usize, } +#[cfg(feature = "alloc")] unsafe impl Send for Heap where T: Send {} +#[cfg(feature = "alloc")] unsafe impl Sync for Heap where T: Sync {} +#[cfg(feature = "alloc")] unsafe impl Storage for Heap { type Item = T; #[inline] @@ -154,6 +160,7 @@ impl Heap { } } } +#[cfg(feature = "alloc")] impl From]>> for Heap { fn from(value: Box<[MaybeUninit]>) -> Self { Self { @@ -162,11 +169,13 @@ impl From]>> for Heap { } } } +#[cfg(feature = "alloc")] impl From> for Box<[MaybeUninit]> { fn from(value: Heap) -> Self { unsafe { Box::from_raw(ptr::slice_from_raw_parts_mut(value.ptr, value.len)) } } } +#[cfg(feature = "alloc")] impl Drop for Heap { fn drop(&mut self) { drop(unsafe { Box::from_raw(ptr::slice_from_raw_parts_mut(self.ptr, self.len)) });