@@ -27,7 +27,7 @@ use core::fmt;
2727use core:: hash:: { Hasher , Hash } ;
2828use core:: iter:: FromIterator ;
2929use core:: mem;
30- use core:: ptr;
30+ use core:: ptr:: Shared ;
3131
3232/// A doubly-linked list.
3333#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -40,7 +40,7 @@ pub struct LinkedList<T> {
4040type Link < T > = Option < Box < Node < T > > > ;
4141
4242struct Rawlink < T > {
43- p : * mut T ,
43+ p : Option < Shared < T > > ,
4444}
4545
4646impl < T > Copy for Rawlink < T > { }
@@ -93,12 +93,12 @@ pub struct IntoIter<T> {
9393impl < T > Rawlink < T > {
9494 /// Like Option::None for Rawlink
9595 fn none ( ) -> Rawlink < T > {
96- Rawlink { p : ptr :: null_mut ( ) }
96+ Rawlink { p : None }
9797 }
9898
9999 /// Like Option::Some for Rawlink
100100 fn some ( n : & mut T ) -> Rawlink < T > {
101- Rawlink { p : n }
101+ unsafe { Rawlink { p : Some ( Shared :: new ( n ) ) } }
102102 }
103103
104104 /// Convert the `Rawlink` into an Option value
@@ -108,7 +108,7 @@ impl<T> Rawlink<T> {
108108 /// - Dereference of raw pointer.
109109 /// - Returns reference of arbitrary lifetime.
110110 unsafe fn resolve < ' a > ( & self ) -> Option < & ' a T > {
111- self . p . as_ref ( )
111+ self . p . map ( |p| & * * p )
112112 }
113113
114114 /// Convert the `Rawlink` into an Option value
@@ -118,7 +118,7 @@ impl<T> Rawlink<T> {
118118 /// - Dereference of raw pointer.
119119 /// - Returns reference of arbitrary lifetime.
120120 unsafe fn resolve_mut < ' a > ( & mut self ) -> Option < & ' a mut T > {
121- self . p . as_mut ( )
121+ self . p . map ( |p| & mut * * p )
122122 }
123123
124124 /// Return the `Rawlink` and replace with `Rawlink::none()`
@@ -984,6 +984,14 @@ impl<A: Hash> Hash for LinkedList<A> {
984984 }
985985}
986986
987+ // Ensure that `LinkedList` and its read-only iterators are covariant in their type parameters.
988+ #[ allow( dead_code) ]
989+ fn assert_covariance ( ) {
990+ fn a < ' a > ( x : LinkedList < & ' static str > ) -> LinkedList < & ' a str > { x }
991+ fn b < ' i , ' a > ( x : Iter < ' i , & ' static str > ) -> Iter < ' i , & ' a str > { x }
992+ fn c < ' a > ( x : IntoIter < & ' static str > ) -> IntoIter < & ' a str > { x }
993+ }
994+
987995#[ cfg( test) ]
988996mod tests {
989997 use std:: clone:: Clone ;
0 commit comments