@@ -17,10 +17,11 @@ use fmt;
1717use kinds:: { marker, Pod } ;
1818use ops:: { Deref , DerefMut , Drop } ;
1919use option:: { None , Option , Some } ;
20+ use ty:: Unsafe ;
2021
2122/// A mutable memory location that admits only `Pod` data.
2223pub struct Cell < T > {
23- priv value : T ,
24+ priv value : Unsafe < T > ,
2425 priv marker1 : marker:: InvariantType < T > ,
2526 priv marker2 : marker:: NoFreeze ,
2627 priv marker3 : marker:: NoShare ,
@@ -30,7 +31,7 @@ impl<T:Pod> Cell<T> {
3031 /// Creates a new `Cell` containing the given value.
3132 pub fn new ( value : T ) -> Cell < T > {
3233 Cell {
33- value : value,
34+ value : Unsafe { value : value , marker1 : marker :: InvariantType :: < T > } ,
3435 marker1 : marker:: InvariantType :: < T > ,
3536 marker2 : marker:: NoFreeze ,
3637 marker3 : marker:: NoShare ,
@@ -40,14 +41,14 @@ impl<T:Pod> Cell<T> {
4041 /// Returns a copy of the contained value.
4142 #[ inline]
4243 pub fn get ( & self ) -> T {
43- self . value
44+ unsafe { * self . value . get ( ) }
4445 }
4546
4647 /// Sets the contained value.
4748 #[ inline]
4849 pub fn set ( & self , value : T ) {
4950 unsafe {
50- * cast :: transmute_mut ( & self . value ) = value
51+ * self . value . get ( ) = value;
5152 }
5253 }
5354}
@@ -66,13 +67,13 @@ impl<T:Eq + Pod> Eq for Cell<T> {
6667
6768impl < T : fmt:: Show > fmt:: Show for Cell < T > {
6869 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
69- write ! ( f. buf, r"Cell \{ value: {} \}" , self . value)
70+ write ! ( f. buf, r"Cell \{ value: {} \}" , unsafe { * & self . value. get ( ) } )
7071 }
7172}
7273
7374/// A mutable memory location with dynamically checked borrow rules
7475pub struct RefCell < T > {
75- priv value : T ,
76+ priv value : Unsafe < T > ,
7677 priv borrow : BorrowFlag ,
7778 priv marker1 : marker:: InvariantType < T > ,
7879 priv marker2 : marker:: NoFreeze ,
@@ -94,15 +95,15 @@ impl<T> RefCell<T> {
9495 marker2 : marker:: NoFreeze ,
9596 marker3 : marker:: NoPod ,
9697 marker4 : marker:: NoShare ,
97- value : value,
98+ value : Unsafe { value : value , marker1 : marker :: InvariantType :: < T > } ,
9899 borrow : UNUSED ,
99100 }
100101 }
101102
102103 /// Consumes the `RefCell`, returning the wrapped value.
103104 pub fn unwrap ( self ) -> T {
104105 assert ! ( self . borrow == UNUSED ) ;
105- self . value
106+ unsafe { self . value . unwrap ( ) }
106107 }
107108
108109 unsafe fn as_mut < ' a > ( & ' a self ) -> & ' a mut RefCell < T > {
@@ -202,7 +203,7 @@ impl<T> RefCell<T> {
202203 #[ inline]
203204 pub fn set ( & self , value : T ) {
204205 let mut reference = self . borrow_mut ( ) ;
205- * reference. get ( ) = value
206+ * reference. get ( ) = value;
206207 }
207208}
208209
@@ -251,14 +252,14 @@ impl<'b, T> Ref<'b, T> {
251252 /// Retrieve an immutable reference to the stored value.
252253 #[ inline]
253254 pub fn get < ' a > ( & ' a self ) -> & ' a T {
254- & self . parent . value
255+ unsafe { & * self . parent . value . get ( ) }
255256 }
256257}
257258
258259impl < ' b , T > Deref < T > for Ref < ' b , T > {
259260 #[ inline]
260261 fn deref < ' a > ( & ' a self ) -> & ' a T {
261- & self . parent . value
262+ unsafe { & * self . parent . value . get ( ) }
262263 }
263264}
264265
@@ -279,21 +280,21 @@ impl<'b, T> RefMut<'b, T> {
279280 /// Retrieve a mutable reference to the stored value.
280281 #[ inline]
281282 pub fn get < ' a > ( & ' a mut self ) -> & ' a mut T {
282- & mut self . parent . value
283+ unsafe { & mut * self . parent . value . get ( ) }
283284 }
284285}
285286
286287impl < ' b , T > Deref < T > for RefMut < ' b , T > {
287288 #[ inline]
288289 fn deref < ' a > ( & ' a self ) -> & ' a T {
289- & self . parent . value
290+ unsafe { & * self . parent . value . get ( ) }
290291 }
291292}
292293
293294impl < ' b , T > DerefMut < T > for RefMut < ' b , T > {
294295 #[ inline]
295296 fn deref_mut < ' a > ( & ' a mut self ) -> & ' a mut T {
296- & mut self . parent . value
297+ unsafe { & mut * self . parent . value . get ( ) }
297298 }
298299}
299300
0 commit comments