Skip to content

Commit e395892

Browse files
committed
Remove sorted_linked_list::FindMut and sorted_linked_list::FindMutInner
1 parent 9924c67 commit e395892

File tree

1 file changed

+65
-80
lines changed

1 file changed

+65
-80
lines changed

src/sorted_linked_list.rs

Lines changed: 65 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -468,64 +468,6 @@ where
468468
}
469469
}
470470

471-
/// Find an element in the list that can be changed and resorted.
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-
/// ll.push(3).unwrap();
482-
///
483-
/// // Find a value and update it
484-
/// let mut find = ll.find_mut(|v| *v == 2).unwrap();
485-
/// *find += 1000;
486-
/// find.finish();
487-
///
488-
/// assert_eq!(ll.pop(), Some(1002));
489-
/// assert_eq!(ll.pop(), Some(3));
490-
/// assert_eq!(ll.pop(), Some(1));
491-
/// assert_eq!(ll.pop(), None);
492-
/// ```
493-
pub fn find_mut<F>(&mut self, mut f: F) -> Option<FindMutInner<'_, T, Idx, K, S>>
494-
where
495-
F: FnMut(&T) -> bool,
496-
{
497-
let head = self.head.option()?;
498-
499-
// Special-case, first element
500-
if f(self.read_data_in_node_at(head)) {
501-
return Some(FindMutInner {
502-
is_head: true,
503-
prev_index: Idx::none(),
504-
index: self.head,
505-
list: self,
506-
maybe_changed: false,
507-
});
508-
}
509-
510-
let mut current = head;
511-
512-
while let Some(next) = self.node_at(current).next.option() {
513-
if f(self.read_data_in_node_at(next)) {
514-
return Some(FindMutInner {
515-
is_head: false,
516-
prev_index: unsafe { Idx::new_unchecked(current) },
517-
index: unsafe { Idx::new_unchecked(next) },
518-
list: self,
519-
maybe_changed: false,
520-
});
521-
}
522-
523-
current = next;
524-
}
525-
526-
None
527-
}
528-
529471
/// Peek at the first element.
530472
///
531473
/// # Example
@@ -668,6 +610,64 @@ where
668610
index: self.head,
669611
}
670612
}
613+
614+
/// Find an element in the list that can be changed and resorted.
615+
///
616+
/// # Example
617+
///
618+
/// ```
619+
/// use heapless::sorted_linked_list::{Max, SortedLinkedList};
620+
/// let mut ll: SortedLinkedList<_, _, Max, 3> = SortedLinkedList::new_usize();
621+
///
622+
/// ll.push(1).unwrap();
623+
/// ll.push(2).unwrap();
624+
/// ll.push(3).unwrap();
625+
///
626+
/// // Find a value and update it
627+
/// let mut find = ll.find_mut(|v| *v == 2).unwrap();
628+
/// *find += 1000;
629+
/// find.finish();
630+
///
631+
/// assert_eq!(ll.pop(), Some(1002));
632+
/// assert_eq!(ll.pop(), Some(3));
633+
/// assert_eq!(ll.pop(), Some(1));
634+
/// assert_eq!(ll.pop(), None);
635+
/// ```
636+
pub fn find_mut<F>(&mut self, mut f: F) -> Option<FindMutView<'_, T, Idx, K>>
637+
where
638+
F: FnMut(&T) -> bool,
639+
{
640+
let head = self.head.option()?;
641+
642+
// Special-case, first element
643+
if f(self.read_data_in_node_at(head)) {
644+
return Some(FindMutView {
645+
is_head: true,
646+
prev_index: Idx::none(),
647+
index: self.head,
648+
list: self,
649+
maybe_changed: false,
650+
});
651+
}
652+
653+
let mut current = head;
654+
655+
while let Some(next) = self.node_at(current).next.option() {
656+
if f(self.read_data_in_node_at(next)) {
657+
return Some(FindMutView {
658+
is_head: false,
659+
prev_index: unsafe { Idx::new_unchecked(current) },
660+
index: unsafe { Idx::new_unchecked(next) },
661+
list: self,
662+
maybe_changed: false,
663+
});
664+
}
665+
666+
current = next;
667+
}
668+
669+
None
670+
}
671671
}
672672

