Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Arlie Davis committed Jun 10, 2024
1 parent ed40399 commit 72495ed
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 118 deletions.
4 changes: 4 additions & 0 deletions crates/libs/result/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ windows_targets::link!("oleaut32.dll" "system" fn SysFreeString(bstrstring : BST
windows_targets::link!("oleaut32.dll" "system" fn SysStringLen(pbstr : BSTR) -> u32);
pub type BOOL = i32;
pub type BSTR = *const u16;
pub const ERROR_INVALID_DATA: WIN32_ERROR = 13u32;
pub const ERROR_NO_UNICODE_TRANSLATION: WIN32_ERROR = 1113u32;
pub const E_INVALIDARG: HRESULT = 0x80070057_u32 as _;
pub const E_UNEXPECTED: HRESULT = 0x8000FFFF_u32 as _;
pub const FORMAT_MESSAGE_ALLOCATE_BUFFER: FORMAT_MESSAGE_OPTIONS = 256u32;
pub const FORMAT_MESSAGE_FROM_HMODULE: FORMAT_MESSAGE_OPTIONS = 2048u32;
pub const FORMAT_MESSAGE_FROM_SYSTEM: FORMAT_MESSAGE_OPTIONS = 4096u32;
Expand Down
130 changes: 12 additions & 118 deletions crates/libs/result/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,6 @@
use super::*;
use crate::HRESULT;
use core::ffi::c_void;

#[allow(dead_code)] // for cases that are not yet stabilized; see below
mod winerror {
use crate::HRESULT;

// We define these here for non-Windows platforms.
// It's just as easy to use them on Windows, too.

// Keep sorted by numeric value
pub const E_UNEXPECTED: HRESULT = HRESULT::from_u32(0x8000FFFF);
pub const E_FAIL: HRESULT = HRESULT::from_u32(0x80004005);
pub const E_OUTOFMEMORY: HRESULT = HRESULT::from_u32(0x8007000E);
pub const E_INVALIDARG: HRESULT = HRESULT::from_u32(0x80070057);

// Keep sorted by numeric value
pub const ERROR_ACCESS_DENIED: u32 = 5;
pub const ERROR_INVALID_DATA: u32 = 13;
pub const ERROR_HANDLE_EOF: u32 = 38;
pub const ERROR_HANDLE_DISK_FULL: u32 = 39;
pub const ERROR_NOT_SUPPORTED: u32 = 50;
pub const ERROR_BROKEN_PIPE: u32 = 109;
pub const ERROR_SEEK_ON_DEVICE: u32 = 132;
pub const ERROR_DIR_NOT_EMPTY: u32 = 145;
pub const ERROR_ALREADY_EXISTS: u32 = 183;
pub const ERROR_DIRECTORY_NOT_SUPPORTED: u32 = 336;
pub const ERROR_NO_UNICODE_TRANSLATION: u32 = 1113;
pub const ERROR_POSSIBLE_DEADLOCK: u32 = 1131;
pub const ERROR_NOT_FOUND: u32 = 1168;
pub const ERROR_DISK_QUOTA_EXCEEDED: u32 = 1295;
pub const ERROR_TIMEOUT: u32 = 1460;
pub const ERROR_FILE_READ_ONLY: u32 = 6009;
pub const WSAEINTR: u32 = 10004;
pub const WSAEWOULDBLOCK: u32 = 10035;
pub const WSAEADDRINUSE: u32 = 10048;
pub const WSAEADDRNOTAVAIL: u32 = 10049;
pub const WSAENETDOWN: u32 = 10050;
pub const WSAENETUNREACH: u32 = 10051;
pub const WSAECONNABORTED: u32 = 10053;
pub const WSAECONNRESET: u32 = 10054;
pub const WSAENOTCONN: u32 = 10057;
pub const WSAECONNREFUSED: u32 = 10061;
pub const WSAEHOSTUNREACH: u32 = 10065;
}

#[allow(unused_imports)] // due to #[cfg] differences
use winerror::{ERROR_INVALID_DATA, ERROR_NO_UNICODE_TRANSLATION, E_INVALIDARG, *};

/// An error object consists of both an error code as well as detailed error information for debugging.
#[derive(Clone, PartialEq, Eq)]
pub struct Error {
Expand Down Expand Up @@ -193,17 +146,15 @@ impl From<Error> for HRESULT {
}

impl From<HRESULT> for Error {
#[allow(unused_mut)]
fn from(code: HRESULT) -> Self {
#[cfg(windows)]
{
let mut info = None;
unsafe { GetErrorInfo(0, &mut info as *mut _ as _) };
Self { code, info }
}
#[cfg(not(windows))]
{
Self { code }
Self {
code,
#[cfg(windows)]
info: {
let mut info = None;
unsafe { GetErrorInfo(0, &mut info as *mut _ as _) };
info
},
}
}
}
Expand All @@ -218,68 +169,11 @@ impl From<Error> for std::io::Error {
#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
fn from(from: std::io::Error) -> Self {
#[cfg(windows)]
{
if let Some(status) = from.raw_os_error() {
return HRESULT::from_win32(status as u32).into();
}
}

use std::io::ErrorKind;

const fn w32(code: u32) -> HRESULT {
HRESULT::from_win32(code)
match from.raw_os_error() {
Some(status) => HRESULT::from_win32(status as u32).into(),
#[cfg(windows)]
None => HRESULT(E_UNEXPECTED).into(),
}

// Many ErrorKind variants are not yet stabilized. Some of them have
// obvious equivalents in Win32 error codes; some do not. We list as
// many as we can here so that enabling them (as they become stable)
// is easy.

let hr: HRESULT = match from.kind() {
ErrorKind::NotFound => w32(ERROR_NOT_FOUND),
ErrorKind::PermissionDenied => w32(ERROR_ACCESS_DENIED),
ErrorKind::ConnectionRefused => w32(WSAECONNREFUSED),
ErrorKind::ConnectionReset => w32(WSAECONNRESET),
// not yet stable: ErrorKind::HostUnreachable => w32(WSAEHOSTUNREACH),
// not yet stable: ErrorKind::NetworkUnreachable => w32(WSAENETUNREACH),
ErrorKind::ConnectionAborted => w32(WSAECONNABORTED),
ErrorKind::NotConnected => w32(WSAENOTCONN),
ErrorKind::AddrInUse => w32(WSAEADDRINUSE),
ErrorKind::AddrNotAvailable => w32(WSAEADDRNOTAVAIL),
// not yet stable: ErrorKind::NetworkDown => w32(WSAENETDOWN),
ErrorKind::BrokenPipe => w32(ERROR_BROKEN_PIPE),
ErrorKind::AlreadyExists => w32(ERROR_ALREADY_EXISTS),
ErrorKind::WouldBlock => w32(WSAEWOULDBLOCK),
// not yet stable: ErrorKind::NotADirectory => _,
// not yet stable: ErrorKind::IsADirectory => w32(ERROR_DIRECTORY_NOT_SUPPORTED),
// not yet stable: ErrorKind::DirectoryNotEmpty => w32(ERROR_DIR_NOT_EMPTY),
// not yet stable: ErrorKind::ReadOnlyFilesystem => w32(ERROR_FILE_READ_ONLY),
// not yet stable: ErrorKind::FilesystemLoop => (),
// not yet stable: ErrorKind::StaleNetworkFileHandle => (),
ErrorKind::InvalidInput => E_INVALIDARG,
ErrorKind::InvalidData => w32(ERROR_INVALID_DATA),
ErrorKind::TimedOut => w32(ERROR_TIMEOUT),
ErrorKind::WriteZero => E_FAIL,
// not yet stable: ErrorKind::StorageFull => w32(ERROR_HANDLE_DISK_FULL),
// not yet stable: ErrorKind::NotSeekable => w32(ERROR_SEEK_ON_DEVICE),
// not yet stable: ErrorKind::FilesystemQuotaExceeded => w32(ERROR_DISK_QUOTA_EXCEEDED),
// not yet stable: ErrorKind::FileTooLarge => _,
// not yet stable: ErrorKind::ResourceBusy => _,
// not yet stable: ErrorKind::ExecutableFileBusy => _,
// not yet stable: ErrorKind::Deadlock => w32(ERROR_POSSIBLE_DEADLOCK),
// not yet stable: ErrorKind::CrossesDevices => _,
// not yet stable: ErrorKind::TooManyLinks => _,
// not yet stable: ErrorKind::InvalidFilename => _,
// not yet stable: ErrorKind::ArgumentListTooLong => _,
ErrorKind::Interrupted => w32(WSAEINTR),
ErrorKind::Unsupported => w32(ERROR_NOT_SUPPORTED),
ErrorKind::UnexpectedEof => w32(ERROR_HANDLE_EOF),
ErrorKind::OutOfMemory => E_OUTOFMEMORY,
ErrorKind::Other => E_FAIL,
_ => E_FAIL,
};
Self::from_hresult(hr)
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/libs/result/tests/bindings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
--config flatten sys minimal vtbl no-bindgen-comment

--filter
Windows.Win32.Foundation.E_INVALIDARG
Windows.Win32.Foundation.E_UNEXPECTED
Windows.Win32.Foundation.ERROR_INVALID_DATA
Windows.Win32.Foundation.ERROR_NO_UNICODE_TRANSLATION
Windows.Win32.Foundation.GetLastError
Windows.Win32.Foundation.SysFreeString
Windows.Win32.Foundation.SysStringLen
Expand Down

0 comments on commit 72495ed

Please sign in to comment.