Skip to content

Commit da404c1

Browse files
authored
Merge pull request #540 from pvdrz/keep-views-only
`SortedLinkedList`: Keep only the view versions of `Iter` and `FindMut`
2 parents 6ae82d5 + 0e8f011 commit da404c1

File tree

2 files changed

+102
-118
lines changed

2 files changed

+102
-118
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
7979
### Removed
8080

8181
- `Vec::storage_capacity` has been removed and `Vec::capacity` must be used instead.
82+
- Removed `sorted_linked_list::Iter` and `sorted_linked_list::IterInner`.
83+
- Removed `sorted_linked_list::FindMut` and `sorted_linked_list::FindMutInner`.
8284

8385
## [v0.8.0] - 2023-11-07
8486

src/sorted_linked_list.rs

Lines changed: 100 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -468,88 +468,6 @@ where
468468
}
469469
}
470470

471-
/// Get an iterator over the sorted list.
472-
///
473-
/// # Example
474-
///
475-
/// ```
476-
/// use heapless::sorted_linked_list::{Max, SortedLinkedList};
477-
/// let mut ll: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();
478-
///
479-
/// ll.push(1).unwrap();
480-
/// ll.push(2).unwrap();
481-
///
482-
/// let mut iter = ll.iter();
483-
///
484-
/// assert_eq!(iter.next(), Some(&2));
485-
/// assert_eq!(iter.next(), Some(&1));
486-
/// assert_eq!(iter.next(), None);
487-
/// ```
488-
pub fn iter(&self) -> IterInner<'_, T, Idx, K, S> {
489-
IterInner {
490-
list: self,
491-
index: self.head,
492-
}
493-
}
494-
495-
/// Find an element in the list that can be changed and resorted.
496-
///
497-
/// # Example
498-
///
499-
/// ```
500-
/// use heapless::sorted_linked_list::{Max, SortedLinkedList};
501-
/// let mut ll: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();
502-
///
503-
/// ll.push(1).unwrap();
504-
/// ll.push(2).unwrap();
505-
/// ll.push(3).unwrap();
506-
///
507-
/// // Find a value and update it
508-
/// let mut find = ll.find_mut(|v| *v == 2).unwrap();
509-
/// *find += 1000;
510-
/// find.finish();
511-
///
512-
/// assert_eq!(ll.pop(), Some(1002));
513-
/// assert_eq!(ll.pop(), Some(3));
514-
/// assert_eq!(ll.pop(), Some(1));
515-
/// assert_eq!(ll.pop(), None);
516-
/// ```
517-
pub fn find_mut<F>(&mut self, mut f: F) -> Option<FindMutInner<'_, T, Idx, K, S>>
518-
where
519-
F: FnMut(&T) -> bool,
520-
{
521-
let head = self.head.option()?;
522-
523-
// Special-case, first element
524-
if f(self.read_data_in_node_at(head)) {
525-
return Some(FindMutInner {
526-
is_head: true,
527-
prev_index: Idx::none(),
528-
index: self.head,
529-
list: self,
530-
maybe_changed: false,
531-
});
532-
}
533-
534-
let mut current = head;
535-
536-
while let Some(next) = self.node_at(current).next.option() {
537-
if f(self.read_data_in_node_at(next)) {
538-
return Some(FindMutInner {
539-
is_head: false,
540-
prev_index: unsafe { Idx::new_unchecked(current) },
541-
index: unsafe { Idx::new_unchecked(next) },
542-
list: self,
543-
maybe_changed: false,
544-
});
545-
}
546-
547-
current = next;
548-
}
549-
550-
None
551-
}
552-
553471
/// Peek at the first element.
554472
///
555473
/// # Example
@@ -663,33 +581,112 @@ where
663581
}
664582
}
665583