673673
/// Iterator for the linked list.
@@ -699,37 +699,25 @@ where
699699
}
700700
}
701701

702-
/// Base struct for [`FindMut`] and [`FindMutView`], generic over the [`SortedLinkedListStorage`].
703-
///
704-
/// In most cases you should use [`FindMut`] or [`FindMutView`] directly. Only use this
705-
/// struct if you want to write code that's generic over both.
706-
pub struct FindMutInner<'a, T, Idx, K, S>
702+
/// Comes from [`SortedLinkedList::find_mut`].
703+
pub struct FindMutView<'a, T, Idx, K>
707704
where
708705
T: Ord,
709706
Idx: SortedLinkedListIndex,
710707
K: Kind,
711-
S: SortedLinkedListStorage<T, Idx> + ?Sized,
712708
{
713-
list: &'a mut SortedLinkedListInner<T, Idx, K, S>,
709+
list: &'a mut SortedLinkedListView<T, Idx, K>,
714710
is_head: bool,
715711
prev_index: Idx,
716712
index: Idx,
717713
maybe_changed: bool,
718714
}
719715

720-
/// Comes from [`SortedLinkedList::find_mut`].
721-
pub type FindMut<'a, T, Idx, K, const N: usize> =
722-
FindMutInner<'a, T, Idx, K, OwnedSortedLinkedListStorage<T, Idx, N>>;
723-
/// Comes from [`SortedLinkedList::find_mut`].
724-
pub type FindMutView<'a, T, Idx, K, const N: usize> =
725-
FindMutInner<'a, T, Idx, K, ViewSortedLinkedListStorage<T, Idx>>;
726-
727-
impl<T, Idx, K, S> FindMutInner<'_, T, Idx, K, S>
716+
impl<T, Idx, K> FindMutView<'_, T, Idx, K>
728717
where
729718
T: Ord,
730719
Idx: SortedLinkedListIndex,
731720
K: Kind,
732-
S: SortedLinkedListStorage<T, Idx> + ?Sized,
733721
{
734722
fn pop_internal(&mut self) -> T {
735723
if self.is_head {
@@ -813,12 +801,11 @@ where
813801
}
814802
}
815803

816-
impl<T, Idx, K, S> Drop for FindMutInner<'_, T, Idx, K, S>
804+
impl<T, Idx, K> Drop for FindMutView<'_, T, Idx, K>
817805
where
818806
T: Ord,
819807
Idx: SortedLinkedListIndex,
820808
K: Kind,
821-
S: SortedLinkedListStorage<T, Idx> + ?Sized,
822809
{
823810
fn drop(&mut self) {
824811
// Only resort the list if the element has changed
@@ -829,12 +816,11 @@ where
829816
}
830817
}
831818

832-
impl<T, Idx, K, S> Deref for FindMutInner<'_, T, Idx, K, S>
819+
impl<T, Idx, K> Deref for FindMutView<'_, T, Idx, K>
833820
where
834821
T: Ord,
835822
Idx: SortedLinkedListIndex,
836823
K: Kind,
837-
S: SortedLinkedListStorage<T, Idx> + ?Sized,
838824
{
839825
type Target = T;
840826

@@ -844,12 +830,11 @@ where
844830
}
845831
}
846832

847-
impl<T, Idx, K, S> DerefMut for FindMutInner<'_, T, Idx, K, S>
833+
impl<T, Idx, K> DerefMut for FindMutView<'_, T, Idx, K>
848834
where
849835
T: Ord,
850836
Idx: SortedLinkedListIndex,
851837
K: Kind,
852-
S: SortedLinkedListStorage<T, Idx> + ?Sized,
853838
{
854839
fn deref_mut(&mut self) -> &mut Self::Target {
855840
self.maybe_changed = true;

0 commit comments

Comments
 (0)