Skip to content

Commit 404397e

Browse files
authored
Promote remaining error types to windows-result (#3783)
1 parent 2670070 commit 404397e

File tree

85 files changed

+12329
-12228
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+12329
-12228
lines changed

crates/libs/bindgen/src/config/cpp_handle.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::*;
22

33
impl Config<'_> {
44
pub fn write_cpp_handle(&self, def: TypeDef) -> TokenStream {
5-
let tn = def.type_name();
65
let name = to_ident(def.name());
76
let ty = def.underlying_type();
87
let ty_name = ty.write_name(self);
@@ -90,14 +89,7 @@ impl Config<'_> {
9089
quote! {}
9190
};
9291

93-
let must_use = if matches!(tn, TypeName::NTSTATUS | TypeName::RPC_STATUS) {
94-
quote! { #[must_use] }
95-
} else {
96-
quote! {}
97-
};
98-
9992
let mut result = quote! {
100-
#must_use
10193
#[repr(transparent)]
10294
#[derive(#derive)]
10395
pub struct #name(pub #ty_name);

crates/libs/bindgen/src/lib.rs

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -353,32 +353,69 @@ where
353353
panic!("exactly one `--out` is required");
354354
};
355355

356-
if !sys && !no_deps {
357-
references.insert(
358-
0,
359-
ReferenceStage::parse("windows_collections,flat,Windows.Foundation.Collections"),
360-
);
361-
references.insert(
362-
0,
363-
ReferenceStage::parse("windows_numerics,flat,Windows.Foundation.Numerics"),
364-
);
365-
references.insert(
366-
0,
367-
ReferenceStage::parse("windows_future,flat,Windows.Foundation.Async*"),
368-
);
369-
references.insert(
370-
0,
371-
ReferenceStage::parse("windows_future,flat,Windows.Foundation.IAsync*"),
372-
);
373-
}
374-
375356
// This isn't strictly necessary but avoids a common newbie pitfall where all metadata
376357
// would be generated when building a component for a specific API.
377358
if include.is_empty() {
378359
panic!("at least one `--filter` required");
379360
}
380361

381362
let reader = Reader::new(expand_input(&input));
363+
364+
if !sys && !no_deps {
365+
if reader.contains_key("Windows.Foundation") {
366+
references.insert(
367+
0,
368+
ReferenceStage::parse("windows_collections,flat,Windows.Foundation.Collections"),
369+
);
370+
references.insert(
371+
0,
372+
ReferenceStage::parse("windows_numerics,flat,Windows.Foundation.Numerics"),
373+
);
374+
references.insert(
375+
0,
376+
ReferenceStage::parse("windows_future,flat,Windows.Foundation.Async*"),
377+
);
378+
references.insert(
379+
0,
380+
ReferenceStage::parse("windows_future,flat,Windows.Foundation.IAsync*"),
381+
);
382+
}
383+
384+
if reader.contains_key("Windows.Win32.Foundation") {
385+
if specific_deps {
386+
references.insert(
387+
0,
388+
ReferenceStage::parse(
389+
"windows_result,flat,Windows.Win32.Foundation.WIN32_ERROR",
390+
),
391+
);
392+
references.insert(
393+
0,
394+
ReferenceStage::parse("windows_result,flat,Windows.Win32.Foundation.NTSTATUS"),
395+
);
396+
references.insert(
397+
0,
398+
ReferenceStage::parse(
399+
"windows_result,flat,Windows.Win32.System.Rpc.RPC_STATUS",
400+
),
401+
);
402+
} else {
403+
references.insert(
404+
0,
405+
ReferenceStage::parse("windows_core,flat,Windows.Win32.Foundation.WIN32_ERROR"),
406+
);
407+
references.insert(
408+
0,
409+
ReferenceStage::parse("windows_core,flat,Windows.Win32.Foundation.NTSTATUS"),
410+
);
411+
references.insert(
412+
0,
413+
ReferenceStage::parse("windows_core,flat,Windows.Win32.System.Rpc.RPC_STATUS"),
414+
);
415+
}
416+
}
417+
}
418+
382419
let filter = Filter::new(&reader, &include, &exclude);
383420
let references = References::new(&reader, references);
384421
let types = TypeMap::filter(&reader, &filter, &references);

crates/libs/bindgen/src/type_name.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ impl TypeName {
3131
pub const IIterable: Self = Self("Windows.Foundation.Collections", "IIterable");
3232
pub const IIterator: Self = Self("Windows.Foundation.Collections", "IIterator");
3333

34-
pub const NTSTATUS: Self = Self("Windows.Win32.Foundation", "NTSTATUS");
35-
pub const WIN32_ERROR: Self = Self("Windows.Win32.Foundation", "WIN32_ERROR");
36-
pub const RPC_STATUS: Self = Self("Windows.Win32.System.Rpc", "RPC_STATUS");
3734
pub const VARIANT: Self = Self("Windows.Win32.System.Variant", "VARIANT");
3835
pub const PROPVARIANT: Self = Self("Windows.Win32.System.Com.StructuredStorage", "PROPVARIANT");
3936

crates/libs/bindgen/src/types/cpp_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ fn is_ansi_encoding(row: Field) -> bool {
150150
fn is_signed_error(ty: &Type) -> bool {
151151
match ty {
152152
Type::HRESULT => true,
153-
Type::CppStruct(ty) => ty.type_name() == TypeName::NTSTATUS,
153+
Type::CppStruct(ty) => !ty.def.underlying_type().is_unsigned(),
154154
_ => false,
155155
}
156156
}

crates/libs/bindgen/src/types/cpp_enum.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,7 @@ impl CppEnum {
108108
}
109109
};
110110

111-
let must_use = if matches!(tn, TypeName::WIN32_ERROR | TypeName::RPC_STATUS) {
112-
quote! { #[must_use] }
113-
} else {
114-
quote! {}
115-
};
116-
117111
quote! {
118-
#must_use
119112
#[repr(transparent)]
120113
#derive
121114
pub struct #name(pub #underlying_type);

crates/libs/core/src/guid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl GUID {
2727
if matches!(result, 0 | imp::RPC_S_UUID_LOCAL_ONLY) {
2828
Ok(guid)
2929
} else {
30-
Err(Error::from_hresult(HRESULT::from_win32(result as u32)))
30+
Err(Error::from_hresult(WIN32_ERROR(result as u32).to_hresult()))
3131
}
3232
}
3333

crates/libs/registry/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ fn win32_error(result: u32) -> Result<()> {
7070
if result == 0 {
7171
Ok(())
7272
} else {
73-
Err(Error::from_hresult(HRESULT::from_win32(result)))
73+
Err(Error::from_hresult(WIN32_ERROR(result).to_hresult()))
7474
}
7575
}
7676

7777
fn invalid_data() -> Error {
78-
Error::from_hresult(HRESULT::from_win32(ERROR_INVALID_DATA))
78+
Error::from_hresult(WIN32_ERROR(ERROR_INVALID_DATA).to_hresult())
7979
}
8080

8181
fn from_le_bytes(ty: Type, from: &[u8]) -> Result<u64> {

crates/libs/result/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Use the `HRESULT`, `Error`, and specialized `Result` types as needed:
1919
use windows_result::*;
2020

2121
const S_OK: HRESULT = HRESULT(0);
22-
const ERROR_CANCELLED: u32 = 1223;
23-
const E_CANCELLED: HRESULT = HRESULT::from_win32(ERROR_CANCELLED);
22+
const ERROR_CANCELLED: WIN32_ERROR = WIN32_ERROR(1223);
23+
const E_CANCELLED: HRESULT = ERROR_CANCELLED.to_hresult();
2424

2525
fn main() -> Result<()> {
2626
S_OK.ok()?;

crates/libs/result/src/error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,27 +176,27 @@ impl From<Error> for std::io::Error {
176176
impl From<std::io::Error> for Error {
177177
fn from(from: std::io::Error) -> Self {
178178
match from.raw_os_error() {
179-
Some(status) => HRESULT::from_win32(status as u32).into(),
179+
Some(status) => WIN32_ERROR(status as u32).into(),
180180
None => HRESULT(E_UNEXPECTED).into(),
181181
}
182182
}
183183
}
184184

185185
impl From<alloc::string::FromUtf16Error> for Error {
186186
fn from(_: alloc::string::FromUtf16Error) -> Self {
187-
Self::from_hresult(HRESULT::from_win32(ERROR_NO_UNICODE_TRANSLATION))
187+
WIN32_ERROR(ERROR_NO_UNICODE_TRANSLATION).into()
188188
}
189189
}
190190

191191
impl From<alloc::string::FromUtf8Error> for Error {
192192
fn from(_: alloc::string::FromUtf8Error) -> Self {
193-
Self::from_hresult(HRESULT::from_win32(ERROR_NO_UNICODE_TRANSLATION))
193+
WIN32_ERROR(ERROR_NO_UNICODE_TRANSLATION).into()
194194
}
195195
}
196196

197197
impl From<core::num::TryFromIntError> for Error {
198198
fn from(_: core::num::TryFromIntError) -> Self {
199-
Self::from_hresult(HRESULT::from_win32(ERROR_INVALID_DATA))
199+
WIN32_ERROR(ERROR_INVALID_DATA).into()
200200
}
201201
}
202202

crates/libs/result/src/hresult.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,32 +116,13 @@ impl HRESULT {
116116
pub fn from_thread() -> Self {
117117
#[cfg(windows)]
118118
{
119-
let error = unsafe { GetLastError() };
120-
Self::from_win32(error)
119+
WIN32_ERROR::from_thread().into()
121120
}
122121
#[cfg(not(windows))]
123122
{
124123
unimplemented!()
125124
}
126125
}
127-
128-
/// Maps a Win32 error code to an HRESULT value.
129-
pub const fn from_win32(error: u32) -> Self {
130-
Self(if error as i32 <= 0 {
131-
error
132-
} else {
133-
(error & 0x0000_FFFF) | (7 << 16) | 0x8000_0000
134-
} as i32)
135-
}
136-
137-
/// Maps an NT error code to an HRESULT value.
138-
pub const fn from_nt(error: i32) -> Self {
139-
Self(if error >= 0 {
140-
error
141-
} else {
142-
error | 0x1000_0000
143-
})
144-
}
145126
}
146127

147128
impl<T> From<Result<T>> for HRESULT {

0 commit comments

Comments
 (0)