From f31d87931515e6e7728603a83136c0541179c345 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 12 Jan 2025 04:49:10 +0100 Subject: [PATCH] Convert Boolean and boolean_t to bool in functions --- crates/header-translator/src/rust_type.rs | 19 +++++-- crates/header-translator/src/stmt.rs | 8 +-- .../objc2-core-foundation/src/base.rs | 2 +- .../objc2-core-foundation/src/number.rs | 6 +-- .../objc2-core-foundation/src/string.rs | 52 ++++++++----------- generated | 2 +- 6 files changed, 47 insertions(+), 42 deletions(-) diff --git a/crates/header-translator/src/rust_type.rs b/crates/header-translator/src/rust_type.rs index 25c20e2c1..d6f79135e 100644 --- a/crates/header-translator/src/rust_type.rs +++ b/crates/header-translator/src/rust_type.rs @@ -1898,6 +1898,9 @@ impl Ty { match self { _ if self.is_objc_bool() => Some((" -> bool".to_string(), "", ".as_bool()")), + Self::TypeDef { id, .. } if matches!(&*id.name, "Boolean" | "boolean_t") => { + Some((" -> bool".to_string(), start, ";\nret != 0")) + } Self::Pointer { nullability, lifetime: Lifetime::Unspecified, @@ -2033,12 +2036,18 @@ impl Ty { pub(crate) fn fn_argument_converter( &self, - ) -> Option<(impl fmt::Display + '_, impl fmt::Display + '_)> { - if self.is_objc_bool() { - Some(("bool", "Bool::new")) - } else { + ) -> Option<( + impl fmt::Display + '_, + impl fmt::Display + '_, + impl fmt::Display + '_, + )> { + match self { + _ if self.is_objc_bool() => Some(("bool", "Bool::new(", ")")), + Self::TypeDef { id, .. } if matches!(&*id.name, "Boolean" | "boolean_t") => { + Some(("bool", "", " as _")) + } // TODO: Support out / autoreleasing pointers? - None + _ => None, } } diff --git a/crates/header-translator/src/stmt.rs b/crates/header-translator/src/stmt.rs index f3d0eed6b..3c80797c6 100644 --- a/crates/header-translator/src/stmt.rs +++ b/crates/header-translator/src/stmt.rs @@ -2749,7 +2749,7 @@ impl Stmt { for (param, arg_ty) in arguments { let param = handle_reserved(&crate::to_snake_case(param)); write!(f, "{param}: ")?; - if let Some((converted_ty, _)) = arg_ty.fn_argument_converter() { + if let Some((converted_ty, _, _)) = arg_ty.fn_argument_converter() { write!(f, "{converted_ty}")?; } else { write!(f, "{}", arg_ty.fn_argument())?; @@ -2777,8 +2777,10 @@ impl Stmt { write!(f, "unsafe {{ {}(", id.name)?; for (param, ty) in arguments { let param = handle_reserved(&crate::to_snake_case(param)); - if let Some((_, converter)) = ty.fn_argument_converter() { - write!(f, "{converter}({param})")?; + if let Some((_, converter_start, converter_end)) = + ty.fn_argument_converter() + { + write!(f, "{converter_start}{param}{converter_end}")?; } else { write!(f, "{param}")?; } diff --git a/framework-crates/objc2-core-foundation/src/base.rs b/framework-crates/objc2-core-foundation/src/base.rs index 83fd7a32c..e36ebf3c6 100644 --- a/framework-crates/objc2-core-foundation/src/base.rs +++ b/framework-crates/objc2-core-foundation/src/base.rs @@ -141,7 +141,7 @@ impl PartialEq for CFType { #[inline] #[doc(alias = "CFEqual")] fn eq(&self, other: &Self) -> bool { - CFEqual(Some(self), Some(other)) != 0 + CFEqual(Some(self), Some(other)) } } diff --git a/framework-crates/objc2-core-foundation/src/number.rs b/framework-crates/objc2-core-foundation/src/number.rs index 81f69a62e..50bd14c70 100644 --- a/framework-crates/objc2-core-foundation/src/number.rs +++ b/framework-crates/objc2-core-foundation/src/number.rs @@ -17,7 +17,7 @@ impl CFBoolean { } pub fn as_bool(&self) -> bool { - unsafe { CFBooleanGetValue(self) != 0 } + unsafe { CFBooleanGetValue(self) } } } @@ -70,9 +70,9 @@ macro_rules! def_get_fn { #[inline] pub fn $fn_name(&self) -> Option<$fn_ret> { let mut value: $fn_ret = <$fn_ret>::default(); - let ptr: *mut$fn_ret = &mut value; + let ptr: *mut $fn_ret = &mut value; let ret = unsafe { CFNumberGetValue(self, CFNumberType::$type, ptr.cast()) }; - if ret != 0 { + if ret { Some(value) } else { None diff --git a/framework-crates/objc2-core-foundation/src/string.rs b/framework-crates/objc2-core-foundation/src/string.rs index 3ff517bc5..902b94769 100644 --- a/framework-crates/objc2-core-foundation/src/string.rs +++ b/framework-crates/objc2-core-foundation/src/string.rs @@ -6,9 +6,9 @@ use core::ptr::NonNull; use core::{fmt, str}; use crate::{ - kCFAllocatorNull, Boolean, CFRange, CFRetained, CFString, CFStringBuiltInEncodings, - CFStringCompare, CFStringCompareFlags, CFStringCreateWithBytes, CFStringCreateWithBytesNoCopy, - CFStringGetBytes, CFStringGetCStringPtr, CFStringGetLength, + kCFAllocatorNull, CFRange, CFRetained, CFString, CFStringBuiltInEncodings, CFStringCompare, + CFStringCompareFlags, CFStringCreateWithBytes, CFStringCreateWithBytesNoCopy, CFStringGetBytes, + CFStringGetCStringPtr, CFStringGetLength, }; #[track_caller] @@ -38,7 +38,7 @@ impl CFString { string.as_ptr(), len, CFStringBuiltInEncodings::EncodingUTF8.0, - false as Boolean, + false, ) }; // Should only fail if the string is not UTF-u (which we know it is) @@ -64,7 +64,7 @@ impl CFString { string.as_ptr(), len, CFStringBuiltInEncodings::EncodingUTF8.0, - false as Boolean, + false, kCFAllocatorNull, ) }; @@ -142,7 +142,7 @@ impl fmt::Display for CFString { }, CFStringBuiltInEncodings::EncodingUTF8.0, 0, // No conversion character - false as Boolean, + false, buf.as_mut_ptr(), buf.len() as _, &mut read_utf8, @@ -242,7 +242,7 @@ mod tests { b"\xd8\x3d\xde".as_ptr(), 3, CFStringBuiltInEncodings::EncodingUTF16BE.0, - 0, + false, ) .unwrap() }; @@ -258,17 +258,14 @@ mod tests { // Test `CFStringGetCString`. let mut buf = [0u8; 10]; - assert_ne!( - unsafe { - CFStringGetCString( - &s, - buf.as_mut_ptr().cast(), - buf.len() as _, - CFStringBuiltInEncodings::EncodingUTF8.0, - ) - }, - false as _, - ); + assert!(unsafe { + CFStringGetCString( + &s, + buf.as_mut_ptr().cast(), + buf.len() as _, + CFStringBuiltInEncodings::EncodingUTF8.0, + ) + }); // All the data is copied to the buffer. assert_eq!(&buf[0..10], b"a\0b\0c\0d\0\0\0"); @@ -307,17 +304,14 @@ mod tests { // So does `CFStringGetCString`. let mut buf = [0u8; 20]; - assert_ne!( - unsafe { - CFStringGetCString( - &s, - buf.as_mut_ptr().cast(), - buf.len() as _, - CFStringBuiltInEncodings::EncodingUTF8.0, - ) - }, - false as _, - ); + assert!(unsafe { + CFStringGetCString( + &s, + buf.as_mut_ptr().cast(), + buf.len() as _, + CFStringBuiltInEncodings::EncodingUTF8.0, + ) + }); let cstr = CStr::from_bytes_until_nul(&buf).unwrap(); assert_eq!(cstr.to_bytes(), "♥".as_bytes()); diff --git a/generated b/generated index eb674b3c2..83a760be6 160000 --- a/generated +++ b/generated @@ -1 +1 @@ -Subproject commit eb674b3c25acc672ca8f6f07d36cdad351b34367 +Subproject commit 83a760be6596dcbe322e1fd4ddf4d73b7f6366e0