From 9d704b32f4a100d283b0034f2183a769945fbb06 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 16 Jul 2020 14:21:29 +0200 Subject: [PATCH] Fix warning about deprecated mem::uninitialized() The API to use for this is now mem::MaybeUninit(). --- src/lib.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d54030b..636a1a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -122,7 +122,7 @@ extern crate libc; use std::io; -use std::mem; +use std::mem::MaybeUninit; use std::ops::{Deref,DerefMut}; use std::os::unix::io::RawFd; @@ -187,10 +187,12 @@ impl Termios { /// /// `fd` must be an open file descriptor for a terminal device. pub fn from_fd(fd: RawFd) -> io::Result { - let mut termios = unsafe { mem::uninitialized() }; + let mut termios = MaybeUninit::uninit(); - match tcgetattr(fd, &mut termios) { - Ok(_) => Ok(termios), + // We can’t use tcgetattr() here because Termios can only be safely initialised with a + // valid ::os::target::termios. + match io_result(unsafe { ffi::tcgetattr(fd, termios.as_mut_ptr()) }) { + Ok(()) => Ok(Termios { inner: unsafe { termios.assume_init() } }), Err(err) => Err(err) } } @@ -224,9 +226,9 @@ impl DerefMut for Termios { /// # Examples /// /// ``` -/// # use std::mem; +/// # use std::mem::MaybeUninit; /// # use termios::{Termios,B9600,cfsetispeed,cfgetispeed}; -/// # let mut termios = unsafe { mem::uninitialized() }; +/// # let mut termios = unsafe { MaybeUninit::uninit().assume_init() }; /// cfsetispeed(&mut termios, B9600).unwrap(); /// assert_eq!(cfgetispeed(&termios), B9600); /// ``` @@ -239,9 +241,9 @@ pub fn cfgetispeed(termios: &Termios) -> speed_t { /// # Examples /// /// ``` -/// # use std::mem; +/// # use std::mem::MaybeUninit; /// # use termios::{Termios,B9600,cfsetospeed,cfgetospeed}; -/// # let mut termios = unsafe { mem::uninitialized() }; +/// # let mut termios = unsafe { MaybeUninit::uninit().assume_init() }; /// cfsetospeed(&mut termios, B9600).unwrap(); /// assert_eq!(cfgetospeed(&termios), B9600); /// ``` @@ -281,9 +283,9 @@ pub fn cfgetospeed(termios: &Termios) -> speed_t { /// # Examples /// /// ``` -/// # use std::mem; +/// # use std::mem::MaybeUninit; /// # use termios::{Termios,B9600,cfsetispeed,cfgetispeed}; -/// # let mut termios = unsafe { mem::uninitialized() }; +/// # let mut termios = unsafe { MaybeUninit::uninit().assume_init() }; /// cfsetispeed(&mut termios, B9600).unwrap(); /// assert_eq!(cfgetispeed(&termios), B9600); /// ``` @@ -324,9 +326,9 @@ pub fn cfsetispeed(termios: &mut Termios, speed: speed_t) -> io::Result<()> { /// # Examples /// /// ``` -/// # use std::mem; +/// # use std::mem::MaybeUninit; /// # use termios::{Termios,B9600,cfsetospeed,cfgetospeed}; -/// # let mut termios = unsafe { mem::uninitialized() }; +/// # let mut termios = unsafe { MaybeUninit::uninit().assume_init() }; /// cfsetospeed(&mut termios, B9600).unwrap(); /// assert_eq!(cfgetospeed(&termios), B9600); /// ``` @@ -364,9 +366,9 @@ pub fn cfsetospeed(termios: &mut Termios, speed: speed_t) -> io::Result<()> { /// # Examples /// /// ``` -/// # use std::mem; +/// # use std::mem::MaybeUninit; /// # use termios::{Termios,B9600,cfsetspeed,cfgetispeed,cfgetospeed}; -/// # let mut termios = unsafe { mem::uninitialized() }; +/// # let mut termios = unsafe { MaybeUninit::uninit().assume_init() }; /// cfsetspeed(&mut termios, B9600).unwrap(); /// assert_eq!(cfgetispeed(&termios), B9600); /// assert_eq!(cfgetospeed(&termios), B9600);