11#[ cfg( feature = "serde" ) ]
22use std:: fmt;
33#[ cfg( any( feature = "serde" , feature = "lib-rustc-serialize" ) ) ]
4+ use std:: mem:: MaybeUninit ;
5+ #[ cfg( any( feature = "serde" , feature = "lib-rustc-serialize" ) ) ]
46use std:: { ptr, slice, str} ;
57
68#[ cfg( feature = "lib-rustc-serialize" ) ]
@@ -26,8 +28,9 @@ const HEX_LUT: &'static [u8] = b"\
2628
2729#[ cfg( any( feature = "serde" , feature = "lib-rustc-serialize" ) ) ]
2830impl Color {
29- fn as_str < ' a > ( self , buf : & ' a mut [ u8 ; 6 ] ) -> & ' a str {
30- let buf_ptr = buf. as_mut_ptr ( ) ;
31+ fn as_str ( self , buf : & mut MaybeUninit < [ u8 ; 6 ] > ) -> & str {
32+ let buf_len = 6 ;
33+ let buf_ptr = buf. as_mut_ptr ( ) as * mut u8 ;
3134 let lut_ptr = HEX_LUT . as_ptr ( ) ;
3235
3336 let r = ( ( self . 0 & 0xFF0000 ) >> 15 ) as isize ;
@@ -39,7 +42,7 @@ impl Color {
3942 ptr:: copy_nonoverlapping ( lut_ptr. offset ( g) , buf_ptr. offset ( 2 ) , 2 ) ;
4043 ptr:: copy_nonoverlapping ( lut_ptr. offset ( b) , buf_ptr. offset ( 4 ) , 2 ) ;
4144
42- str:: from_utf8 ( slice:: from_raw_parts ( buf_ptr, buf . len ( ) ) ) . unwrap ( )
45+ str:: from_utf8 ( slice:: from_raw_parts ( buf_ptr, buf_len ) ) . unwrap ( )
4346 }
4447 }
4548}
@@ -50,7 +53,7 @@ impl Serialize for Color {
5053 where
5154 S : Serializer ,
5255 {
53- let mut buf: [ u8 ; 6 ] = unsafe { :: std :: mem :: uninitialized ( ) } ;
56+ let mut buf = MaybeUninit :: uninit ( ) ;
5457 serializer. serialize_str ( self . as_str ( & mut buf) )
5558 }
5659}
@@ -91,7 +94,7 @@ impl Encodable for Color {
9194 where
9295 S : Encoder ,
9396 {
94- let mut buf: [ u8 ; 6 ] = unsafe { :: std :: mem :: uninitialized ( ) } ;
97+ let mut buf = MaybeUninit :: uninit ( ) ;
9598 self . as_str ( & mut buf) . encode ( s)
9699 }
97100}
@@ -112,7 +115,7 @@ impl Decodable for Color {
112115
113116#[ test]
114117fn test_color ( ) {
115- let mut buf: [ u8 ; 6 ] = unsafe { :: std :: mem :: uninitialized ( ) } ;
118+ let mut buf = MaybeUninit :: uninit ( ) ;
116119 let string = Color ( 0xA0A0A0 ) . as_str ( & mut buf) ;
117120 assert_eq ! ( string, "A0A0A0" ) ;
118121}
0 commit comments