Skip to content

Commit c6250bd

Browse files
committed
Complete new storage
1 parent c8078f9 commit c6250bd

File tree

9 files changed

+40
-25
lines changed

9 files changed

+40
-25
lines changed

async/src/alias.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::{
44
};
55
#[cfg(feature = "alloc")]
66
use alloc::sync::Arc;
7+
use ringbuf::{storage::Array, SharedRb};
78
#[cfg(feature = "alloc")]
89
use ringbuf::{storage::Heap, HeapRb};
9-
use ringbuf::{storage::Static, SharedRb};
1010

1111
#[cfg(feature = "alloc")]
1212
pub type AsyncHeapRb<T> = AsyncRb<Heap<T>>;
@@ -22,11 +22,11 @@ impl<T> AsyncHeapRb<T> {
2222
}
2323
}
2424

25-
pub type AsyncStaticRb<T, const N: usize> = AsyncRb<Static<T, N>>;
25+
pub type AsyncStaticRb<T, const N: usize> = AsyncRb<Array<T, N>>;
2626
pub type AsyncStaticProd<'a, T, const N: usize> = AsyncProd<&'a AsyncStaticRb<T, N>>;
2727
pub type AsyncStaticCons<'a, T, const N: usize> = AsyncCons<&'a AsyncStaticRb<T, N>>;
2828

29-
impl<T, const N: usize> Default for AsyncRb<Static<T, N>> {
29+
impl<T, const N: usize> Default for AsyncRb<Array<T, N>> {
3030
fn default() -> Self {
3131
AsyncRb::from(SharedRb::default())
3232
}

async/src/rb.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ impl<S: Storage> Observer for AsyncRb<S> {
5454
self.base.write_index()
5555
}
5656

57-
unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&mut [MaybeUninit<S::Item>], &mut [MaybeUninit<S::Item>]) {
57+
unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&[MaybeUninit<S::Item>], &[MaybeUninit<S::Item>]) {
5858
self.base.unsafe_slices(start, end)
5959
}
60+
unsafe fn unsafe_slices_mut(&self, start: usize, end: usize) -> (&mut [MaybeUninit<S::Item>], &mut [MaybeUninit<S::Item>]) {
61+
self.base.unsafe_slices_mut(start, end)
62+
}
6063

6164
#[inline]
6265
fn read_is_held(&self) -> bool {

async/src/traits/consumer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub trait AsyncConsumer: Consumer {
9696
}
9797
}
9898

99-
pub struct PopFuture<'a, A: AsyncConsumer> {
99+
pub struct PopFuture<'a, A: AsyncConsumer + ?Sized> {
100100
owner: &'a mut A,
101101
done: bool,
102102
}
@@ -130,7 +130,7 @@ impl<'a, A: AsyncConsumer> Future for PopFuture<'a, A> {
130130
}
131131
}
132132

133-
pub struct PopSliceFuture<'a, 'b, A: AsyncConsumer>
133+
pub struct PopSliceFuture<'a, 'b, A: AsyncConsumer + ?Sized>
134134
where
135135
A::Item: Copy,
136136
{
@@ -177,7 +177,7 @@ where
177177
}
178178
}
179179

180-
pub struct WaitOccupiedFuture<'a, A: AsyncConsumer> {
180+
pub struct WaitOccupiedFuture<'a, A: AsyncConsumer + ?Sized> {
181181
owner: &'a A,
182182
count: usize,
183183
done: bool,

async/src/traits/producer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub trait AsyncProducer: Producer {
110110
}
111111
}
112112

113-
pub struct PushFuture<'a, A: AsyncProducer> {
113+
pub struct PushFuture<'a, A: AsyncProducer + ?Sized> {
114114
owner: &'a mut A,
115115
item: Option<A::Item>,
116116
}
@@ -144,7 +144,7 @@ impl<'a, A: AsyncProducer> Future for PushFuture<'a, A> {
144144
}
145145
}
146146

147-
pub struct PushSliceFuture<'a, 'b, A: AsyncProducer>
147+
pub struct PushSliceFuture<'a, 'b, A: AsyncProducer + ?Sized>
148148
where
149149
A::Item: Copy,
150150
{
@@ -190,7 +190,7 @@ where
190190
}
191191
}
192192

