summaryrefslogtreecommitdiffstats
path: root/vendor/errno/src/windows.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:39 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:39 +0000
commit1376c5a617be5c25655d0d7cb63e3beaa5a6e026 (patch)
tree3bb8d61aee02bc7a15eab3f36e3b921afc2075d0 /vendor/errno/src/windows.rs
parentReleasing progress-linux version 1.69.0+dfsg1-1~progress7.99u1. (diff)
downloadrustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.tar.xz
rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.zip
Merging upstream version 1.70.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/errno/src/windows.rs')
-rw-r--r--vendor/errno/src/windows.rs75
1 files changed, 43 insertions, 32 deletions
diff --git a/vendor/errno/src/windows.rs b/vendor/errno/src/windows.rs
index 94e736210..9c7c0e400 100644
--- a/vendor/errno/src/windows.rs
+++ b/vendor/errno/src/windows.rs
@@ -12,59 +12,70 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-#[cfg(feature = "std")]
-use std::ptr;
-use winapi::shared::minwindef::DWORD;
-#[cfg(feature = "std")]
-use winapi::shared::ntdef::WCHAR;
-#[cfg(feature = "std")]
-use winapi::um::winbase::{FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS};
+use core::char::{self, REPLACEMENT_CHARACTER};
+use core::ptr;
+use core::str;
+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;
+use crate::Errno;
-#[cfg(feature = "std")]
-pub fn with_description<F, T>(err: Errno, callback: F) -> T where
- F: FnOnce(Result<&str, Errno>) -> T
+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))
+ {
+ let c_len = c.len_utf8();
+ 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]) }
+}
+
+pub fn with_description<F, T>(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)
- 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(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 = FormatMessageW(
+ FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+ ptr::null_mut(),
+ err.0 as u32,
+ lang_id,
+ buf.as_mut_ptr(),
+ buf.len() as u32,
+ ptr::null_mut(),
+ );
if res == 0 {
// Sometimes FormatMessageW can fail e.g. system doesn't like lang_id
let fm_err = errno();
return callback(Err(fm_err));
}
- let msg = String::from_utf16_lossy(&buf[..res as usize]);
+ let mut msg = [0u8; 2048];
+ let msg = from_utf16_lossy(&buf[..res as usize], &mut msg[..]);
// Trim trailing CRLF inserted by FormatMessageW
- #[allow(deprecated)] // TODO: remove when MSRV >= 1.30
- callback(Ok(msg.trim_right()))
+ callback(Ok(msg.trim_end()))
}
}
-#[cfg(feature = "std")]
-pub const STRERROR_NAME: &'static str = "FormatMessageW";
+pub const STRERROR_NAME: &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) }
}