Remove sorted_linked_list::Iter and sorted_linked_list::IterInner

This commit is contained in:
Christian Poveda 2025-03-20 14:25:33 -05:00
parent 6ae82d53a5
commit 9924c673e1
No known key found for this signature in database
GPG Key ID: 3B422F347D81A9E8

View File

@ -468,30 +468,6 @@ where
} }
} }
/// Get an iterator over the sorted list.
///
/// # Example
///
/// ```
/// use heapless::sorted_linked_list::{Max, SortedLinkedList};
/// let mut ll: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();
///
/// ll.push(1).unwrap();
/// ll.push(2).unwrap();
///
/// let mut iter = ll.iter();
///
/// assert_eq!(iter.next(), Some(&2));
/// assert_eq!(iter.next(), Some(&1));
/// assert_eq!(iter.next(), None);
/// ```
pub fn iter(&self) -> IterInner<'_, T, Idx, K, S> {
IterInner {
list: self,
index: self.head,
}
}
/// Find an element in the list that can be changed and resorted. /// Find an element in the list that can be changed and resorted.
/// ///
/// # Example /// # Example
@ -663,33 +639,53 @@ where
} }
} }
/// Base struct for [`Iter`] and [`IterView`], generic over the [`SortedLinkedListStorage`]. impl<T, Idx, K> SortedLinkedListView<T, Idx, K>
///
/// In most cases you should use [`Iter`] or [`IterView`] directly. Only use this
/// struct if you want to write code that's generic over both.
pub struct IterInner<'a, T, Idx, K, S>
where where
T: Ord, T: Ord,
Idx: SortedLinkedListIndex, Idx: SortedLinkedListIndex,
K: Kind, K: Kind,
S: SortedLinkedListStorage<T, Idx> + ?Sized,
{ {
list: &'a SortedLinkedListInner<T, Idx, K, S>, /// Get an iterator over the sorted list.
index: Idx, ///
/// # Example
///
/// ```
/// use heapless::sorted_linked_list::{Max, SortedLinkedList};
/// let mut ll: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();
///
/// ll.push(1).unwrap();
/// ll.push(2).unwrap();
///
/// let mut iter = ll.iter();
///
/// assert_eq!(iter.next(), Some(&2));
/// assert_eq!(iter.next(), Some(&1));
/// assert_eq!(iter.next(), None);
/// ```
pub fn iter(&self) -> IterView<'_, T, Idx, K> {
IterView {
list: self,
index: self.head,
}
}
} }
/// Iterator for the linked list. /// Iterator for the linked list.
pub type Iter<'a, T, Idx, K, const N: usize> = pub struct IterView<'a, T, Idx, K>
IterInner<'a, T, Idx, K, OwnedSortedLinkedListStorage<T, Idx, N>>; where
/// Iterator for the linked list. T: Ord,
pub type IterView<'a, T, Idx, K> = IterInner<'a, T, Idx, K, ViewSortedLinkedListStorage<T, Idx>>; Idx: SortedLinkedListIndex,
K: Kind,
impl<'a, T, Idx, K, S> Iterator for IterInner<'a, T, Idx, K, S> {
list: &'a SortedLinkedListInner<T, Idx, K, ViewSortedLinkedListStorage<T, Idx>>,
index: Idx,
}
impl<'a, T, Idx, K> Iterator for IterView<'a, T, Idx, K>
where where
T: Ord, T: Ord,
Idx: SortedLinkedListIndex, Idx: SortedLinkedListIndex,
K: Kind, K: Kind,
S: SortedLinkedListStorage<T, Idx> + ?Sized,
{ {
type Item = &'a T; type Item = &'a T;
@ -887,12 +883,11 @@ where
// } // }
// } // }
impl<T, Idx, K, S> fmt::Debug for SortedLinkedListInner<T, Idx, K, S> impl<T, Idx, K> fmt::Debug for SortedLinkedListView<T, Idx, K>
where where
T: Ord + core::fmt::Debug, T: Ord + core::fmt::Debug,
Idx: SortedLinkedListIndex, Idx: SortedLinkedListIndex,
K: Kind, K: Kind,
S: SortedLinkedListStorage<T, Idx> + ?Sized,
{ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish() f.debug_list().entries(self.iter()).finish()