1
1
use std:: cmp:: Ordering ;
2
- use std:: ptr:: NonNull ;
3
2
4
3
#[ derive( Debug ) ]
5
4
struct FixedVecNode < T > {
@@ -400,10 +399,16 @@ impl<T> FixedVec<T> {
400
399
/// ```
401
400
#[ inline]
402
401
pub fn iter_mut ( & mut self ) -> FixedVecIterMut < ' _ , T > {
403
- let head = self . head ;
404
- let tail = self . tail ;
405
- let len = self . len ( ) ;
406
- FixedVecIterMut :: new ( & mut self . nodes , head, tail, len)
402
+ FixedVecIterMut {
403
+ head : self . head ,
404
+ tail : self . tail ,
405
+ len : self . len ( ) ,
406
+ list : self ,
407
+ }
408
+ // let head = self.head;
409
+ // let tail = self.tail;
410
+ // let len = self.len();
411
+ // FixedVecIterMut::new(self, head, tail, len)
407
412
}
408
413
409
414
fn reorder ( & mut self ) {
@@ -670,46 +675,23 @@ impl<'a, T> DoubleEndedIterator for FixedVecIter<'a, T> {
670
675
}
671
676
}
672
677
678
+ #[ derive( Debug ) ]
673
679
pub struct FixedVecIterMut < ' a , T > {
674
- ptr : NonNull < Option < FixedVecNode < T > > > ,
680
+ list : & ' a mut FixedVec < T > ,
675
681
head : usize ,
676
682
tail : usize ,
677
683
len : usize ,
678
- _marker : std:: marker:: PhantomData < & ' a mut T > ,
679
- }
680
-
681
- impl < ' a , T > FixedVecIterMut < ' a , T > {
682
- #[ allow( unsafe_code) ]
683
- fn new (
684
- slice : & ' a mut [ Option < FixedVecNode < T > > ] ,
685
- head : usize ,
686
- tail : usize ,
687
- len : usize ,
688
- ) -> Self {
689
- let ptr = slice. as_mut_ptr ( ) ;
690
- Self {
691
- ptr : unsafe { NonNull :: new_unchecked ( ptr) } ,
692
- head,
693
- tail,
694
- len,
695
- _marker : std:: marker:: PhantomData ,
696
- }
697
- }
698
684
}
699
685
700
686
impl < ' a , T > Iterator for FixedVecIterMut < ' a , T > {
701
687
type Item = ( usize , & ' a mut T ) ;
702
688
703
- #[ allow( unsafe_code) ]
704
689
fn next ( & mut self ) -> Option < Self :: Item > {
705
690
if self . len > 0 {
706
691
let head = self . head ;
707
- let node_ref = unsafe {
708
- let ptr = NonNull :: new_unchecked ( self . ptr . as_ptr ( ) . add ( head) ) . as_ptr ( ) ;
709
- & mut * ptr
692
+ let node = unsafe {
693
+ core:: mem:: transmute :: < & ' _ mut FixedVecNode < T > , & ' a mut FixedVecNode < T > > ( self . list . node_mut ( head) . unwrap ( ) )
710
694
} ;
711
-
712
- let node = node_ref. as_mut ( ) . unwrap ( ) ;
713
695
self . head = node. next ;
714
696
self . len -= 1 ;
715
697
Some ( ( head, & mut node. data ) )
@@ -724,21 +706,17 @@ impl<'a, T> Iterator for FixedVecIterMut<'a, T> {
724
706
}
725
707
726
708
impl < ' a , T > DoubleEndedIterator for FixedVecIterMut < ' a , T > {
727
- #[ allow( unsafe_code) ]
728
709
fn next_back ( & mut self ) -> Option < Self :: Item > {
729
710
if self . len > 0 {
730
711
let tail = self . tail ;
731
- let node_ref = unsafe {
732
- let ptr = NonNull :: new_unchecked ( self . ptr . as_ptr ( ) . add ( tail) ) . as_ptr ( ) ;
733
- & mut * ptr
712
+ let node = unsafe {
713
+ core:: mem:: transmute :: < & ' _ mut FixedVecNode < T > , & ' a mut FixedVecNode < T > > ( self . list . node_mut ( tail) . unwrap ( ) )
734
714
} ;
735
-
736
- let node = node_ref. as_mut ( ) . unwrap ( ) ;
737
715
self . tail = node. prev ;
738
716
self . len -= 1 ;
739
717
Some ( ( tail, & mut node. data ) )
740
718
} else {
741
719
None
742
720
}
743
721
}
744
- }
722
+ }
0 commit comments