Skip to content

Commit

Permalink
Use mem::MaybeUninit instead of mem::uninitialized() since Rust 1.36
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Aug 21, 2019
1 parent 61d944c commit fe47137
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dirs = "2.0.1"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["consoleapi", "wincon", "handleapi", "fileapi"] }
rustversion = "0.1"

[features]
default=[]
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ pub fn stderr() -> Option<Box<StderrTerminal>> {
})
}

#[macro_use]
extern crate rustversion;
#[rustversion::before(1.36)]
unsafe fn uninitialized<T>() -> T {
::std::mem::uninitialized()
}

/// Terminal color definitions
#[allow(missing_docs)]
pub mod color {
Expand Down
17 changes: 13 additions & 4 deletions src/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ fn test_conout() {
assert!(conout().is_ok())
}

#[rustversion::before(1.36)]
unsafe fn uninitialized<T>() -> T {
::std::mem::uninitialized()
}
#[rustversion::since(1.36)]
unsafe fn uninitialized<T>() -> T {
::std::mem::MaybeUninit::uninit().assume_init()
}

impl WinConsoleInfo {
/// Returns `Err` whenever console info cannot be retrieved for some
/// reason.
Expand All @@ -169,7 +178,7 @@ impl WinConsoleInfo {
let bg;
let handle = conout()?;
unsafe {
let mut buffer_info = ::std::mem::uninitialized();
let mut buffer_info = uninitialized();
if GetConsoleScreenBufferInfo(*handle, &mut buffer_info) != 0 {
fg = bits_to_color(buffer_info.wAttributes);
bg = bits_to_color(buffer_info.wAttributes >> 4);
Expand Down Expand Up @@ -326,7 +335,7 @@ impl<T: Write + Send> Terminal for WinConsole<T> {
let _unused = self.buf.flush();
let handle = conout()?;
unsafe {
let mut buffer_info = ::std::mem::uninitialized();
let mut buffer_info = uninitialized();
if GetConsoleScreenBufferInfo(*handle, &mut buffer_info) != 0 {
let (x, y) = (
buffer_info.dwCursorPosition.X,
Expand Down Expand Up @@ -356,7 +365,7 @@ impl<T: Write + Send> Terminal for WinConsole<T> {
let _unused = self.buf.flush();
let handle = conout()?;
unsafe {
let mut buffer_info = ::std::mem::uninitialized();
let mut buffer_info = uninitialized();
if GetConsoleScreenBufferInfo(*handle, &mut buffer_info) == 0 {
return Err(io::Error::last_os_error().into());
}
Expand All @@ -382,7 +391,7 @@ impl<T: Write + Send> Terminal for WinConsole<T> {
let _unused = self.buf.flush();
let handle = conout()?;
unsafe {
let mut buffer_info = ::std::mem::uninitialized();
let mut buffer_info = uninitialized();
if GetConsoleScreenBufferInfo(*handle, &mut buffer_info) != 0 {
let COORD { X: x, Y: y } = buffer_info.dwCursorPosition;
if x == 0 {
Expand Down

0 comments on commit fe47137

Please sign in to comment.