Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make BOOL, BOOLEAN, and NTSTATUS extensions rather than replacements #1995

Merged
merged 6 commits into from Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Expand Up @@ -155,6 +155,7 @@ jobs:
cargo clippy -p test_enums &&
cargo clippy -p test_error &&
cargo clippy -p test_event &&
cargo clippy -p test_extensions &&
cargo clippy -p test_handles &&
cargo clippy -p test_helpers &&
cargo clippy -p test_identity &&
Expand All @@ -169,7 +170,6 @@ jobs:
cargo clippy -p test_metadata &&
cargo clippy -p test_no_use &&
cargo clippy -p test_not_dll &&
cargo clippy -p test_ntstatus &&
cargo clippy -p test_properties &&
cargo clippy -p test_query_signature &&
cargo clippy -p test_return_struct &&
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -139,6 +139,7 @@ jobs:
cargo test --target ${{ matrix.target }} -p test_enums &&
cargo test --target ${{ matrix.target }} -p test_error &&
cargo test --target ${{ matrix.target }} -p test_event &&
cargo test --target ${{ matrix.target }} -p test_extensions &&
cargo test --target ${{ matrix.target }} -p test_handles &&
cargo test --target ${{ matrix.target }} -p test_helpers &&
cargo test --target ${{ matrix.target }} -p test_identity &&
Expand All @@ -153,7 +154,6 @@ jobs:
cargo test --target ${{ matrix.target }} -p test_metadata &&
cargo test --target ${{ matrix.target }} -p test_no_use &&
cargo test --target ${{ matrix.target }} -p test_not_dll &&
cargo test --target ${{ matrix.target }} -p test_ntstatus &&
cargo test --target ${{ matrix.target }} -p test_properties &&
cargo test --target ${{ matrix.target }} -p test_query_signature &&
cargo test --target ${{ matrix.target }} -p test_return_struct &&
Expand Down
Expand Up @@ -2,15 +2,11 @@ use super::*;

pub fn gen() -> TokenStream {
quote! {
#[repr(transparent)]
pub struct BOOL(pub i32);

impl BOOL {
#[inline]
pub fn as_bool(self) -> bool {
self.0 != 0
}

#[inline]
pub fn ok(self) -> ::windows::core::Result<()> {
if self.as_bool() {
Expand All @@ -19,101 +15,60 @@ pub fn gen() -> TokenStream {
Err(::windows::core::Error::from_win32())
}
}

#[inline]
#[track_caller]
pub fn unwrap(self) {
self.ok().unwrap();
}

#[inline]
#[track_caller]
pub fn expect(self, msg: &str) {
self.ok().expect(msg);
}
}

impl ::core::default::Default for BOOL {
fn default() -> Self {
Self(0)
}
}

impl ::core::clone::Clone for BOOL {
fn clone(&self) -> Self {
*self
}
}

impl ::core::marker::Copy for BOOL {}

impl ::core::cmp::PartialEq for BOOL {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}

impl ::core::cmp::Eq for BOOL {}

impl ::core::fmt::Debug for BOOL {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_tuple("BOOL").field(&self.0).finish()
}
}

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

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

impl ::core::convert::From<bool> for BOOL {
fn from(value: bool) -> Self {
if value {
BOOL(1)
Self(1)
} else {
BOOL(0)
Self(0)
}
}
}

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

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

impl ::core::cmp::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 {
if self.as_bool() {
BOOL(0)
Self(0)
} else {
BOOL(1)
Self(1)
}
}
}

unsafe impl ::windows::core::Abi for BOOL {
type Abi = Self;
}
}
}
74 changes: 74 additions & 0 deletions crates/libs/bindgen/src/extensions/boolean.rs
@@ -0,0 +1,74 @@
use super::*;

