@@ -468,88 +468,6 @@ where
468
468
}
469
469
}
470
470
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
-
553
471
/// Peek at the first element.
554
472
///
555
473
/// # Example
@@ -663,33 +581,112 @@ where
663
581
}
664
582
}
665
583
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 >
671
585
where
672
586
T : Ord ,
673
587
Idx : SortedLinkedListIndex ,
674
588
K : Kind ,
675
589
S : SortedLinkedListStorage < T , Idx > + ?Sized ,
676
590
{
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
+ }
679
672
}
680
673
681
674
/// 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
+ }
686
684
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 >
688
686
where
689
687
T : Ord ,
690
688
Idx : SortedLinkedListIndex ,
691
689
K : Kind ,
692
- S : SortedLinkedListStorage < T , Idx > + ?Sized ,
693
690
{
694
691
type Item = & ' a T ;
695
692
@@ -703,37 +700,25 @@ where
703
700
}
704
701
}
705
702
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 >
711
705
where
712
706
T : Ord ,
713
707
Idx : SortedLinkedListIndex ,
714
708
K : Kind ,
715
- S : SortedLinkedListStorage < T , Idx > + ?Sized ,
716
709
{
717
- list : & ' a mut SortedLinkedListInner < T , Idx , K , S > ,
710
+ list : & ' a mut SortedLinkedListView < T , Idx , K > ,
718
711
is_head : bool ,
719
712
prev_index : Idx ,
720
713
index : Idx ,
721
714
maybe_changed : bool ,
722
715
}
723
716
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 >
732
718
where
733
719
T : Ord ,
734
720
Idx : SortedLinkedListIndex ,
735
721
K : Kind ,
736
- S : SortedLinkedListStorage < T , Idx > + ?Sized ,
737
722
{
738
723
fn pop_internal ( & mut self ) -> T {
739
724
if self . is_head {
@@ -817,12 +802,11 @@ where
817
802
}
818
803
}
819
804
820
- impl < T , Idx , K , S > Drop for FindMutInner < ' _ , T , Idx , K , S >
805
+ impl < T , Idx , K > Drop for FindMutView < ' _ , T , Idx , K >
821
806
where
822
807
T : Ord ,
823
808
Idx : SortedLinkedListIndex ,
824
809
K : Kind ,
825
- S : SortedLinkedListStorage < T , Idx > + ?Sized ,
826
810
{
827
811
fn drop ( & mut self ) {
828
812
// Only resort the list if the element has changed
@@ -833,12 +817,11 @@ where
833
817
}
834
818
}
835
819
836
- impl < T , Idx , K , S > Deref for FindMutInner < ' _ , T , Idx , K , S >
820
+ impl < T , Idx , K > Deref for FindMutView < ' _ , T , Idx , K >
837
821
where
838
822
T : Ord ,
839
823
Idx : SortedLinkedListIndex ,
840
824
K : Kind ,
841
- S : SortedLinkedListStorage < T , Idx > + ?Sized ,
842
825
{
843
826
type Target = T ;
844
827
@@ -848,12 +831,11 @@ where
848
831
}
849
832
}
850
833
851
- impl < T , Idx , K , S > DerefMut for FindMutInner < ' _ , T , Idx , K , S >
834
+ impl < T , Idx , K > DerefMut for FindMutView < ' _ , T , Idx , K >
852
835
where
853
836
T : Ord ,
854
837
Idx : SortedLinkedListIndex ,
855
838
K : Kind ,
856
- S : SortedLinkedListStorage < T , Idx > + ?Sized ,
857
839
{
858
840
fn deref_mut ( & mut self ) -> & mut Self :: Target {
859
841
self . maybe_changed = true ;
@@ -892,7 +874,7 @@ where
892
874
T : Ord + core:: fmt:: Debug ,
893
875
Idx : SortedLinkedListIndex ,
894
876
K : Kind ,
895
- S : SortedLinkedListStorage < T , Idx > + ? Sized ,
877
+ S : ? Sized + SortedLinkedListStorage < T , Idx > ,
896
878
{
897
879
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
898
880
f. debug_list ( ) . entries ( self . iter ( ) ) . finish ( )
0 commit comments