@@ -104,17 +104,23 @@ impl<T> Rawlink<T> {
104
104
}
105
105
106
106
/// Convert the `Rawlink` into an Option value
107
- fn resolve_immut < ' a > ( & self ) -> Option < & ' a T > {
108
- unsafe {
109
- self . p . as_ref ( )
110
- }
107
+ ///
108
+ /// **unsafe** because:
109
+ ///
110
+ /// - Dereference of raw pointer.
111
+ /// - Returns reference of arbitrary lifetime.
112
+ unsafe fn resolve < ' a > ( & self ) -> Option < & ' a T > {
113
+ self . p . as_ref ( )
111
114
}
112
115
113
116
/// Convert the `Rawlink` into an Option value
114
- fn resolve < ' a > ( & mut self ) -> Option < & ' a mut T > {
115
- unsafe {
116
- self . p . as_mut ( )
117
- }
117
+ ///
118
+ /// **unsafe** because:
119
+ ///
120
+ /// - Dereference of raw pointer.
121
+ /// - Returns reference of arbitrary lifetime.
122
+ unsafe fn resolve_mut < ' a > ( & mut self ) -> Option < & ' a mut T > {
123
+ self . p . as_mut ( )
118
124
}
119
125
120
126
/// Return the `Rawlink` and replace with `Rawlink::none()`
@@ -179,7 +185,7 @@ impl<T> LinkedList<T> {
179
185
/// Add a Node last in the list
180
186
#[ inline]
181
187
fn push_back_node ( & mut self , mut new_tail : Box < Node < T > > ) {
182
- match self . list_tail . resolve ( ) {
188
+ match unsafe { self . list_tail . resolve_mut ( ) } {
183
189
None => return self . push_front_node ( new_tail) ,
184
190
Some ( tail) => {
185
191
self . list_tail = Rawlink :: some ( & mut * new_tail) ;
@@ -192,14 +198,16 @@ impl<T> LinkedList<T> {
192
198
/// Remove the last Node and return it, or None if the list is empty
193
199
#[ inline]
194
200
fn pop_back_node ( & mut self ) -> Option < Box < Node < T > > > {
195
- self . list_tail . resolve ( ) . map_or ( None , |tail| {
196
- self . length -= 1 ;
197
- self . list_tail = tail. prev ;
198
- match tail. prev . resolve ( ) {
199
- None => self . list_head . take ( ) ,
200
- Some ( tail_prev) => tail_prev. next . take ( )
201
- }
202
- } )
201
+ unsafe {
202
+ self . list_tail . resolve_mut ( ) . and_then ( |tail| {
203
+ self . length -= 1 ;
204
+ self . list_tail = tail. prev ;
205
+ match tail. prev . resolve_mut ( ) {
206
+ None => self . list_head . take ( ) ,
207
+ Some ( tail_prev) => tail_prev. next . take ( )
208
+ }
209
+ } )
210
+ }
203
211
}
204
212
}
205
213
@@ -246,7 +254,7 @@ impl<T> LinkedList<T> {
246
254
/// ```
247
255
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
248
256
pub fn append ( & mut self , other : & mut LinkedList < T > ) {
249
- match self . list_tail . resolve ( ) {
257
+ match unsafe { self . list_tail . resolve_mut ( ) } {
250
258
None => {
251
259
self . length = other. length ;
252
260
self . list_head = other. list_head . take ( ) ;
@@ -433,7 +441,9 @@ impl<T> LinkedList<T> {
433
441
#[ inline]
434
442
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
435
443
pub fn back ( & self ) -> Option < & T > {
436
- self . list_tail . resolve_immut ( ) . as_ref ( ) . map ( |tail| & tail. value )
444
+ unsafe {
445
+ self . list_tail . resolve ( ) . map ( |tail| & tail. value )
446
+ }
437
447
}
438
448
439
449
/// Provides a mutable reference to the back element, or `None` if the list
@@ -460,7 +470,9 @@ impl<T> LinkedList<T> {
460
470
#[ inline]
461
471
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
462
472
pub fn back_mut ( & mut self ) -> Option < & mut T > {
463
- self . list_tail . resolve ( ) . map ( |tail| & mut tail. value )
473
+ unsafe {
474
+ self . list_tail . resolve_mut ( ) . map ( |tail| & mut tail. value )
475
+ }
464
476
}
465
477
466
478
/// Adds an element first in the list.
@@ -609,12 +621,14 @@ impl<T> LinkedList<T> {
609
621
length : len - at
610
622
} ;
611
623
612
- // Swap split_node.next with list_head (which is None), nulling out split_node.next,
613
- // as it is the new tail.
614
- mem:: swap ( & mut split_node. resolve ( ) . unwrap ( ) . next , & mut splitted_list. list_head ) ;
615
- // Null out list_head.prev. Note this `unwrap` won't fail because if at == len
616
- // we already branched out at the top of the fn to return the empty list.
617
- splitted_list. list_head . as_mut ( ) . unwrap ( ) . prev = Rawlink :: none ( ) ;
624
+ unsafe {
625
+ // Swap split_node.next with list_head (which is None), nulling out split_node.next,
626
+ // as it is the new tail.
627
+ mem:: swap ( & mut split_node. resolve_mut ( ) . unwrap ( ) . next , & mut splitted_list. list_head ) ;
628
+ // Null out list_head.prev. Note this `unwrap` won't fail because if at == len
629
+ // we already branched out at the top of the fn to return the empty list.
630
+ splitted_list. list_head . as_mut ( ) . unwrap ( ) . prev = Rawlink :: none ( ) ;
631
+ }
618
632
// Fix the tail ptr
619
633
self . list_tail = split_node;
620
634
self . length = at;
@@ -666,11 +680,13 @@ impl<'a, A> DoubleEndedIterator for Iter<'a, A> {
666
680
if self . nelem == 0 {
667
681
return None ;
668
682
}
669
- self . tail . resolve_immut ( ) . as_ref ( ) . map ( |prev| {
670
- self . nelem -= 1 ;
671
- self . tail = prev. prev ;
672
- & prev. value
673
- } )
683
+ unsafe {
684
+ self . tail . resolve ( ) . map ( |prev| {
685
+ self . nelem -= 1 ;
686
+ self . tail = prev. prev ;
687
+ & prev. value
688
+ } )
689
+ }
674
690
}
675
691
}
676
692
@@ -685,14 +701,16 @@ impl<'a, A> Iterator for IterMut<'a, A> {
685
701
if self . nelem == 0 {
686
702
return None ;
687
703
}
688
- self . head . resolve ( ) . map ( |next| {
689
- self . nelem -= 1 ;
690
- self . head = match next. next {
691
- Some ( ref mut node) => Rawlink :: some ( & mut * * node) ,
692
- None => Rawlink :: none ( ) ,
693
- } ;
694
- & mut next. value
695
- } )
704
+ unsafe {
705
+ self . head . resolve_mut ( ) . map ( |next| {
706
+ self . nelem -= 1 ;
707
+ self . head = match next. next {
708
+ Some ( ref mut node) => Rawlink :: some ( & mut * * node) ,
709
+ None => Rawlink :: none ( ) ,
710
+ } ;
711
+ & mut next. value
712
+ } )
713
+ }
696
714
}
697
715
698
716
#[ inline]
@@ -708,11 +726,13 @@ impl<'a, A> DoubleEndedIterator for IterMut<'a, A> {
708
726
if self . nelem == 0 {
709
727
return None ;
710
728
}
711
- self . tail . resolve ( ) . map ( |prev| {
712
- self . nelem -= 1 ;
713
- self . tail = prev. prev ;
714
- & mut prev. value
715
- } )
729
+ unsafe {
730
+ self . tail . resolve_mut ( ) . map ( |prev| {
731
+ self . nelem -= 1 ;
732
+ self . tail = prev. prev ;
733
+ & mut prev. value
734
+ } )
735
+ }
716
736
}
717
737
}
718
738
@@ -726,10 +746,10 @@ impl<'a, A> IterMut<'a, A> {
726
746
// previously yielded element and self.head.
727
747
//
728
748
// The inserted node will not appear in further iteration.
729
- match self . head . resolve ( ) {
749
+ match unsafe { self . head . resolve_mut ( ) } {
730
750
None => { self . list . push_back_node ( ins_node) ; }
731
751
Some ( node) => {
732
- let prev_node = match node. prev . resolve ( ) {
752
+ let prev_node = match unsafe { node. prev . resolve_mut ( ) } {
733
753
None => return self . list . push_front_node ( ins_node) ,
734
754
Some ( prev) => prev,
735
755
} ;
@@ -795,7 +815,9 @@ impl<'a, A> IterMut<'a, A> {
795
815
if self . nelem == 0 {
796
816
return None
797
817
}
798
- self . head . resolve ( ) . map ( |head| & mut head. value )
818
+ unsafe {
819
+ self . head . resolve_mut ( ) . map ( |head| & mut head. value )
820
+ }
799
821
}
800
822
}
801
823
@@ -947,7 +969,7 @@ mod tests {
947
969
Some ( ref node) => node_ptr = & * * node,
948
970
}
949
971
loop {
950
- match ( last_ptr, node_ptr. prev . resolve_immut ( ) ) {
972
+ match unsafe { ( last_ptr, node_ptr. prev . resolve ( ) ) } {
951
973
( None , None ) => { }
952
974
( None , _ ) => panic ! ( "prev link for list_head" ) ,
953
975
( Some ( p) , Some ( pptr) ) => {
0 commit comments