From 131cf5044e228daa86eda22685f4171b054e3335 Mon Sep 17 00:00:00 2001 From: LingMan Date: Fri, 16 Dec 2022 01:29:12 +0100 Subject: [PATCH 1/2] Run `cargo fmt` --- src/hermit.rs | 8 +++---- src/lib.rs | 39 +++++++++++++++++++++++---------- src/unix.rs | 58 +++++++++++++++++++++++++------------------------- src/wasi.rs | 18 +++++++--------- src/windows.rs | 42 +++++++++++++++++++----------------- 5 files changed, 91 insertions(+), 74 deletions(-) diff --git a/src/hermit.rs b/src/hermit.rs index d14a6c8..88472ee 100644 --- a/src/hermit.rs +++ b/src/hermit.rs @@ -16,8 +16,9 @@ use Errno; -pub fn with_description(_err: Errno, callback: F) -> T where - F: FnOnce(Result<&str, Errno>) -> T +pub fn with_description(_err: Errno, callback: F) -> T +where + F: FnOnce(Result<&str, Errno>) -> T, { callback(Ok("unknown error")) } @@ -28,5 +29,4 @@ pub fn errno() -> Errno { Errno(0) } -pub fn set_errno(_: Errno) { -} +pub fn set_errno(_: Errno) {} diff --git a/src/lib.rs b/src/lib.rs index d91ab1b..4183e73 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,11 +23,16 @@ #[cfg(feature = "std")] extern crate core; -#[cfg(unix)] extern crate libc; -#[cfg(windows)] extern crate winapi; -#[cfg(target_os = "dragonfly")] extern crate errno_dragonfly; -#[cfg(target_os = "wasi")] extern crate libc; -#[cfg(target_os = "hermit")] extern crate libc; +#[cfg(target_os = "dragonfly")] +extern crate errno_dragonfly; +#[cfg(unix)] +extern crate libc; +#[cfg(target_os = "wasi")] +extern crate libc; +#[cfg(target_os = "hermit")] +extern crate libc; +#[cfg(windows)] +extern crate winapi; #[cfg_attr(unix, path = "unix.rs")] #[cfg_attr(windows, path = "windows.rs")] @@ -37,9 +42,9 @@ mod sys; use core::fmt; #[cfg(feature = "std")] -use std::io; -#[cfg(feature = "std")] use std::error::Error; +#[cfg(feature = "std")] +use std::io; /// Wraps a platform-specific error code. /// @@ -68,8 +73,12 @@ impl fmt::Display for Errno { sys::with_description(*self, |desc| match desc { Ok(desc) => fmt.write_str(&desc), Err(fm_err) => write!( - fmt, "OS error {} ({} returned error {})", - self.0, sys::STRERROR_NAME, fm_err.0), + fmt, + "OS error {} ({} returned error {})", + self.0, + sys::STRERROR_NAME, + fm_err.0 + ), }) } } @@ -134,13 +143,21 @@ fn check_description() { "Operation not permitted" }; - let errno_code = if cfg!(target_os = "haiku") { -2147483633 } else { 1 }; + let errno_code = if cfg!(target_os = "haiku") { + -2147483633 + } else { + 1 + }; set_errno(Errno(errno_code)); assert_eq!(errno().to_string(), expect); assert_eq!( format!("{:?}", errno()), - format!("Errno {{ code: {}, description: Some({:?}) }}", errno_code, expect)); + format!( + "Errno {{ code: {}, description: Some({:?}) }}", + errno_code, expect + ) + ); } #[cfg(feature = "std")] diff --git a/src/unix.rs b/src/unix.rs index f9d8157..c682554 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -13,21 +13,22 @@ // except according to those terms. use core::str; -use libc::{self, c_char, c_int, strlen}; #[cfg(target_os = "dragonfly")] use errno_dragonfly::errno_location; +use libc::{self, c_char, c_int, strlen}; use Errno; fn from_utf8_lossy(input: &[u8]) -> &str { match str::from_utf8(input) { Ok(valid) => valid, - Err(error) => unsafe { str::from_utf8_unchecked(&input[.. error.valid_up_to()]) }, + Err(error) => unsafe { str::from_utf8_unchecked(&input[..error.valid_up_to()]) }, } } -pub fn with_description(err: Errno, callback: F) -> T where - F: FnOnce(Result<&str, Errno>) -> T +pub fn with_description(err: Errno, callback: F) -> T +where + F: FnOnce(Result<&str, Errno>) -> T, { let mut buf = [0u8; 1024]; let c_str = unsafe { @@ -38,7 +39,7 @@ pub fn with_description(err: Errno, callback: F) -> T where } } let c_str_len = strlen(buf.as_ptr() as *const _); - &buf[.. c_str_len] + &buf[..c_str_len] }; callback(Ok(from_utf8_lossy(c_str))) } @@ -46,9 +47,7 @@ pub fn with_description(err: Errno, callback: F) -> T where pub const STRERROR_NAME: &'static str = "strerror_r"; pub fn errno() -> Errno { - unsafe { - Errno(*errno_location()) - } + unsafe { Errno(*errno_location()) } } pub fn set_errno(Errno(errno): Errno) { @@ -57,29 +56,30 @@ pub fn set_errno(Errno(errno): Errno) { } } -extern { +extern "C" { #[cfg(not(target_os = "dragonfly"))] - #[cfg_attr(any(target_os = "macos", - target_os = "ios", - target_os = "freebsd"), - link_name = "__error")] - #[cfg_attr(any(target_os = "openbsd", - target_os = "netbsd", - target_os = "bitrig", - target_os = "android"), - link_name = "__errno")] - #[cfg_attr(any(target_os = "solaris", - target_os = "illumos"), - link_name = "___errno")] - #[cfg_attr(target_os = "haiku", - link_name = "_errnop")] - #[cfg_attr(target_os = "linux", - link_name = "__errno_location")] - #[cfg_attr(target_os = "aix", - link_name = "_Errno")] + #[cfg_attr( + any(target_os = "macos", target_os = "ios", target_os = "freebsd"), + link_name = "__error" + )] + #[cfg_attr( + any( + target_os = "openbsd", + target_os = "netbsd", + target_os = "bitrig", + target_os = "android" + ), + link_name = "__errno" + )] + #[cfg_attr( + any(target_os = "solaris", target_os = "illumos"), + link_name = "___errno" + )] + #[cfg_attr(target_os = "haiku", link_name = "_errnop")] + #[cfg_attr(target_os = "linux", link_name = "__errno_location")] + #[cfg_attr(target_os = "aix", link_name = "_Errno")] fn errno_location() -> *mut c_int; #[cfg_attr(target_os = "linux", link_name = "__xpg_strerror_r")] - fn strerror_r(errnum: c_int, buf: *mut c_char, - buflen: libc::size_t) -> c_int; + fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: libc::size_t) -> c_int; } diff --git a/src/wasi.rs b/src/wasi.rs index cc40f0e..fe6e09c 100644 --- a/src/wasi.rs +++ b/src/wasi.rs @@ -20,12 +20,13 @@ use Errno; fn from_utf8_lossy(input: &[u8]) -> &str { match str::from_utf8(input) { Ok(valid) => valid, - Err(error) => unsafe { str::from_utf8_unchecked(&input[.. error.valid_up_to()]) }, + Err(error) => unsafe { str::from_utf8_unchecked(&input[..error.valid_up_to()]) }, } } -pub fn with_description(err: Errno, callback: F) -> T where - F: FnOnce(Result<&str, Errno>) -> T +pub fn with_description(err: Errno, callback: F) -> T +where + F: FnOnce(Result<&str, Errno>) -> T, { let mut buf = [0u8; 1024]; let c_str = unsafe { @@ -36,7 +37,7 @@ pub fn with_description(err: Errno, callback: F) -> T where } } let c_str_len = strlen(buf.as_ptr() as *const _); - &buf[.. c_str_len] + &buf[..c_str_len] }; callback(Ok(from_utf8_lossy(c_str))) } @@ -45,9 +46,7 @@ pub const STRERROR_NAME: &'static str = "strerror_r"; pub fn errno() -> Errno { // libc_errno is thread-local, so simply read its value. - unsafe { - Errno(libc_errno) - } + unsafe { Errno(libc_errno) } } pub fn set_errno(Errno(new_errno): Errno) { @@ -57,11 +56,10 @@ pub fn set_errno(Errno(new_errno): Errno) { } } -extern { +extern "C" { #[thread_local] #[link_name = "errno"] static mut libc_errno: c_int; - fn strerror_r(errnum: c_int, buf: *mut c_char, - buflen: libc::size_t) -> c_int; + fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: libc::size_t) -> c_int; } diff --git a/src/windows.rs b/src/windows.rs index eb2a8f9..4238691 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -23,17 +23,22 @@ use Errno; fn from_utf16_lossy<'a>(input: &[u16], output: &'a mut [u8]) -> &'a str { let mut output_len = 0; - for c in char::decode_utf16(input.iter().copied().take_while(|&x| x != 0)).map(|x| x.unwrap_or(REPLACEMENT_CHARACTER)) { + for c in char::decode_utf16(input.iter().copied().take_while(|&x| x != 0)) + .map(|x| x.unwrap_or(REPLACEMENT_CHARACTER)) + { let c_len = c.len_utf8(); - if c_len > output.len() - output_len { break; } - c.encode_utf8(&mut output[output_len ..]); + if c_len > output.len() - output_len { + break; + } + c.encode_utf8(&mut output[output_len..]); output_len += c_len; } - unsafe { str::from_utf8_unchecked(&output[.. output_len]) } + unsafe { str::from_utf8_unchecked(&output[..output_len]) } } -pub fn with_description(err: Errno, callback: F) -> T where - F: FnOnce(Result<&str, Errno>) -> T +pub fn with_description(err: Errno, callback: F) -> T +where + F: FnOnce(Result<&str, Errno>) -> T, { // This value is calculated from the macro // MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT) @@ -42,14 +47,15 @@ pub fn with_description(err: Errno, callback: F) -> T where let mut buf = [0 as WCHAR; 2048]; unsafe { - let res = ::winapi::um::winbase::FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - ptr::null_mut(), - err.0 as DWORD, - lang_id, - buf.as_mut_ptr(), - buf.len() as DWORD, - ptr::null_mut()); + let res = ::winapi::um::winbase::FormatMessageW( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + ptr::null_mut(), + err.0 as DWORD, + lang_id, + buf.as_mut_ptr(), + buf.len() as DWORD, + ptr::null_mut(), + ); if res == 0 { // Sometimes FormatMessageW can fail e.g. system doesn't like lang_id let fm_err = errno(); @@ -67,13 +73,9 @@ pub fn with_description(err: Errno, callback: F) -> T where pub const STRERROR_NAME: &'static str = "FormatMessageW"; pub fn errno() -> Errno { - unsafe { - Errno(::winapi::um::errhandlingapi::GetLastError() as i32) - } + unsafe { Errno(::winapi::um::errhandlingapi::GetLastError() as i32) } } pub fn set_errno(Errno(errno): Errno) { - unsafe { - ::winapi::um::errhandlingapi::SetLastError(errno as DWORD) - } + unsafe { ::winapi::um::errhandlingapi::SetLastError(errno as DWORD) } } From 11dfeff6f2b086fe48ee18178e7ad81fec15c172 Mon Sep 17 00:00:00 2001 From: LingMan Date: Fri, 16 Dec 2022 01:24:20 +0100 Subject: [PATCH 2/2] Switch from winapi to windows-sys The ecosystem is switching from winapi to the Microsoft-maintained windows-sys. Follow suit so depentents don't have to compile both. This raises the MSRV to 1.48. --- .github/workflows/main.yml | 4 ++-- CHANGELOG.md | 8 +++++--- Cargo.toml | 8 ++++++-- README.md | 2 +- src/lib.rs | 2 +- src/windows.rs | 21 +++++++++++---------- 6 files changed, 26 insertions(+), 19 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 55abb9d..a802f97 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,11 +16,11 @@ jobs: strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] - rust: [stable, nightly, 1.36.0] + rust: [stable, nightly, 1.48] exclude: # TODO(#52): Investigate why this is broken - os: macos-latest - rust: 1.36.0 + rust: 1.48 steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 699d3d6..537e6e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,11 @@ - Add formatting with `#![no_std]` [#44](https://github.com/lambda-fairy/rust-errno/pull/44) - -- Update minimum Rust version to 1.36 - [#48](https://github.com/lambda-fairy/rust-errno/pull/48) + +- Switch from `winapi` to `windows-sys` [#55](https://github.com/lambda-fairy/rust-errno/pull/55) + +- Update minimum Rust version to 1.48 + [#48](https://github.com/lambda-fairy/rust-errno/pull/48) [#55](https://github.com/lambda-fairy/rust-errno/pull/55) # [0.2.8] - 2021-10-27 diff --git a/Cargo.toml b/Cargo.toml index 21b4fe4..b083199 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,12 @@ categories = ["no-std", "os"] [target.'cfg(unix)'.dependencies] libc = "0.2" -[target.'cfg(windows)'.dependencies] -winapi = { version = "0.3", features = ["errhandlingapi", "minwindef", "ntdef", "winbase"] } +[target.'cfg(windows)'.dependencies.windows-sys] +version = "0.42" +features = [ + "Win32_Foundation", + "Win32_System_Diagnostics_Debug", +] [target.'cfg(target_os="dragonfly")'.dependencies] errno-dragonfly = "0.1.1" diff --git a/README.md b/README.md index 9ae40fb..610ad84 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # errno [![CI](https://github.com/lambda-fairy/rust-errno/actions/workflows/main.yml/badge.svg)](https://github.com/lambda-fairy/rust-errno/actions/workflows/main.yml) [![Cargo](https://img.shields.io/crates/v/errno.svg)](https://crates.io/crates/errno) -Cross-platform interface to the [`errno`][errno] variable. Works on Rust 1.36 or newer. +Cross-platform interface to the [`errno`][errno] variable. Works on Rust 1.48 or newer. Documentation is available at . diff --git a/src/lib.rs b/src/lib.rs index 4183e73..abb0137 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,7 @@ extern crate libc; #[cfg(target_os = "hermit")] extern crate libc; #[cfg(windows)] -extern crate winapi; +extern crate windows_sys; #[cfg_attr(unix, path = "unix.rs")] #[cfg_attr(windows, path = "windows.rs")] diff --git a/src/windows.rs b/src/windows.rs index 4238691..a0fc0ed 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -15,9 +15,10 @@ use core::char::{self, REPLACEMENT_CHARACTER}; use core::ptr; use core::str; -use winapi::shared::minwindef::DWORD; -use winapi::shared::ntdef::WCHAR; -use winapi::um::winbase::{FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS}; +use windows_sys::Win32::Foundation::{GetLastError, SetLastError, WIN32_ERROR}; +use windows_sys::Win32::System::Diagnostics::Debug::{ + FormatMessageW, FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS, +}; use Errno; @@ -42,18 +43,18 @@ where { // This value is calculated from the macro // MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT) - let lang_id = 0x0800 as DWORD; + let lang_id = 0x0800_u32; - let mut buf = [0 as WCHAR; 2048]; + let mut buf = [0u16; 2048]; unsafe { - let res = ::winapi::um::winbase::FormatMessageW( + let res = FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, ptr::null_mut(), - err.0 as DWORD, + err.0 as u32, lang_id, buf.as_mut_ptr(), - buf.len() as DWORD, + buf.len() as u32, ptr::null_mut(), ); if res == 0 { @@ -73,9 +74,9 @@ where pub const STRERROR_NAME: &'static str = "FormatMessageW"; pub fn errno() -> Errno { - unsafe { Errno(::winapi::um::errhandlingapi::GetLastError() as i32) } + unsafe { Errno(GetLastError() as i32) } } pub fn set_errno(Errno(errno): Errno) { - unsafe { ::winapi::um::errhandlingapi::SetLastError(errno as DWORD) } + unsafe { SetLastError(errno as WIN32_ERROR) } }