From 9924c673e19657cc0dd8cc904491bc999ea6b919 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Thu, 20 Mar 2025 14:25:33 -0500 Subject: [PATCH 1/4] Remove `sorted_linked_list::Iter` and `sorted_linked_list::IterInner` --- src/sorted_linked_list.rs | 75 ++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/src/sorted_linked_list.rs b/src/sorted_linked_list.rs index 60d2d3258b..55c80415b6 100644 --- a/src/sorted_linked_list.rs +++ b/src/sorted_linked_list.rs @@ -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. /// /// # Example @@ -663,33 +639,53 @@ where } } -/// Base struct for [`Iter`] and [`IterView`], generic over the [`SortedLinkedListStorage`]. -/// -/// 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> +impl SortedLinkedListView where T: Ord, Idx: SortedLinkedListIndex, K: Kind, - S: SortedLinkedListStorage + ?Sized, { - list: &'a SortedLinkedListInner, - index: Idx, + /// 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) -> IterView<'_, T, Idx, K> { + IterView { + list: self, + index: self.head, + } + } } /// Iterator for the linked list. -pub type Iter<'a, T, Idx, K, const N: usize> = - IterInner<'a, T, Idx, K, OwnedSortedLinkedListStorage>; -/// Iterator for the linked list. -pub type IterView<'a, T, Idx, K> = IterInner<'a, T, Idx, K, ViewSortedLinkedListStorage>; +pub struct IterView<'a, T, Idx, K> +where + T: Ord, + Idx: SortedLinkedListIndex, + K: Kind, +{ + list: &'a SortedLinkedListInner>, + index: Idx, +} -impl<'a, T, Idx, K, S> Iterator for IterInner<'a, T, Idx, K, S> +impl<'a, T, Idx, K> Iterator for IterView<'a, T, Idx, K> where T: Ord, Idx: SortedLinkedListIndex, K: Kind, - S: SortedLinkedListStorage + ?Sized, { type Item = &'a T; @@ -887,12 +883,11 @@ where // } // } -impl fmt::Debug for SortedLinkedListInner +impl fmt::Debug for SortedLinkedListView where T: Ord + core::fmt::Debug, Idx: SortedLinkedListIndex, K: Kind, - S: SortedLinkedListStorage + ?Sized, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.iter()).finish() From e395892318ab00c88b5959ebee9eaa96acf7cb24 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Thu, 20 Mar 2025 14:30:49 -0500 Subject: [PATCH 2/4] Remove `sorted_linked_list::FindMut` and `sorted_linked_list::FindMutInner` --- src/sorted_linked_list.rs | 145 +++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 80 deletions(-) diff --git a/src/sorted_linked_list.rs b/src/sorted_linked_list.rs index 55c80415b6..bb01ec6469 100644 --- a/src/sorted_linked_list.rs +++ b/src/sorted_linked_list.rs @@ -468,64 +468,6 @@ where } } - /// Find an element in the list that can be changed and resorted. - /// - /// # 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(); - /// ll.push(3).unwrap(); - /// - /// // Find a value and update it - /// let mut find = ll.find_mut(|v| *v == 2).unwrap(); - /// *find += 1000; - /// find.finish(); - /// - /// assert_eq!(ll.pop(), Some(1002)); - /// assert_eq!(ll.pop(), Some(3)); - /// assert_eq!(ll.pop(), Some(1)); - /// assert_eq!(ll.pop(), None); - /// ``` - pub fn find_mut(&mut self, mut f: F) -> Option> - where - F: FnMut(&T) -> bool, - { - let head = self.head.option()?; - - // Special-case, first element - if f(self.read_data_in_node_at(head)) { - return Some(FindMutInner { - is_head: true, - prev_index: Idx::none(), - index: self.head, - list: self, - maybe_changed: false, - }); - } - - let mut current = head; - - while let Some(next) = self.node_at(current).next.option() { - if f(self.read_data_in_node_at(next)) { - return Some(FindMutInner { - is_head: false, - prev_index: unsafe { Idx::new_unchecked(current) }, - index: unsafe { Idx::new_unchecked(next) }, - list: self, - maybe_changed: false, - }); - } - - current = next; - } - - None - } - /// Peek at the first element. /// /// # Example @@ -668,6 +610,64 @@ where index: self.head, } } + + /// Find an element in the list that can be changed and resorted. + /// + /// # 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(); + /// ll.push(3).unwrap(); + /// + /// // Find a value and update it + /// let mut find = ll.find_mut(|v| *v == 2).unwrap(); + /// *find += 1000; + /// find.finish(); + /// + /// assert_eq!(ll.pop(), Some(1002)); + /// assert_eq!(ll.pop(), Some(3)); + /// assert_eq!(ll.pop(), Some(1)); + /// assert_eq!(ll.pop(), None); + /// ``` + pub fn find_mut(&mut self, mut f: F) -> Option> + where + F: FnMut(&T) -> bool, + { + let head = self.head.option()?; + + // Special-case, first element + if f(self.read_data_in_node_at(head)) { + return Some(FindMutView { + is_head: true, + prev_index: Idx::none(), + index: self.head, + list: self, + maybe_changed: false, + }); + } + + let mut current = head; + + while let Some(next) = self.node_at(current).next.option() { + if f(self.read_data_in_node_at(next)) { + return Some(FindMutView { + is_head: false, + prev_index: unsafe { Idx::new_unchecked(current) }, + index: unsafe { Idx::new_unchecked(next) }, + list: self, + maybe_changed: false, + }); + } + + current = next; + } + + None + } } /// Iterator for the linked list. @@ -699,37 +699,25 @@ where } } -/// Base struct for [`FindMut`] and [`FindMutView`], generic over the [`SortedLinkedListStorage`]. -/// -/// In most cases you should use [`FindMut`] or [`FindMutView`] directly. Only use this -/// struct if you want to write code that's generic over both. -pub struct FindMutInner<'a, T, Idx, K, S> +/// Comes from [`SortedLinkedList::find_mut`]. +pub struct FindMutView<'a, T, Idx, K> where T: Ord, Idx: SortedLinkedListIndex, K: Kind, - S: SortedLinkedListStorage + ?Sized, { - list: &'a mut SortedLinkedListInner, + list: &'a mut SortedLinkedListView, is_head: bool, prev_index: Idx, index: Idx, maybe_changed: bool, } -/// Comes from [`SortedLinkedList::find_mut`]. -pub type FindMut<'a, T, Idx, K, const N: usize> = - FindMutInner<'a, T, Idx, K, OwnedSortedLinkedListStorage>; -/// Comes from [`SortedLinkedList::find_mut`]. -pub type FindMutView<'a, T, Idx, K, const N: usize> = - FindMutInner<'a, T, Idx, K, ViewSortedLinkedListStorage>; - -impl FindMutInner<'_, T, Idx, K, S> +impl FindMutView<'_, T, Idx, K> where T: Ord, Idx: SortedLinkedListIndex, K: Kind, - S: SortedLinkedListStorage + ?Sized, { fn pop_internal(&mut self) -> T { if self.is_head { @@ -813,12 +801,11 @@ where } } -impl Drop for FindMutInner<'_, T, Idx, K, S> +impl Drop for FindMutView<'_, T, Idx, K> where T: Ord, Idx: SortedLinkedListIndex, K: Kind, - S: SortedLinkedListStorage + ?Sized, { fn drop(&mut self) { // Only resort the list if the element has changed @@ -829,12 +816,11 @@ where } } -impl Deref for FindMutInner<'_, T, Idx, K, S> +impl Deref for FindMutView<'_, T, Idx, K> where T: Ord, Idx: SortedLinkedListIndex, K: Kind, - S: SortedLinkedListStorage + ?Sized, { type Target = T; @@ -844,12 +830,11 @@ where } } -impl DerefMut for FindMutInner<'_, T, Idx, K, S> +impl DerefMut for FindMutView<'_, T, Idx, K> where T: Ord, Idx: SortedLinkedListIndex, K: Kind, - S: SortedLinkedListStorage + ?Sized, { fn deref_mut(&mut self) -> &mut Self::Target { self.maybe_changed = true; From 85f1aeae6801e4ab68064c8f1943d972c33c293e Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Thu, 10 Apr 2025 10:40:36 -0500 Subject: [PATCH 3/4] Make implementations generic over the storage --- src/sorted_linked_list.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/sorted_linked_list.rs b/src/sorted_linked_list.rs index bb01ec6469..093c52bbe8 100644 --- a/src/sorted_linked_list.rs +++ b/src/sorted_linked_list.rs @@ -581,11 +581,12 @@ where } } -impl SortedLinkedListView +impl SortedLinkedListInner where T: Ord, Idx: SortedLinkedListIndex, K: Kind, + S: SortedLinkedListStorage + ?Sized, { /// Get an iterator over the sorted list. /// @@ -606,7 +607,7 @@ where /// ``` pub fn iter(&self) -> IterView<'_, T, Idx, K> { IterView { - list: self, + list: S::as_view(self), index: self.head, } } @@ -645,7 +646,7 @@ where is_head: true, prev_index: Idx::none(), index: self.head, - list: self, + list: S::as_mut_view(self), maybe_changed: false, }); } @@ -658,7 +659,7 @@ where is_head: false, prev_index: unsafe { Idx::new_unchecked(current) }, index: unsafe { Idx::new_unchecked(next) }, - list: self, + list: S::as_mut_view(self), maybe_changed: false, }); } @@ -868,11 +869,12 @@ where // } // } -impl fmt::Debug for SortedLinkedListView +impl fmt::Debug for SortedLinkedListInner where T: Ord + core::fmt::Debug, Idx: SortedLinkedListIndex, K: Kind, + S: ?Sized + SortedLinkedListStorage, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.iter()).finish() From 0e8f01103e1d39d2068d7e5579dfc869f94105e8 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Thu, 20 Mar 2025 14:34:24 -0500 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e4ad61ada..645d403c42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Removed - `Vec::storage_capacity` has been removed and `Vec::capacity` must be used instead. +- Removed `sorted_linked_list::Iter` and `sorted_linked_list::IterInner`. +- Removed `sorted_linked_list::FindMut` and `sorted_linked_list::FindMutInner`. ## [v0.8.0] - 2023-11-07