Replace uninitialized with MaybeUninit

This commit is contained in:
Matthias Krüger 2019-09-10 18:08:01 +02:00 committed by Christian Duerr
parent 8aa406b98b
commit 1067fa609b
3 changed files with 9 additions and 8 deletions

View File

@ -138,14 +138,14 @@ impl Cell {
#[inline] #[inline]
pub fn chars(&self) -> [char; MAX_ZEROWIDTH_CHARS + 1] { pub fn chars(&self) -> [char; MAX_ZEROWIDTH_CHARS + 1] {
unsafe { unsafe {
let mut chars = [std::mem::uninitialized(); MAX_ZEROWIDTH_CHARS + 1]; let mut chars = [std::mem::MaybeUninit::uninit(); MAX_ZEROWIDTH_CHARS + 1];
std::ptr::write(&mut chars[0], self.c); std::ptr::write(chars[0].as_mut_ptr(), self.c);
std::ptr::copy_nonoverlapping( std::ptr::copy_nonoverlapping(
self.extra.as_ptr(), self.extra.as_ptr() as *mut std::mem::MaybeUninit<char>,
chars.as_mut_ptr().offset(1), chars.as_mut_ptr().offset(1),
self.extra.len(), self.extra.len(),
); );
chars std::mem::transmute(chars)
} }
} }

View File

@ -143,7 +143,7 @@ pub struct List([Rgb; COUNT]);
impl<'a> From<&'a Colors> for List { impl<'a> From<&'a Colors> for List {
fn from(colors: &Colors) -> List { fn from(colors: &Colors) -> List {
// Type inference fails without this annotation // Type inference fails without this annotation
let mut list: List = unsafe { ::std::mem::uninitialized() }; let mut list = List([Rgb::default(); COUNT]);
list.fill_named(colors); list.fill_named(colors);
list.fill_cube(colors); list.fill_cube(colors);

View File

@ -13,7 +13,6 @@
// limitations under the License. // limitations under the License.
// //
//! tty related functionality //! tty related functionality
//!
use crate::config::{Config, Shell}; use crate::config::{Config, Shell};
use crate::display::OnResize; use crate::display::OnResize;
@ -36,6 +35,7 @@ use std::os::unix::{
use std::process::{Child, Command, Stdio}; use std::process::{Child, Command, Stdio};
use std::ptr; use std::ptr;
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use std::mem::MaybeUninit;
/// Process ID of child process /// Process ID of child process
/// ///
@ -91,15 +91,16 @@ struct Passwd<'a> {
/// If `buf` is changed while `Passwd` is alive, bad thing will almost certainly happen. /// If `buf` is changed while `Passwd` is alive, bad thing will almost certainly happen.
fn get_pw_entry(buf: &mut [i8; 1024]) -> Passwd<'_> { fn get_pw_entry(buf: &mut [i8; 1024]) -> Passwd<'_> {
// Create zeroed passwd struct // Create zeroed passwd struct
let mut entry: libc::passwd = unsafe { ::std::mem::uninitialized() }; let mut entry: MaybeUninit<libc::passwd> = MaybeUninit::uninit();
let mut res: *mut libc::passwd = ptr::null_mut(); let mut res: *mut libc::passwd = ptr::null_mut();
// Try and read the pw file. // Try and read the pw file.
let uid = unsafe { libc::getuid() }; let uid = unsafe { libc::getuid() };
let status = unsafe { let status = unsafe {
libc::getpwuid_r(uid, &mut entry, buf.as_mut_ptr() as *mut _, buf.len(), &mut res) libc::getpwuid_r(uid, entry.as_mut_ptr(), buf.as_mut_ptr() as *mut _, buf.len(), &mut res)
}; };
let entry = unsafe { entry.assume_init() };
if status < 0 { if status < 0 {
die!("getpwuid_r failed"); die!("getpwuid_r failed");