@@ -6,7 +6,7 @@ use std::marker::PhantomData;
66/// This should be identical to the `Cell` implementation in the standard
77/// library, but always require that the internal type implements `Copy`
88/// and implements `Copy` itself.
9- #[ derive( PartialEq , Eq ) ]
9+ #[ derive( PartialEq , Eq , Copy , Clone ) ]
1010#[ repr( transparent) ]
1111pub struct CopyCell < T > {
1212 /// Internal value
@@ -23,8 +23,7 @@ unsafe impl<T> Send for CopyCell<T> {}
2323
2424impl < T > CopyCell < T > {
2525 /// Creates a new `CopyCell` containing the given value.
26- #[ inline]
27- pub fn new ( value : T ) -> Self {
26+ pub const fn new ( value : T ) -> Self {
2827 CopyCell {
2928 value,
3029 _no_sync : PhantomData
@@ -39,19 +38,6 @@ impl<T: Copy> CopyCell<T> {
3938 self . value
4039 }
4140
42- /// Returns a mutable reference to the underlying data.
43- ///
44- /// This call borrows `CopyCell` mutably, which gives us a compile time
45- /// memory safety guarantee.
46- #[ inline]
47- pub fn get_mut < ' a > ( & ' a mut self ) -> & ' a mut T {
48- // We can just cast the pointer from `CopyCell<T>` to `T` because of
49- // #[repr(transparent)]
50- unsafe {
51- & mut * ( self as * mut CopyCell < T > as * mut T )
52- }
53- }
54-
5541 /// Sets the contained value.
5642 #[ inline]
5743 pub fn set ( & self , value : T ) {
@@ -71,19 +57,10 @@ impl<T: Copy> CopyCell<T> {
7157 }
7258}
7359
74- impl < T : Copy > Clone for CopyCell < T > {
75- #[ inline]
76- fn clone ( & self ) -> CopyCell < T > {
77- CopyCell :: new ( self . get ( ) )
78- }
79- }
80-
81- impl < T : Copy > Copy for CopyCell < T > { }
82-
83- impl < T : Debug + Copy > Debug for CopyCell < T > {
60+ impl < T : Debug > Debug for CopyCell < T > {
8461 #[ inline]
8562 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
86- Debug :: fmt ( & self . get ( ) , f)
63+ Debug :: fmt ( & self . value , f)
8764 }
8865}
8966
@@ -94,8 +71,8 @@ mod test {
9471 #[ test]
9572 fn cell ( ) {
9673 let cell_a = CopyCell :: new ( 42u64 ) ;
97- let mut cell_b = cell_a; // copy
98- let cell_c = & cell_a; // reference
74+ let cell_b = cell_a; // copy
75+ let cell_c = & cell_a; // reference
9976
10077 assert_eq ! ( cell_a. get( ) , 42 ) ;
10178 assert_eq ! ( cell_b. get( ) , 42 ) ;
@@ -116,7 +93,7 @@ mod test {
11693 assert_eq ! ( cell_c. get( ) , 200 ) ;
11794
11895 // Again, only affects the copy
119- * cell_b. get_mut ( ) = 300 ;
96+ cell_b. set ( 300 ) ;
12097
12198 assert_eq ! ( cell_a. get( ) , 200 ) ;
12299 assert_eq ! ( cell_b. get( ) , 300 ) ;
0 commit comments