Skip to content

Commit 7759201

Browse files
Genericise as_(mut_)view
1 parent 1a33fec commit 7759201

File tree

10 files changed

+592
-134
lines changed

10 files changed

+592
-134
lines changed

src/binary_heap.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,16 @@ impl<T, K, const N: usize> BinaryHeap<T, K, N> {
184184
pub fn into_vec(self) -> Vec<T, N> {
185185
self.data
186186
}
187+
}
187188

189+
impl<T, K, S: VecStorage<T>> BinaryHeapInner<T, K, S> {
188190
/// Get a reference to the `BinaryHeap`, erasing the `N` const-generic.
189191
pub fn as_view(&self) -> &BinaryHeapView<T, K> {
190-
self
192+
S::as_binary_heap_view(self)
191193
}
192194
/// Get a mutable reference to the `BinaryHeap`, erasing the `N` const-generic.
193195
pub fn as_mut_view(&mut self) -> &mut BinaryHeapView<T, K> {
194-
self
196+
S::as_binary_heap_mut_view(self)
195197
}
196198
}
197199

src/deque.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,19 @@ impl<T, const N: usize> Deque<T, N> {
186186
self.back - self.front
187187
}
188188
}
189+
}
189190

191+
impl<T, S: VecStorage<T> + ?Sized> DequeInner<T, S> {
190192
/// Get a reference to the `Deque`, erasing the `N` const-generic.
191193
pub fn as_view(&self) -> &DequeView<T> {
192-
self
194+
S::as_deque_view(self)
193195
}
194196

195197
/// Get a mutable reference to the `Deque`, erasing the `N` const-generic.
196198
pub fn as_mut_view(&mut self) -> &mut DequeView<T> {
197-
self
199+
S::as_deque_mut_view(self)
198200
}
199-
}
200201

201-
impl<T, S: VecStorage<T> + ?Sized> DequeInner<T, S> {
202202
/// Returns the maximum number of elements the deque can hold.
203203
pub fn storage_capacity(&self) -> usize {
204204
self.buffer.borrow().len()

src/histbuf.rs

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ use core::slice;
4141
mod storage {
4242
use core::mem::MaybeUninit;
4343

44+
use super::{HistoryBufferInner, HistoryBufferView};
45+
4446
/// Trait defining how data for a container is stored.
4547
///
4648
/// There's two implementations available:
@@ -72,6 +74,14 @@ mod storage {
7274
// part of the sealed trait so that no trait is publicly implemented by `OwnedHistBufStorage` besides `Storage`
7375
fn borrow(&self) -> &[MaybeUninit<T>];
7476
fn borrow_mut(&mut self) -> &mut [MaybeUninit<T>];
77+
fn as_hist_buf_view(this: &HistoryBufferInner<T, Self>) -> &HistoryBufferView<T>
78+
where
79+
Self: HistBufStorage<T>;
80+
fn as_hist_buf_mut_view(
81+
this: &mut HistoryBufferInner<T, Self>,
82+
) -> &mut HistoryBufferView<T>
83+
where
84+
Self: HistBufStorage<T>;
7585
}
7686

7787
// One sealed layer of indirection to hide the internal details (The MaybeUninit).
@@ -91,6 +101,18 @@ mod storage {
91101
fn borrow_mut(&mut self) -> &mut [MaybeUninit<T>] {
92102
&mut self.buffer
93103
}
104+
fn as_hist_buf_view(this: &HistoryBufferInner<T, Self>) -> &HistoryBufferView<T>
105+
where
106+
Self: HistBufStorage<T>,
107+
{
108+
this
109+
}
110+
fn as_hist_buf_mut_view(this: &mut HistoryBufferInner<T, Self>) -> &mut HistoryBufferView<T>
111+
where
112+
Self: HistBufStorage<T>,
113+
{
114+
this
115+
}
94116
}
95117
impl<T, const N: usize> HistBufStorage<T> for OwnedHistBufStorage<T, N> {}
96118

@@ -101,6 +123,18 @@ mod storage {
101123
fn borrow_mut(&mut self) -> &mut [MaybeUninit<T>] {
102124
&mut self.buffer
103125
}
126+
fn as_hist_buf_view(this: &HistoryBufferInner<T, Self>) -> &HistoryBufferView<T>
127+
where
128+
Self: HistBufStorage<T>,
129+
{
130+
this
131+
}
132+
fn as_hist_buf_mut_view(this: &mut HistoryBufferInner<T, Self>) -> &mut HistoryBufferView<T>
133+
where
134+
Self: HistBufStorage<T>,
135+
{
136+
this
137+
}
104138
}
105139
impl<T> HistBufStorage<T> for ViewHistBufStorage<T> {}
106140
}
@@ -221,18 +255,6 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
221255
filled: false,
222256
}
223257
}
224-
225-
/// Get a reference to the `HistoryBuffer`, erasing the `N` const-generic.
226-
#[inline]
227-
pub const fn as_view(&self) -> &HistoryBufferView<T> {
228-
self
229-
}
230-
231-
/// Get a mutable reference to the `HistoryBuffer`, erasing the `N` const-generic.
232-
#[inline]
233-
pub fn as_mut_view(&mut self) -> &mut HistoryBufferView<T> {
234-
self
235-
}
236258
}
237259