pub fn gen() -> TokenStream {
quote! {
impl BOOLEAN {
#[inline]
pub fn as_bool(self) -> bool {
self.0 != 0
}
#[inline]
pub fn ok(self) -> ::windows::core::Result<()> {
if self.as_bool() {
Ok(())
} else {
Err(::windows::core::Error::from_win32())
}
}
#[inline]
#[track_caller]
pub fn unwrap(self) {
self.ok().unwrap();
}
#[inline]
#[track_caller]
pub fn expect(self, msg: &str) {
self.ok().expect(msg);
}
}
impl ::core::convert::From<BOOLEAN> for bool {
fn from(value: BOOLEAN) -> Self {
value.as_bool()
}
}
impl ::core::convert::From<&BOOLEAN> for bool {
fn from(value: &BOOLEAN) -> Self {
value.as_bool()
}
}
impl ::core::convert::From<bool> for BOOLEAN {
fn from(value: bool) -> Self {
if value {
Self(1)
} else {
Self(0)
}
}
}
impl ::core::convert::From<&bool> for BOOLEAN {
fn from(value: &bool) -> Self {
(*value).into()
}
}
impl ::core::cmp::PartialEq<bool> for BOOLEAN {
fn eq(&self, other: &bool) -> bool {
self.as_bool() == *other
}
}
impl ::core::cmp::PartialEq<BOOLEAN> for bool {
fn eq(&self, other: &BOOLEAN) -> bool {
*self == other.as_bool()
}
}
impl ::core::ops::Not for BOOLEAN {
type Output = Self;
fn not(self) -> Self::Output {
if self.as_bool() {
Self(0)
} else {
Self(1)
}
}
}
}
}
6 changes: 6 additions & 0 deletions crates/libs/bindgen/src/extensions/mod.rs
@@ -1,8 +1,11 @@
use super::*;
mod bool32;
mod boolean;
mod in6_addr;
mod in_addr;
mod matrix3x2;
mod matrix4x4;
mod ntstatus;
mod sockaddr_in;
mod sockaddr_in6;
mod sockaddr_inet;
Expand All @@ -14,10 +17,13 @@ mod win32_error;

pub fn gen(type_name: TypeName) -> TokenStream {
match type_name {
TypeName::BOOL => bool32::gen(),
TypeName::BOOLEAN => boolean::gen(),
TypeName::IN_ADDR => in_addr::gen(),
TypeName::IN6_ADDR => in6_addr::gen(),
TypeName::Matrix3x2 => matrix3x2::gen(),
TypeName::Matrix4x4 => matrix4x4::gen(),
TypeName::NTSTATUS => ntstatus::gen(),
TypeName::SOCKADDR_IN => sockaddr_in::gen(),
TypeName::SOCKADDR_IN6 => sockaddr_in6::gen(),
TypeName::SOCKADDR_INET => sockaddr_inet::gen(),
Expand Down
Expand Up @@ -2,9 +2,6 @@ use super::*;

pub fn gen() -> TokenStream {
quote! {
#[repr(transparent)]
#[derive(::core::cmp::PartialEq, ::core::cmp::Eq)]
pub struct NTSTATUS(pub i32);
impl NTSTATUS {
#[inline]
pub const fn is_ok(self) -> bool {
Expand Down Expand Up @@ -37,24 +34,5 @@ pub fn gen() -> TokenStream {
Self{ code: value.to_hresult(), info: None }
}
}
impl ::core::default::Default for NTSTATUS {
fn default() -> Self {
Self(0)
}
}
impl ::core::clone::Clone for NTSTATUS {
fn clone(&self) -> Self {
*self
}
}
impl ::core::marker::Copy for NTSTATUS {}
impl ::core::fmt::Debug for NTSTATUS {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.write_fmt(format_args!("NTSTATUS(0x{:08X})", self.0))
}
}
unsafe impl ::windows::core::Abi for NTSTATUS {
type Abi = Self;
}
}
}
1 change: 1 addition & 0 deletions crates/libs/bindgen/src/handles.rs
Expand Up @@ -101,5 +101,6 @@ pub fn gen_win_handle(gen: &Gen, def: TypeDef) -> TokenStream {
});
}

tokens.combine(&extensions::gen(gen.reader.type_def_type_name(def)));
tokens
}
4 changes: 0 additions & 4 deletions crates/libs/bindgen/src/replacements/mod.rs
@@ -1,13 +1,9 @@
use super::*;
mod bool32;
mod bstr;
mod ntstatus;

pub fn gen(type_name: TypeName) -> Option<TokenStream> {
match type_name {
TypeName::BOOL => Some(bool32::gen()),
TypeName::BSTR => Some(bstr::gen()),
TypeName::NTSTATUS => Some(ntstatus::gen()),
_ => None,
}
}
1 change: 1 addition & 0 deletions crates/libs/metadata/src/reader/type_name.rs
Expand Up @@ -37,6 +37,7 @@ impl<'a> TypeName<'a> {
pub const WIN32_ERROR: Self = Self::from_const("Windows.Win32.Foundation", "WIN32_ERROR");
pub const NTSTATUS: Self = Self::from_const("Windows.Win32.Foundation", "NTSTATUS");
pub const BOOL: Self = Self::from_const("Windows.Win32.Foundation", "BOOL");
pub const BOOLEAN: Self = Self::from_const("Windows.Win32.Foundation", "BOOLEAN");
pub const PWSTR: Self = Self::from_const("Windows.Win32.Foundation", "PWSTR");
pub const PSTR: Self = Self::from_const("Windows.Win32.Foundation", "PSTR");
pub const BSTR: Self = Self::from_const("Windows.Win32.Foundation", "BSTR");
Expand Down