666-
/// Base struct for [`Iter`] and [`IterView`], generic over the [`SortedLinkedListStorage`].
667-
///
668-
/// In most cases you should use [`Iter`] or [`IterView`] directly. Only use this
669-
/// struct if you want to write code that's generic over both.
670-
pub struct IterInner<'a, T, Idx, K, S>
584+
impl<T, Idx, K, S> SortedLinkedListInner<T, Idx, K, S>
671585
where
672586
T: Ord,
673587
Idx: SortedLinkedListIndex,
674588
K: Kind,
675589
S: SortedLinkedListStorage<T, Idx> + ?Sized,
676590
{
677-
list: &'a SortedLinkedListInner<T, Idx, K, S>,
678-
index: Idx,
591+
/// Get an iterator over the sorted list.
592+
///
593+
/// # Example
594+
///
595+
/// ```
596+
/// use heapless::sorted_linked_list::{Max, SortedLinkedList};
597+
/// let mut ll: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();
598+
///
599+
/// ll.push(1).unwrap();
600+
/// ll.push(2).unwrap();
601+
///
602+
/// let mut iter = ll.iter();
603+
///
604+
/// assert_eq!(iter.next(), Some(&2));
605+
/// assert_eq!(iter.next(), Some(&1));
606+
/// assert_eq!(iter.next(), None);
607+
/// ```
608+
pub fn iter(&self) -> IterView<'_, T, Idx, K> {
609+
IterView {
610+
list: S::as_view(self),
611+
index: self.head,
612+
}
613+
}
614+
615+
/// Find an element in the list that can be changed and resorted.
616+
///
617+
/// # Example
618+
///
619+
/// ```
620+
/// use heapless::sorted_linked_list::{Max, SortedLinkedList};
621+
/// let mut ll: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();
622+
///
623+
/// ll.push(1).unwrap();
624+
/// ll.push(2).unwrap();
625+
/// ll.push(3).unwrap();
626+
///
627+
/// // Find a value and update it
628+
/// let mut find = ll.find_mut(|v| *v == 2).unwrap();
629+
/// *find += 1000;
630+
/// find.finish();
631+
///
632+
/// assert_eq!(ll.pop(), Some(1002));
633+
/// assert_eq!(ll.pop(), Some(3));
634+
/// assert_eq!(ll.pop(), Some(1));
635+
/// assert_eq!(ll.pop(), None);
636+
/// ```
637+
pub fn find_mut<F>(&mut self, mut f: F) -> Option<FindMutView<'_, T, Idx, K>>
638+
where
639+
F: FnMut(&T) -> bool,
640+
{
641+
let head = self.head.option()?;
642+
643+
// Special-case, first element
644+
if f(self.read_data_in_node_at(head)) {
645+
return Some(FindMutView {
646+
is_head: true,
647+
prev_index: Idx::none(),
648+
index: self.head,
649+
list: S::as_mut_view(self),
650+
maybe_changed: false,
651+
});
652+
}
653+
654+
let mut current = head;
655+
656+
while let Some(next) = self.node_at(current).next.option() {
657+
if f(self.read_data_in_node_at(next)) {
658+
return Some(FindMutView {
659+
is_head: false,
660+
prev_index: unsafe { Idx::new_unchecked(current) },
661+
index: unsafe { Idx::new_unchecked(next) },
662+
list: S::as_mut_view(self),
663+
maybe_changed: false,
664+
});
665+
}
666+
667+
current = next;
668+
}
669+
670+
None
671+
}
679672
}
680673

681674
/// Iterator for the linked list.
682-
pub type Iter<'a, T, Idx, K, const N: usize> =
683-
IterInner<'a, T, Idx, K, OwnedSortedLinkedListStorage<T, Idx, N>>;
684-
/// Iterator for the linked list.
685-
pub type IterView<'a, T, Idx, K> = IterInner<'a, T, Idx, K, ViewSortedLinkedListStorage<T, Idx>>;
675+
pub struct IterView<'a, T, Idx, K>
676+
where
677+
T: Ord,
678+
Idx: SortedLinkedListIndex,
679+
K: Kind,
680+
{
681+
list: &'a SortedLinkedListInner<T, Idx, K, ViewSortedLinkedListStorage<T, Idx>>,
682+
index: Idx,
683+
}
686684

687-
impl<'a, T, Idx, K, S> Iterator for IterInner<'a, T, Idx, K, S>
685+
impl<'a, T, Idx, K> Iterator for IterView<'a, T, Idx, K>
688686
where
689687
T: Ord,
690688
Idx: SortedLinkedListIndex,
691689
K: Kind,
692-
S: SortedLinkedListStorage<T, Idx> + ?Sized,
693690
{
694691
type Item = &'a T;
695692

@@ -703,37 +700,25 @@ where
703700
}
704701
}
705702

