Skip to content

Commit

Permalink
Convert Boolean and boolean_t to bool in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jan 12, 2025
1 parent d4a7c9f commit f31d879
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
19 changes: 14 additions & 5 deletions crates/header-translator/src/rust_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
}

Expand Down
8 changes: 5 additions & 3 deletions crates/header-translator/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
Expand Down Expand Up @@ -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}")?;
}
Expand Down
2 changes: 1 addition & 1 deletion framework-crates/objc2-core-foundation/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
6 changes: 3 additions & 3 deletions framework-crates/objc2-core-foundation/src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl CFBoolean {
}

pub fn as_bool(&self) -> bool {
unsafe { CFBooleanGetValue(self) != 0 }
unsafe { CFBooleanGetValue(self) }
}
}

Expand Down Expand Up @@ -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
Expand Down
52 changes: 23 additions & 29 deletions framework-crates/objc2-core-foundation/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -64,7 +64,7 @@ impl CFString {
string.as_ptr(),
len,
CFStringBuiltInEncodings::EncodingUTF8.0,
false as Boolean,
false,
kCFAllocatorNull,
)
};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -242,7 +242,7 @@ mod tests {
b"\xd8\x3d\xde".as_ptr(),
3,
CFStringBuiltInEncodings::EncodingUTF16BE.0,
0,
false,
)
.unwrap()
};
Expand All @@ -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");

Expand Down Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion generated
Submodule generated updated 71 files
+21 −13 AudioToolbox/AudioComponent.rs
+271 −169 AudioToolbox/AudioFile.rs
+59 −45 AudioToolbox/AudioQueue.rs
+40 −29 AudioToolbox/AudioSession.rs
+31 −23 AudioToolbox/ExtendedAudioFile.rs
+262 −160 AudioToolbox/mod.rs
+37 −28 CoreFoundation/CFArray.rs
+20 −9 CoreFoundation/CFAttributedString.rs
+22 −8 CoreFoundation/CFBag.rs
+3 −2 CoreFoundation/CFBase.rs
+48 −32 CoreFoundation/CFBinaryHeap.rs
+12 −3 CoreFoundation/CFBitVector.rs
+78 −28 CoreFoundation/CFBundle.rs
+20 −9 CoreFoundation/CFCalendar.rs
+82 −50 CoreFoundation/CFCharacterSet.rs
+12 −4 CoreFoundation/CFDate.rs
+18 −8 CoreFoundation/CFDateFormatter.rs
+86 −61 CoreFoundation/CFDictionary.rs
+11 −5 CoreFoundation/CFFileDescriptor.rs
+136 −48 CoreFoundation/CFFileSecurity.rs
+7 −2 CoreFoundation/CFMachPort.rs
+25 −7 CoreFoundation/CFMessagePort.rs
+27 −9 CoreFoundation/CFNotificationCenter.rs
+30 −11 CoreFoundation/CFNumber.rs
+44 −16 CoreFoundation/CFNumberFormatter.rs
+98 −41 CoreFoundation/CFPlugIn.rs
+53 −20 CoreFoundation/CFPreferences.rs
+11 −3 CoreFoundation/CFPropertyList.rs
+113 −46 CoreFoundation/CFRunLoop.rs
+57 −43 CoreFoundation/CFSet.rs
+7 −2 CoreFoundation/CFSocket.rs
+96 −38 CoreFoundation/CFStream.rs
+264 −120 CoreFoundation/CFString.rs
+13 −5 CoreFoundation/CFTimeZone.rs
+146 −65 CoreFoundation/CFURL.rs
+68 −30 CoreFoundation/CFURLAccess.rs
+10 −3 CoreFoundation/CFURLEnumerator.rs
+8 −3 CoreFoundation/CFXMLParser.rs
+9 −4 CoreGraphics/CGDirectDisplay.rs
+116 −46 CoreGraphics/CGDisplayConfiguration.rs
+45 −16 CoreGraphics/CGDisplayFade.rs
+57 −27 CoreGraphics/CGRemoteOperation.rs
+48 −34 CoreMedia/CMBlockBuffer.rs
+52 −29 CoreMedia/CMBufferQueue.rs
+182 −95 CoreMedia/CMFormatDescription.rs
+25 −12 CoreMedia/CMFormatDescriptionBridge.rs
+54 −30 CoreMedia/CMMetadata.rs
+470 −290 CoreMedia/CMSampleBuffer.rs
+25 −12 CoreMedia/CMSync.rs
+64 −39 CoreMedia/CMTag.rs
+106 −64 CoreMedia/CMTagCollection.rs
+24 −14 CoreMedia/CMTaggedBufferGroup.rs
+68 −50 CoreMedia/CMTimeRange.rs
+14 −9 CoreVideo/CVBuffer.rs
+15 −10 CoreVideo/CVDisplayLink.rs
+13 −8 CoreVideo/CVImageBuffer.rs
+13 −8 CoreVideo/CVMetalTexture.rs
+14 −9 CoreVideo/CVOpenGLTexture.rs
+13 −8 CoreVideo/CVPixelBuffer.rs
+14 −7 CoreVideo/CVPixelFormatDescription.rs
+14 −4 IOSurface/IOSurfaceRef.rs
+55 −40 SystemConfiguration/CaptiveNetwork.rs
+29 −20 SystemConfiguration/DHCPClientPreferences.rs
+175 −112 SystemConfiguration/SCDynamicStore.rs
+103 −81 SystemConfiguration/SCNetwork.rs
+651 −407 SystemConfiguration/SCNetworkConfiguration.rs
+178 −130 SystemConfiguration/SCNetworkConnection.rs
+116 −80 SystemConfiguration/SCNetworkReachability.rs
+246 −173 SystemConfiguration/SCPreferences.rs
+71 −45 SystemConfiguration/SCPreferencesPath.rs
+57 −40 SystemConfiguration/SCPreferencesSetSpecific.rs

0 comments on commit f31d879

Please sign in to comment.