@@ -468,64 +468,6 @@ where
468
468
}
469
469
}
470
470
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
-
529
471
/// Peek at the first element.
530
472
///
531
473
/// # Example
@@ -668,6 +610,64 @@ where
668
610
index : self . head ,
669
611
}
670
612
}
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
+ }
671
671
}
672
672
673
673
/// Iterator for the linked list.
@@ -699,37 +699,25 @@ where
699
699
}
700
700
}
701
701
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 >
707
704
where
708
705
T : Ord ,
709
706
Idx : SortedLinkedListIndex ,
710
707
K : Kind ,
711
- S : SortedLinkedListStorage < T , Idx > + ?Sized ,
712
708
{
713
- list : & ' a mut SortedLinkedListInner < T , Idx , K , S > ,
709
+ list : & ' a mut SortedLinkedListView < T , Idx , K > ,
714
710
is_head : bool ,
715
711
prev_index : Idx ,
716
712
index : Idx ,
717
713
maybe_changed : bool ,
718
714
}
719
715
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 >
728
717
where
729
718
T : Ord ,
730
719
Idx : SortedLinkedListIndex ,
731
720
K : Kind ,
732
- S : SortedLinkedListStorage < T , Idx > + ?Sized ,
733
721
{
734
722
fn pop_internal ( & mut self ) -> T {
735
723
if self . is_head {
@@ -813,12 +801,11 @@ where
813
801
}
814
802
}
815
803
816
- impl < T , Idx , K , S > Drop for FindMutInner < ' _ , T , Idx , K , S >
804
+ impl < T , Idx , K > Drop for FindMutView < ' _ , T , Idx , K >
817
805
where
818
806
T : Ord ,
819
807
Idx : SortedLinkedListIndex ,
820
808
K : Kind ,
821
- S : SortedLinkedListStorage < T , Idx > + ?Sized ,
822
809
{
823
810
fn drop ( & mut self ) {
824
811
// Only resort the list if the element has changed
@@ -829,12 +816,11 @@ where
829
816
}
830
817
}
831
818
832
- impl < T , Idx , K , S > Deref for FindMutInner < ' _ , T , Idx , K , S >
819
+ impl < T , Idx , K > Deref for FindMutView < ' _ , T , Idx , K >
833
820
where
834
821
T : Ord ,
835
822
Idx : SortedLinkedListIndex ,
836
823
K : Kind ,
837
- S : SortedLinkedListStorage < T , Idx > + ?Sized ,
838
824
{
839
825
type Target = T ;
840
826
@@ -844,12 +830,11 @@ where
844
830
}
845
831
}
846
832
847
- impl < T , Idx , K , S > DerefMut for FindMutInner < ' _ , T , Idx , K , S >
833
+ impl < T , Idx , K > DerefMut for FindMutView < ' _ , T , Idx , K >
848
834
where
849
835
T : Ord ,
850
836
Idx : SortedLinkedListIndex ,
851
837
K : Kind ,
852
- S : SortedLinkedListStorage < T , Idx > + ?Sized ,
853
838
{
854
839
fn deref_mut ( & mut self ) -> & mut Self :: Target {
855
840
self . maybe_changed = true ;
0 commit comments