Skip to content

Commit 9d2477d

Browse files
committed
Introduce private AsView trait
1 parent 2682e1c commit 9d2477d

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

src/sorted_linked_list.rs

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,56 @@ impl_index_and_const_new!(LinkedIndexU8, u8, new_u8, { u8::MAX as usize - 1 });
272272
impl_index_and_const_new!(LinkedIndexU16, u16, new_u16, { u16::MAX as usize - 1 });
273273
impl_index_and_const_new!(LinkedIndexUsize, usize, new_usize, { usize::MAX - 1 });
274274

275+
mod as_view {
276+
use super::{SortedLinkedList, SortedLinkedListIndex, SortedLinkedListView};
277+
278+
pub trait AsView<T, Idx, K>
279+
where
280+
Idx: SortedLinkedListIndex,
281+
{
282+
fn as_view(&self) -> &SortedLinkedListView<T, Idx, K>;
283+
fn as_mut_view(&mut self) -> &mut SortedLinkedListView<T, Idx, K>;
284+
}
285+
286+
impl<T, Idx, K> AsView<T, Idx, K> for SortedLinkedListView<T, Idx, K>
287+
where
288+
Idx: SortedLinkedListIndex,
289+
{
290+
fn as_view(&self) -> &SortedLinkedListView<T, Idx, K> {
291+
self
292+
}
293+
294+
fn as_mut_view(&mut self) -> &mut SortedLinkedListView<T, Idx, K> {
295+
self
296+
}
297+
}
298+
299+
impl<T, Idx, K, const N: usize> AsView<T, Idx, K> for SortedLinkedList<T, Idx, K, N>
300+
where
301+
Idx: SortedLinkedListIndex,
302+
{
303+
fn as_view(&self) -> &SortedLinkedListView<T, Idx, K> {
304+
self
305+
}
306+
307+
fn as_mut_view(&mut self) -> &mut SortedLinkedListView<T, Idx, K> {
308+
self
309+
}
310+
}
311+
}
312+
275313
impl<T, Idx, K, const N: usize> SortedLinkedList<T, Idx, K, N>
276314
where
277315
Idx: SortedLinkedListIndex,
278316
{
279317
/// Get a reference to the `SortedLinkedList`, erasing the `N` const-generic.
280318
pub fn as_view(&self) -> &SortedLinkedListView<T, Idx, K> {
281-
self
319+
as_view::AsView::as_view(self)
282320
}
283321

284322
/// Get a mutable reference to the `Vec`, erasing the `N` const-generic.
285323
pub fn as_mut_view(&mut self) -> &mut SortedLinkedListView<T, Idx, K> {
286-
self
324+
as_view::AsView::as_mut_view(self)
287325
}
288326
}
289327

@@ -537,11 +575,13 @@ where
537575
}
538576
}
539577

540-
impl<T, Idx, K> SortedLinkedListView<T, Idx, K>
578+
impl<T, Idx, K, S> SortedLinkedListInner<T, Idx, K, S>
541579
where
542580
T: Ord,
543581
Idx: SortedLinkedListIndex,
544582
K: Kind,
583+
S: SortedLinkedListStorage<T, Idx> + ?Sized,
584+
Self: as_view::AsView<T, Idx, K>,
545585
{
546586
/// Get an iterator over the sorted list.
547587
///
@@ -554,15 +594,15 @@ where
554594
/// ll.push(1).unwrap();
555595
/// ll.push(2).unwrap();
556596
///
557-
/// let mut iter = ll.as_view().iter();
597+
/// let mut iter = ll.iter();
558598
///
559599
/// assert_eq!(iter.next(), Some(&2));
560600
/// assert_eq!(iter.next(), Some(&1));
561601
/// assert_eq!(iter.next(), None);
562602
/// ```
563603
pub fn iter(&self) -> IterView<'_, T, Idx, K> {
564604
IterView {
565-
list: self,
605+
list: as_view::AsView::as_view(self),
566606
index: self.head,
567607
}
568608
}
@@ -601,7 +641,7 @@ where
601641
is_head: true,
602642
prev_index: Idx::none(),
603643
index: self.head,
604-
list: self,
644+
list: as_view::AsView::as_mut_view(self),
605645
maybe_changed: false,
606646
});
607647
}
@@ -614,7 +654,7 @@ where
614654
is_head: false,
615655
prev_index: unsafe { Idx::new_unchecked(current) },
616656
index: unsafe { Idx::new_unchecked(next) },
617-
list: self,
657+
list: as_view::AsView::as_mut_view(self),
618658
maybe_changed: false,
619659
});
620660
}
@@ -824,11 +864,13 @@ where
824864
// }
825865
// }
826866

827-
impl<T, Idx, K> fmt::Debug for SortedLinkedListView<T, Idx, K>
867+
impl<T, Idx, K, S> fmt::Debug for SortedLinkedListInner<T, Idx, K, S>
828868
where
829869
T: Ord + core::fmt::Debug,
830870
Idx: SortedLinkedListIndex,
831871
K: Kind,
872+
S: ?Sized + SortedLinkedListStorage<T, Idx>,
873+
Self: as_view::AsView<T, Idx, K>,
832874
{
833875
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
834876
f.debug_list().entries(self.iter()).finish()
@@ -942,22 +984,22 @@ mod tests {
942984
ll.push(2).unwrap();
943985
ll.push(3).unwrap();
944986

945-
let mut find = ll.as_mut_view().find_mut(|v| *v == 2).unwrap();
987+
let mut find = ll.find_mut(|v| *v == 2).unwrap();
946988

947989
*find += 1000;
948990
find.finish();
949991

950992
assert_eq!(ll.peek().unwrap(), &1002);
951993

952-
let mut find = ll.as_mut_view().find_mut(|v| *v == 3).unwrap();
994+
let mut find = ll.find_mut(|v| *v == 3).unwrap();
953995

954996
*find += 1000;
955997
find.finish();
956998

957999
assert_eq!(ll.peek().unwrap(), &1003);
9581000

9591001
// Remove largest element
960-
ll.as_mut_view().find_mut(|v| *v == 1003).unwrap().pop();
1002+
ll.find_mut(|v| *v == 1003).unwrap().pop();
9611003

9621004
assert_eq!(ll.peek().unwrap(), &1002);
9631005
}
@@ -977,7 +1019,7 @@ mod tests {
9771019
let mut ll: SortedLinkedList<u32, LinkedIndexUsize, Max, 3> = SortedLinkedList::new_usize();
9781020
ll.push(1).unwrap();
9791021

980-
let mut find = ll.as_mut_view().find_mut(|v| *v == 1).unwrap();
1022+
let mut find = ll.find_mut(|v| *v == 1).unwrap();
9811023

9821024
*find += 1000;
9831025
find.finish();

0 commit comments

Comments
 (0)