Skip to content

Commit 5c76900

Browse files
yescallopgierens
authored andcommitted
Avoid zero initialization in Short
1 parent 08ac96d commit 5c76900

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/short.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::mem::MaybeUninit;
12
use std::{ ptr, str, slice, fmt };
23
use std::ops::Deref;
34

@@ -6,7 +7,7 @@ pub const MAX_LEN: usize = 30;
67
#[derive(Clone, Copy)]
78
pub struct Short {
89
len: u8,
9-
value: [u8; MAX_LEN],
10+
value: [MaybeUninit<u8>; MAX_LEN],
1011
}
1112

1213
/// A `Short` is a small string, up to `MAX_LEN` bytes, that can be managed without
@@ -23,11 +24,11 @@ impl Short {
2324
#[inline(always)]
2425
pub unsafe fn from_slice(slice: &str) -> Self {
2526
let mut short = Short {
26-
value: [0; MAX_LEN],
27+
value: MaybeUninit::uninit().assume_init(),
2728
len: slice.len() as u8,
2829
};
2930

30-
ptr::copy_nonoverlapping(slice.as_ptr(), short.value.as_mut_ptr(), slice.len());
31+
ptr::copy_nonoverlapping(slice.as_ptr(), short.value.as_mut_ptr() as _, slice.len());
3132

3233
short
3334
}
@@ -37,7 +38,7 @@ impl Short {
3738
pub fn as_str(&self) -> &str {
3839
unsafe {
3940
str::from_utf8_unchecked(
40-
slice::from_raw_parts(self.value.as_ptr(), self.len as usize)
41+
slice::from_raw_parts(self.value.as_ptr() as _, self.len as usize)
4142
)
4243
}
4344
}

0 commit comments

Comments
 (0)