706-
/// Base struct for [`FindMut`] and [`FindMutView`], generic over the [`SortedLinkedListStorage`].
707-
///
708-
/// In most cases you should use [`FindMut`] or [`FindMutView`] directly. Only use this
709-
/// struct if you want to write code that's generic over both.
710-
pub struct FindMutInner<'a, T, Idx, K, S>
703+
/// Comes from [`SortedLinkedList::find_mut`].
704+
pub struct FindMutView<'a, T, Idx, K>
711705
where
712706
T: Ord,
713707
Idx: SortedLinkedListIndex,
714708
K: Kind,
715-
S: SortedLinkedListStorage<T, Idx> + ?Sized,
716709
{
717-
list: &'a mut SortedLinkedListInner<T, Idx, K, S>,
710+
list: &'a mut SortedLinkedListView<T, Idx, K>,
718711
is_head: bool,
719712
prev_index: Idx,
720713
index: Idx,
721714
maybe_changed: bool,
722715
}
723716

724-
/// Comes from [`SortedLinkedList::find_mut`].
725-
pub type FindMut<'a, T, Idx, K, const N: usize> =
726-
FindMutInner<'a, T, Idx, K, OwnedSortedLinkedListStorage<T, Idx, N>>;
727-
/// Comes from [`SortedLinkedList::find_mut`].
728-
pub type FindMutView<'a, T, Idx, K, const N: usize> =
729-
FindMutInner<'a, T, Idx, K, ViewSortedLinkedListStorage<T, Idx>>;
730-
731-
impl<T, Idx, K, S> FindMutInner<'_, T, Idx, K, S>
717+
impl<T, Idx, K> FindMutView<'_, T, Idx, K>
732718
where
733719
T: Ord,
734720
Idx: SortedLinkedListIndex,
735721
K: Kind,
736-
S: SortedLinkedListStorage<T, Idx> + ?Sized,
737722
{
738723
fn pop_internal(&mut self) -> T {
739724
if self.is_head {
@@ -817,12 +802,11 @@ where
817802
}
818803
}
819804

820-
impl<T, Idx, K, S> Drop for FindMutInner<'_, T, Idx, K, S>
805+
impl<T, Idx, K> Drop for FindMutView<'_, T, Idx, K>
821806
where
822807
T: Ord,
823808
Idx: SortedLinkedListIndex,
824809
K: Kind,
825-
S: SortedLinkedListStorage<T, Idx> + ?Sized,
826810
{
827811
fn drop(&mut self) {
828812
// Only resort the list if the element has changed
@@ -833,12 +817,11 @@ where
833817
}
834818
}
835819

836-
impl<T, Idx, K, S> Deref for FindMutInner<'_, T, Idx, K, S>
820+
impl<T, Idx, K> Deref for FindMutView<'_, T, Idx, K>
837821
where
838822
T: Ord,
839823
Idx: SortedLinkedListIndex,
840824
K: Kind,
841-
S: SortedLinkedListStorage<T, Idx> + ?Sized,
842825
{
843826
type Target = T;
844827

@@ -848,12 +831,11 @@ where
848831
}
849832
}
850833

851-
impl<T, Idx, K, S> DerefMut for FindMutInner<'_, T, Idx, K, S>
834+
impl<T, Idx, K> DerefMut for FindMutView<'_, T, Idx, K>
852835
where
853836
T: Ord,
854837
Idx: SortedLinkedListIndex,
855838
K: Kind,
856-
S: SortedLinkedListStorage<T, Idx> + ?Sized,
857839
{
858840
fn deref_mut(&mut self) -> &mut Self::Target {
859841
self.maybe_changed = true;
@@ -892,7 +874,7 @@ where
892874
T: Ord + core::fmt::Debug,
893875
Idx: SortedLinkedListIndex,
894876
K: Kind,
895-
S: SortedLinkedListStorage<T, Idx> + ?Sized,
877+
S: ?Sized + SortedLinkedListStorage<T, Idx>,
896878
{
897879
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
898880
f.debug_list().entries(self.iter()).finish()

0 commit comments

Comments
 (0)