193-
pub struct PushIterFuture<'a, A: AsyncProducer, I: Iterator<Item = A::Item>> {
193+
pub struct PushIterFuture<'a, A: AsyncProducer + ?Sized, I: Iterator<Item = A::Item>> {
194194
owner: &'a mut A,
195195
iter: Option<Peekable<I>>,
196196
}
@@ -224,7 +224,7 @@ impl<'a, A: AsyncProducer, I: Iterator<Item = A::Item>> Future for PushIterFutur
224224
}
225225
}
226226

227-
pub struct WaitVacantFuture<'a, A: AsyncProducer> {
227+
pub struct WaitVacantFuture<'a, A: AsyncProducer + ?Sized> {
228228
owner: &'a A,
229229
count: usize,
230230
done: bool,

blocking/src/alias.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#[cfg(feature = "std")]
22
use crate::sync::StdSemaphore;
33
use crate::{rb::BlockingRb, sync::Semaphore};
4+
use ringbuf::{storage::Array, SharedRb};
45
#[cfg(feature = "alloc")]
56
use ringbuf::{storage::Heap, HeapRb};
6-
use ringbuf::{storage::Static, SharedRb};
77

88
#[cfg(feature = "std")]
99
pub type BlockingHeapRb<T, X = StdSemaphore> = BlockingRb<Heap<T>, X>;
@@ -18,11 +18,11 @@ impl<T, X: Semaphore> BlockingHeapRb<T, X> {
1818
}
1919

2020
#[cfg(feature = "std")]
21-
pub type BlockingStaticRb<T, const N: usize, X = StdSemaphore> = BlockingRb<Static<T, N>, X>;
21+
pub type BlockingStaticRb<T, const N: usize, X = StdSemaphore> = BlockingRb<Array<T, N>, X>;
2222
#[cfg(all(feature = "alloc", not(feature = "std")))]
23-
pub type BlockingStaticRb<T, const N: usize, X> = BlockingRb<Static<T, N>, X>;
23+
pub type BlockingStaticRb<T, const N: usize, X> = BlockingRb<Array<T, N>, X>;
2424

