1
1
#[ cfg( feature = "serde" ) ]
2
2
use std:: fmt;
3
3
#[ cfg( any( feature = "serde" , feature = "lib-rustc-serialize" ) ) ]
4
+ use std:: mem:: MaybeUninit ;
5
+ #[ cfg( any( feature = "serde" , feature = "lib-rustc-serialize" ) ) ]
4
6
use std:: { ptr, slice, str} ;
5
7
6
8
#[ cfg( feature = "lib-rustc-serialize" ) ]
@@ -26,8 +28,9 @@ const HEX_LUT: &'static [u8] = b"\
26
28
27
29
#[ cfg( any( feature = "serde" , feature = "lib-rustc-serialize" ) ) ]
28
30
impl 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 ;
31
34
let lut_ptr = HEX_LUT . as_ptr ( ) ;
32
35
33
36
let r = ( ( self . 0 & 0xFF0000 ) >> 15 ) as isize ;
@@ -39,7 +42,7 @@ impl Color {
39
42
ptr:: copy_nonoverlapping ( lut_ptr. offset ( g) , buf_ptr. offset ( 2 ) , 2 ) ;
40
43
ptr:: copy_nonoverlapping ( lut_ptr. offset ( b) , buf_ptr. offset ( 4 ) , 2 ) ;
41
44
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 ( )
43
46
}
44
47
}
45
48
}
@@ -50,7 +53,7 @@ impl Serialize for Color {
50
53
where
51
54
S : Serializer ,
52
55
{
53
- let mut buf: [ u8 ; 6 ] = unsafe { :: std :: mem :: uninitialized ( ) } ;
56
+ let mut buf = MaybeUninit :: uninit ( ) ;
54
57
serializer. serialize_str ( self . as_str ( & mut buf) )
55
58
}
56
59
}
@@ -91,7 +94,7 @@ impl Encodable for Color {
91
94
where
92
95
S : Encoder ,
93
96
{
94
- let mut buf: [ u8 ; 6 ] = unsafe { :: std :: mem :: uninitialized ( ) } ;
97
+ let mut buf = MaybeUninit :: uninit ( ) ;
95
98
self . as_str ( & mut buf) . encode ( s)
96
99
}
97
100
}
@@ -112,7 +115,7 @@ impl Decodable for Color {
112
115
113
116
#[ test]
114
117
fn test_color ( ) {
115
- let mut buf: [ u8 ; 6 ] = unsafe { :: std :: mem :: uninitialized ( ) } ;
118
+ let mut buf = MaybeUninit :: uninit ( ) ;
116
119
let string = Color ( 0xA0A0A0 ) . as_str ( & mut buf) ;
117
120
assert_eq ! ( string, "A0A0A0" ) ;
118
121
}
0 commit comments