Skip to content

Commit

Permalink
Use MaybeUninit instead of mem::uninitialized
Browse files Browse the repository at this point in the history
  • Loading branch information
lqf96 committed Nov 12, 2019
1 parent 80a9bf0 commit 727837f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 4 additions & 4 deletions core-foundation/src/filedescriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
}
}

Expand Down
12 changes: 7 additions & 5 deletions core-foundation/src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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))
}
}
Expand Down

0 comments on commit 727837f

Please sign in to comment.