25-
impl<T, const N: usize, X: Semaphore> Default for BlockingRb<Static<T, N>, X> {
25+
impl<T, const N: usize, X: Semaphore> Default for BlockingRb<Array<T, N>, X> {
2626
fn default() -> Self {
2727
BlockingRb::from(SharedRb::default())
2828
}

blocking/src/rb.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ impl<S: Storage, X: Semaphore> Observer for BlockingRb<S, X> {
5353
self.base.write_index()
5454
}
5555

56-
unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&mut [MaybeUninit<S::Item>], &mut [MaybeUninit<S::Item>]) {
56+
unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&[MaybeUninit<S::Item>], &[MaybeUninit<S::Item>]) {
5757
self.base.unsafe_slices(start, end)
5858
}
59+
unsafe fn unsafe_slices_mut(&self, start: usize, end: usize) -> (&mut [MaybeUninit<S::Item>], &mut [MaybeUninit<S::Item>]) {
60+
self.base.unsafe_slices_mut(start, end)
61+
}
5962

6063
#[inline]
6164
fn read_is_held(&self) -> bool {

src/benchmarks/base.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use crate::{storage::Static, traits::*, LocalRb, SharedRb};
1+
use crate::{storage::Array, traits::*, LocalRb, SharedRb};
22
use test::{black_box, Bencher};
33

44
const RB_SIZE: usize = 256;
55
const BATCH_SIZE: usize = 100;
66

77
#[bench]
88
fn push_pop_shared(b: &mut Bencher) {
9-
let buf = SharedRb::<Static<u64, RB_SIZE>>::default();
9+
let buf = SharedRb::<Array<u64, RB_SIZE>>::default();
1010
let (mut prod, mut cons) = buf.split();
1111
prod.push_slice(&[1; RB_SIZE / 2]);
1212
b.iter(|| {
@@ -17,7 +17,7 @@ fn push_pop_shared(b: &mut Bencher) {
1717

1818
#[bench]
1919
fn push_pop_local(b: &mut Bencher) {
20-
let buf = LocalRb::<Static<u64, RB_SIZE>>::default();
20+
let buf = LocalRb::<Array<u64, RB_SIZE>>::default();
2121
let (mut prod, mut cons) = buf.split();
2222
prod.push_slice(&[1; RB_SIZE / 2]);
2323
b.iter(|| {
@@ -28,7 +28,7 @@ fn push_pop_local(b: &mut Bencher) {
2828

2929
#[bench]
3030
fn push_pop_x100(b: &mut Bencher) {
31-
let buf = SharedRb::<Static<u64, RB_SIZE>>::default();
31+
let buf = SharedRb::<Array<u64, RB_SIZE>>::default();
3232
let (mut prod, mut cons) = buf.split();
3333
prod.push_slice(&[1; RB_SIZE / 2]);
3434
b.iter(|| {

src/benchmarks/parts.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::{storage::Static, traits::*, SharedRb};
1+
use crate::{storage::Array, traits::*, SharedRb};
22
use test::{black_box, Bencher};
33

44
const RB_SIZE: usize = 256;
55

66
#[bench]
77
fn advance(b: &mut Bencher) {
8-
let buf = SharedRb::<Static<u64, RB_SIZE>>::default();
8+
let buf = SharedRb::<Array<u64, RB_SIZE>>::default();
99
let (mut prod, cons) = buf.split();
1010
prod.push_slice(&[1; RB_SIZE / 2]);
1111
b.iter(|| {
@@ -16,7 +16,7 @@ fn advance(b: &mut Bencher) {
1616

1717
#[bench]
1818
fn get_occupied_slices(b: &mut Bencher) {
19-
let buf = SharedRb::<Static<u64, RB_SIZE>>::default();
19+
let buf = SharedRb::<Array<u64, RB_SIZE>>::default();
2020
let (mut prod, mut cons) = buf.split();
2121
prod.push_slice(&[0; 3 * RB_SIZE / 4]);
2222
cons.skip(RB_SIZE);
@@ -29,7 +29,7 @@ fn get_occupied_slices(b: &mut Bencher) {
2929

3030
#[bench]
3131
fn get_vacant_slices(b: &mut Bencher) {
32-
let buf = SharedRb::<Static<u64, RB_SIZE>>::default();
32+
let buf = SharedRb::<Array<u64, RB_SIZE>>::default();
3333
let (mut prod, mut cons) = buf.split();
3434
prod.push_slice(&[0; 1 * RB_SIZE / 4]);
3535
cons.skip(RB_SIZE);

src/storage.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#[cfg(feature = "alloc")]
22
use alloc::{boxed::Box, vec::Vec};
3-
use core::{cell::UnsafeCell, marker::PhantomData, mem::MaybeUninit, ops::Range, ptr, ptr::NonNull, slice};
3+
#[cfg(feature = "alloc")]
4+
use core::ptr;
5+
use core::{cell::UnsafeCell, marker::PhantomData, mem::MaybeUninit, ops::Range, ptr::NonNull, slice};
46

57
/// Abstract storage for the ring buffer.
68
///
@@ -128,12 +130,16 @@ unsafe impl<T> Storage for Slice<T> {
128130
}
129131
}
130132

133+
#[cfg(feature = "alloc")]
131134
pub struct Heap<T> {
132135
ptr: *mut MaybeUninit<T>,
133136
len: usize,
134137
}
138+
#[cfg(feature = "alloc")]
135139
unsafe impl<T> Send for Heap<T> where T: Send {}
140+
#[cfg(feature = "alloc")]
136141
unsafe impl<T> Sync for Heap<T> where T: Sync {}
142+
#[cfg(feature = "alloc")]
137143
unsafe impl<T> Storage for Heap<T> {
138144
type Item = T;
139145
#[inline]
@@ -154,6 +160,7 @@ impl<T> Heap<T> {
154160
}
155161
}
156162
}
163+
#[cfg(feature = "alloc")]
157164
impl<T> From<Box<[MaybeUninit<T>]>> for Heap<T> {
158165
fn from(value: Box<[MaybeUninit<T>]>) -> Self {
159166
Self {
@@ -162,11 +169,13 @@ impl<T> From<Box<[MaybeUninit<T>]>> for Heap<T> {
162169
}
163170
}
164171
}
172+
#[cfg(feature = "alloc")]
165173
impl<T> From<Heap<T>> for Box<[MaybeUninit<T>]> {
166174
fn from(value: Heap<T>) -> Self {
167175
unsafe { Box::from_raw(ptr::slice_from_raw_parts_mut(value.ptr, value.len)) }
168176
}
169177
}
178+
#[cfg(feature = "alloc")]
170179
impl<T> Drop for Heap<T> {
171180
fn drop(&mut self) {
172181
drop(unsafe { Box::from_raw(ptr::slice_from_raw_parts_mut(self.ptr, self.len)) });

0 commit comments

Comments
 (0)