Use MaybeUninit instead of mem::uninitialized

This commit is contained in:
Qifan Lu
2019-11-11 20:55:59 -08:00
parent 80a9bf00e4
commit 727837f12e
2 changed files with 11 additions and 9 deletions
+4 -4
View File
@@ -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::<CFFileDescriptorContext>::uninit();
CFFileDescriptorGetContext(self.0, context.as_mut_ptr());
context.assume_init()
}
}
+7 -5
View File
@@ -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<PathBuf> {
// 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))
}
}