Skip to content

Commit

Permalink
Make BOOL a core type (#3441)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jan 14, 2025
1 parent b9c8b43 commit 94e56c9
Show file tree
Hide file tree
Showing 409 changed files with 20,398 additions and 20,103 deletions.
13 changes: 5 additions & 8 deletions crates/libs/bindgen/src/types/cpp_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ impl CppMethod {
|| param.has_attribute("ReservedAttribute"))
{
*hint = ParamHint::Optional;
} else if !flags.contains(ParamAttributes::Out) && ty.type_name() == TypeName::BOOL
{
} else if !flags.contains(ParamAttributes::Out) && *ty == Type::BOOL {
*hint = ParamHint::Bool;
} else if ty.is_primitive() && (!ty.is_pointer() || ty.deref().is_copyable()) {
*hint = ParamHint::ValueType;
Expand Down Expand Up @@ -206,12 +205,10 @@ impl CppMethod {
}
}
}
Type::CppStruct(ty)
if TypeName(ty.def.namespace(), ty.def.name()) == TypeName::BOOL
&& last_error =>
{
// TODO: maybe use ResultBool here to make the code gen less ambiguous
return_hint = ReturnHint::ResultVoid
Type::BOOL => {
if last_error {
return_hint = ReturnHint::ResultVoid
}
}
Type::GUID => return_hint = ReturnHint::ReturnStruct,
Type::CppStruct(ty) if !ty.is_handle() => return_hint = ReturnHint::ReturnStruct,
Expand Down
11 changes: 11 additions & 0 deletions crates/libs/bindgen/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub enum Type {
HRESULT,
IUnknown,
BSTR,
BOOL,
}

impl Ord for Type {
Expand Down Expand Up @@ -167,6 +168,7 @@ impl Type {
TypeName::IInspectable => Remap::Type(Self::Object),
TypeName::CHAR => Remap::Type(Self::I8),
TypeName::BOOLEAN => Remap::Type(Self::Bool),
TypeName::BOOL => Remap::Type(Self::BOOL),
TypeName::IUnknown => Remap::Type(Self::IUnknown),
TypeName::Type => Remap::Type(Self::Type),
TypeName::EventRegistrationToken => Remap::Type(Type::I64),
Expand Down Expand Up @@ -398,6 +400,10 @@ impl Type {
let name = writer.write_core();
quote! { #name HRESULT }
}
Self::BOOL => {
let name = writer.write_core();
quote! { #name BOOL }
}
Self::String => {
let name = writer.write_core();
quote! { #name HSTRING }
Expand Down Expand Up @@ -764,6 +770,7 @@ impl Type {
| Self::ISize
| Self::USize
| Self::HRESULT
| Self::BOOL
| Self::PtrConst(_, _)
| Self::PtrMut(_, _) => true,
_ => false,
Expand Down Expand Up @@ -848,6 +855,7 @@ impl Type {
Self::Enum(ty) => ty.def.underlying_type(),
Self::CppStruct(ty) => ty.def.underlying_type(),
Self::HRESULT => Type::I32,
Self::BOOL => Type::I32,
_ => self.clone(),
}
}
Expand All @@ -861,6 +869,7 @@ impl Type {

match self {
Self::HRESULT => quote! { pub type HRESULT = i32; },
Self::BOOL => quote! { pub type BOOL = i32; },

Self::PWSTR => quote! { pub type PWSTR = *mut u16; },
Self::PCSTR => quote! { pub type PCSTR = *const u8; },
Expand Down Expand Up @@ -955,6 +964,7 @@ impl Type {
Self::PCWSTR => TypeName("", "PCWSTR"),
Self::GUID => TypeName("", "GUID"),
Self::HRESULT => TypeName("", "HRESULT"),
Self::BOOL => TypeName("", "BOOL"),
Self::IUnknown => TypeName("", "IUnknown"),
Self::BSTR => TypeName("", "BSTR"),
Self::String => TypeName("", "String"),
Expand All @@ -973,6 +983,7 @@ impl Type {
| Self::PCWSTR
| Self::GUID
| Self::HRESULT
| Self::BOOL
| Self::IUnknown
| Self::Object
| Self::BSTR
Expand Down
5 changes: 1 addition & 4 deletions crates/libs/bindgen/src/writer/cpp_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ impl Writer {
quote! {}
};

let must_use = if matches!(
tn,
TypeName::BOOL | TypeName::NTSTATUS | TypeName::RPC_STATUS
) {
let must_use = if matches!(tn, TypeName::NTSTATUS | TypeName::RPC_STATUS) {
quote! { #[must_use] }
} else {
quote! {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,55 @@
use crate::Win32::Foundation::BOOL;
use super::*;

/// A 32-bit value representing boolean values and returned by some functions to indicate success or failure.
#[must_use]
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct BOOL(pub i32);

impl BOOL {
/// Converts the [`BOOL`] to a [`prim@bool`] value.
#[inline]
pub fn as_bool(self) -> bool {
self.0 != 0
}

/// Converts the [`BOOL`] to [`Result<()>`][Result<_>].
#[inline]
pub fn ok(self) -> windows_core::Result<()> {
pub fn ok(self) -> Result<()> {
if self.as_bool() {
Ok(())
} else {
Err(windows_core::Error::from_win32())
Err(Error::from_win32())
}
}

/// Asserts that `self` is a success code.
#[inline]
#[track_caller]
pub fn unwrap(self) {
self.ok().unwrap();
}

/// Asserts that `self` is a success code using the given panic message.
#[inline]
#[track_caller]
pub fn expect(self, msg: &str) {
self.ok().expect(msg);
}
}

impl From<BOOL> for bool {
fn from(value: BOOL) -> Self {
value.as_bool()
}
}

impl From<&BOOL> for bool {
fn from(value: &BOOL) -> Self {
value.as_bool()
}
}

impl From<bool> for BOOL {
fn from(value: bool) -> Self {
if value {
Expand All @@ -43,21 +59,25 @@ impl From<bool> for BOOL {
}
}
}

impl From<&bool> for BOOL {
fn from(value: &bool) -> Self {
(*value).into()
}
}

impl PartialEq<bool> for BOOL {
fn eq(&self, other: &bool) -> bool {
self.as_bool() == *other
}
}

impl PartialEq<BOOL> for bool {
fn eq(&self, other: &BOOL) -> bool {
*self == other.as_bool()
}
}

impl core::ops::Not for BOOL {
type Output = Self;
fn not(self) -> Self::Output {
Expand Down
3 changes: 3 additions & 0 deletions crates/libs/result/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ pub use error::*;
mod hresult;
pub use hresult::HRESULT;

mod bool;
pub use bool::BOOL;

/// A specialized [`Result`] type that provides Windows error information.
pub type Result<T> = core::result::Result<T, Error>;
Loading

0 comments on commit 94e56c9

Please sign in to comment.