From 727837f12e8ba7ce4b86004fbdba2f2dfaf5c61b Mon Sep 17 00:00:00 2001 From: Qifan Lu Date: Mon, 11 Nov 2019 20:55:59 -0800 Subject: [PATCH] Use MaybeUninit instead of mem::uninitialized --- core-foundation/src/filedescriptor.rs | 8 ++++---- core-foundation/src/url.rs | 12 +++++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/core-foundation/src/filedescriptor.rs b/core-foundation/src/filedescriptor.rs index 2c999b519..061bd2d28 100644 --- a/core-foundation/src/filedescriptor.rs +++ b/core-foundation/src/filedescriptor.rs @@ -15,7 +15,7 @@ use core_foundation_sys::base::{kCFAllocatorDefault, CFOptionFlags}; use base::TCFType; use runloop::CFRunLoopSource; -use std::mem; +use std::mem::MaybeUninit; use std::os::unix::io::{AsRawFd, RawFd}; use std::ptr; @@ -46,9 +46,9 @@ impl CFFileDescriptor { pub fn context(&self) -> CFFileDescriptorContext { unsafe { - let mut context: CFFileDescriptorContext = mem::uninitialized(); - CFFileDescriptorGetContext(self.0, &mut context); - context + let mut context = MaybeUninit::::uninit(); + CFFileDescriptorGetContext(self.0, context.as_mut_ptr()); + context.assume_init() } } diff --git a/core-foundation/src/url.rs b/core-foundation/src/url.rs index 199ad2ec8..db0b9de0b 100644 --- a/core-foundation/src/url.rs +++ b/core-foundation/src/url.rs @@ -16,9 +16,9 @@ use string::{CFString}; use core_foundation_sys::base::{kCFAllocatorDefault, Boolean}; use std::fmt; +use std::mem::MaybeUninit; use std::ptr; use std::path::{Path, PathBuf}; -use std::mem; use libc::{c_char, strlen, PATH_MAX}; @@ -78,13 +78,15 @@ impl CFURL { pub fn to_path(&self) -> Option { // implementing this on Windows is more complicated because of the different OsStr representation unsafe { - let mut buf: [u8; PATH_MAX as usize] = mem::uninitialized(); - let result = CFURLGetFileSystemRepresentation(self.0, true as Boolean, buf.as_mut_ptr(), buf.len() as CFIndex); + let mut buf = MaybeUninit::<[u8; PATH_MAX as usize]>::uninit(); + let result = CFURLGetFileSystemRepresentation(self.0, true as Boolean, buf.as_mut_ptr() as *mut u8, PATH_MAX as CFIndex); if result == false as Boolean { return None; } - let len = strlen(buf.as_ptr() as *const c_char); - let path = OsStr::from_bytes(&buf[0..len]); + + let buf_init = buf.assume_init(); + let len = strlen(buf_init.as_ptr() as *const c_char); + let path = OsStr::from_bytes(&buf_init[0..len]); Some(PathBuf::from(path)) } }