238260
impl<T, const N: usize> HistoryBuffer<T, N>
@@ -264,6 +286,17 @@ where
264286
}
265287
}
266288
impl<T: Copy, S: HistBufStorage<T> + ?Sized> HistoryBufferInner<T, S> {
289+
/// Get a reference to the `HistoryBuffer`, erasing the `N` const-generic.
290+
#[inline]
291+
pub fn as_view(&self) -> &HistoryBufferView<T> {
292+
S::as_hist_buf_view(self)
293+
}
294+
295+
/// Get a mutable reference to the `HistoryBuffer`, erasing the `N` const-generic.
296+
#[inline]
297+
pub fn as_mut_view(&mut self) -> &mut HistoryBufferView<T> {
298+
S::as_hist_buf_mut_view(self)
299+
}
267300
/// Clears the buffer, replacing every element with the given value.
268301
pub fn clear_with(&mut self, t: T) {
269302
// SAFETY: we reset the values just after

src/linear_map.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
55
use core::{borrow::Borrow, fmt, mem, ops, slice};
66

7-
use crate::vec::{OwnedVecStorage, Vec, VecInner, VecStorage, ViewVecStorage};
7+
use crate::vec::{OwnedVecStorage, Vec, VecStorage, ViewVecStorage};
88

9-
/// Base struct for [`LinearMap`] and [`LinearMapView`]
10-
pub struct LinearMapInner<K, V, S: VecStorage<(K, V)> + ?Sized> {
11-
pub(crate) buffer: VecInner<(K, V), S>,
9+
mod sealed {
10+
use crate::vec::{VecInner, VecStorage};
11+
/// Base struct for [`LinearMap`] and [`LinearMapView`]
12+
pub struct LinearMapInnerInner<T, S: VecStorage<T> + ?Sized> {
13+
pub(crate) buffer: VecInner<T, S>,
14+
}
1215
}
16+
pub(crate) use sealed::LinearMapInnerInner;
17+
18+
/// Base struct for [`LinearMap`] and [`LinearMapView`]
19+
pub type LinearMapInner<K, V, S> = LinearMapInnerInner<(K, V), S>;
1320

1421
/// A fixed capacity map/dictionary that performs lookups via linear search.
1522
///
@@ -38,22 +45,22 @@ impl<K, V, const N: usize> LinearMap<K, V, N> {
3845
pub const fn new() -> Self {
3946
Self { buffer: Vec::new() }
4047
}
48+
}
4149

50+
impl<K, V, S: VecStorage<(K, V)> + ?Sized> LinearMapInner<K, V, S>
51+
where
52+
K: Eq,
53+
{
4254
/// Get a reference to the `LinearMap`, erasing the `N` const-generic.
4355
pub fn as_view(&self) -> &LinearMapView<K, V> {
44-
self
56+
S::as_linear_map_view(self)
4557
}
4658

4759
/// Get a mutable reference to the `LinearMap`, erasing the `N` const-generic.
4860
pub fn as_mut_view(&mut self) -> &mut LinearMapView<K, V> {
49-
self
61+
S::as_linear_map_mut_view(self)
5062
}
51-
}
5263

53-
impl<K, V, S: VecStorage<(K, V)> + ?Sized> LinearMapInner<K, V, S>
54-
where
55-
K: Eq,
56-
{
5764
/// Returns the number of elements that the map can hold.
5865
///
5966
/// Computes in *O*(1) time.

src/mpmc.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ impl<T, const N: usize> MpMcQueue<T, N> {
173173
enqueue_pos: AtomicTargetSize::new(0),
174174
}
175175
}
176+
177+
/// Used in `Storage` implementation
178+
pub(crate) fn as_view_private(&self) -> &MpMcQueueView<T> {
179+
self
180+
}
181+
/// Used in `Storage` implementation
182+
pub(crate) fn as_view_mut_private(&mut self) -> &mut MpMcQueueView<T> {
183+
self
184+
}
185+
}
186+
187+
impl<T, S: Storage> MpMcQueueInner<T, S> {
176188
/// Get a reference to the `MpMcQueue`, erasing the `N` const-generic.
177189
///
178190
///
@@ -190,8 +202,8 @@ impl<T, const N: usize> MpMcQueue<T, N> {
190202
/// let view: &MpMcQueueView<u8> = &queue;
191203
/// ```
192204
#[inline]
193-
pub const fn as_view(&self) -> &MpMcQueueView<T> {
194-
self
205+
pub fn as_view(&self) -> &MpMcQueueView<T> {
206+
S::as_mpmc_view(self)
195207
}
196208

197209
/// Get a mutable reference to the `MpMcQueue`, erasing the `N` const-generic.
@@ -211,11 +223,9 @@ impl<T, const N: usize> MpMcQueue<T, N> {
211223
/// ```
212224
#[inline]
213225
pub fn as_mut_view(&mut self) -> &mut MpMcQueueView<T> {
214-
self
226+
S::as_mpmc_mut_view(self)
215227
}
216-
}
217228

218-
impl<T, S: Storage> MpMcQueueInner<T, S> {
219229
fn mask(&self) -> UintSize {
220230
(S::len(self.buffer.get()) - 1) as _
221231
}

src/sorted_linked_list.rs

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use core::ops::{Deref, DerefMut};
3434
use core::ptr;
3535

3636
mod storage {
37-
use super::Node;
37+
use super::{Node, SortedLinkedListIndex, SortedLinkedListInner, SortedLinkedListView};
3838

3939
/// Trait defining how data for a container is stored.
4040
///
@@ -66,6 +66,18 @@ mod storage {
6666
// part of the sealed trait so that no trait is publicly implemented by `OwnedSortedLinkedListStorage` besides `Storage`
6767
fn borrow(&self) -> &[Node<T, Idx>];
6868
fn borrow_mut(&mut self) -> &mut [Node<T, Idx>];
69+
fn as_view<K>(
70+
this: &SortedLinkedListInner<T, Idx, K, Self>,
71+
) -> &SortedLinkedListView<T, Idx, K>
72+
where
73+
Idx: SortedLinkedListIndex,
74+
Self: SortedLinkedListStorage<T, Idx>;
75+
fn as_mut_view<K>(
76+
this: &mut SortedLinkedListInner<T, Idx, K, Self>,
77+
) -> &mut SortedLinkedListView<T, Idx, K>
78+
where
79+
Idx: SortedLinkedListIndex,
80+
Self: SortedLinkedListStorage<T, Idx>;
6981
}
7082

7183
// One sealed layer of indirection to hide the internal details (The MaybeUninit).
@@ -88,6 +100,24 @@ mod storage {
88100
fn borrow_mut(&mut self) -> &mut [Node<T, Idx>] {
89101
&mut self.buffer
90102
}
103+
fn as_view<K>(
104+
this: &SortedLinkedListInner<T, Idx, K, Self>,
105+
) -> &SortedLinkedListView<T, Idx, K>
106+
where
107+
Self: SortedLinkedListStorage<T, Idx>,
108+
Idx: SortedLinkedListIndex,
109+
{
110+
this
111+
}
112+
fn as_mut_view<K>(
113+
this: &mut SortedLinkedListInner<T, Idx, K, Self>,
114+
) -> &mut SortedLinkedListView<T, Idx, K>
115+
where
116+
Self: SortedLinkedListStorage<T, Idx>,
117+
Idx: SortedLinkedListIndex,
118+
{
119+
this
120+
}
91121
}
92122
impl<T, Idx, const N: usize> SortedLinkedListStorage<T, Idx>
93123
for OwnedSortedLinkedListStorage<T, Idx, N>
@@ -101,6 +131,24 @@ mod storage {
101131
fn borrow_mut(&mut self) -> &mut [Node<T, Idx>] {
102132
&mut self.buffer
103133
}
134+
fn as_view<K>(
135+
this: &SortedLinkedListInner<T, Idx, K, Self>,
136+
) -> &SortedLinkedListView<T, Idx, K>
137+
where
138+
Self: SortedLinkedListStorage<T, Idx>,
139+
Idx: SortedLinkedListIndex,
140+
{
141+
this
142+
}
143+
fn as_mut_view<K>(
144+
this: &mut SortedLinkedListInner<T, Idx, K, Self>,
145+
) -> &mut SortedLinkedListView<T, Idx, K>
146+
where
147+
Self: SortedLinkedListStorage<T, Idx>,
148+
Idx: SortedLinkedListIndex,
149+
{
150+
this
151+
}
104152
}
105153
impl<T, Idx> SortedLinkedListStorage<T, Idx> for ViewSortedLinkedListStorage<T, Idx> {}
106154
}
@@ -273,26 +321,21 @@ impl_index_and_const_new!(LinkedIndexU8, u8, new_u8, { u8::MAX as usize - 1 });
273321
impl_index_and_const_new!(LinkedIndexU16, u16, new_u16, { u16::MAX as usize - 1 });
274322
impl_index_and_const_new!(LinkedIndexUsize, usize, new_usize, { usize::MAX - 1 });
275323

276-
impl<T, Idx, K, const N: usize> SortedLinkedList<T, Idx, K, N>
324+
impl<T, Idx, K, S> SortedLinkedListInner<T, Idx, K, S>
277325
where
278326
Idx: SortedLinkedListIndex,
327+
S: SortedLinkedListStorage<T, Idx> + ?Sized,
279328
{
280329
/// Get a reference to the `SortedLinkedList`, erasing the `N` const-generic.
281330
pub fn as_view(&self) -> &SortedLinkedListView<T, Idx, K> {
282-
self
331+
S::as_view(self)
283332
}
284333

285334
/// Get a mutable reference to the `Vec`, erasing the `N` const-generic.
286335
pub fn as_mut_view(&mut self) -> &mut SortedLinkedListView<T, Idx, K> {
287-
self
336+
S::as_mut_view(self)
288337
}
289-
}
290338

291-
impl<T, Idx, K, S> SortedLinkedListInner<T, Idx, K, S>
292-
where
293-
Idx: SortedLinkedListIndex,
294-
S: SortedLinkedListStorage<T, Idx> + ?Sized,
295-
{
296339
/// Internal access helper
297340
#[inline(always)]
298341
fn node_at(&self, index: usize) -> &Node<T, Idx> {

src/spsc.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,28 @@ impl<T, const N: usize> Queue<T, N> {
162162
N - 1
163163
}
164164

165-
/// Get a reference to the `Queue`, erasing the `N` const-generic.
166-
pub fn as_view(&self) -> &QueueView<T> {
165+
/// Used in `Storage` implementation
166+
pub(crate) fn as_view_private(&self) -> &QueueView<T> {
167167
self
168168
}
169169

170-
/// Get a mutable reference to the `Queue`, erasing the `N` const-generic.
171-
pub fn as_mut_view(&mut self) -> &mut QueueView<T> {
170+
/// Used in `Storage` implementation
171+
pub(crate) fn as_mut_view_private(&mut self) -> &mut QueueView<T> {
172172
self
173173
}
174174
}
175175

176176
impl<T, S: Storage> QueueInner<T, S> {
177+
/// Get a reference to the `Queue`, erasing the `N` const-generic.
178+
pub fn as_view(&self) -> &QueueView<T> {
179+
S::as_queue_view(self)
180+
}
181+
182+
/// Get a mutable reference to the `Queue`, erasing the `N` const-generic.
183+
pub fn as_mut_view(&mut self) -> &mut QueueView<T> {
184+
S::as_mut_queue_view(self)
185+
}
186+
177187
#[inline]
178188
fn increment(&self, val: usize) -> usize {
179189
(val + 1) % self.n()

0 commit comments

Comments
 (0)