Skip to content

Commit 6d82a79

Browse files
committed
Add PartialEq, Eq and Hash impls for all CF-like types
By forwarding to CFType's impl.
1 parent 3829588 commit 6d82a79

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

framework-crates/objc2-core-foundation/src/__cf_macro_helpers.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
pub use core::borrow::Borrow;
2+
pub use core::cmp::{Eq, PartialEq};
23
pub use core::convert::AsRef;
34
pub use core::fmt;
5+
pub use core::hash::{Hash, Hasher};
46
pub use core::mem::transmute;
57
pub use core::ops::Deref;
8+
pub use core::primitive::bool;
69
pub use core::stringify;
710
#[cfg(feature = "objc2")]
811
pub use objc2::encode::{Encode, Encoding, RefEncode};

framework-crates/objc2-core-foundation/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl fmt::Debug for CFType {
122122
}
123123
#[cfg(not(feature = "CFString"))]
124124
{
125-
f.debug_struct("<CoreFoundation type>")
125+
f.debug_struct("<CoreFoundation type (enable CFString feature for more info)>")
126126
.finish_non_exhaustive()
127127
}
128128
}

framework-crates/objc2-core-foundation/src/cf_type.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,10 @@ macro_rules! cf_type {
4040
}
4141
}
4242

43-
impl $crate::__cf_macro_helpers::fmt::Debug for $ty {
44-
fn fmt(
45-
&self,
46-
f: &mut $crate::__cf_macro_helpers::fmt::Formatter<'_>,
47-
) -> $crate::__cf_macro_helpers::fmt::Result {
48-
f.debug_struct($crate::__cf_macro_helpers::stringify!($ty))
49-
.finish_non_exhaustive()
50-
}
51-
}
52-
5343
// SAFETY: The type is a CoreFoundation-like type.
5444
unsafe impl $crate::Type for $ty {}
5545

56-
$crate::__cf_type_convert_cf_type!($ty);
46+
$crate::__cf_type_needs_cf_base!($ty);
5747

5848
$crate::__cf_type_superclass!($ty $(: $superclass)?);
5949

@@ -68,7 +58,7 @@ macro_rules! cf_type {
6858
#[cfg(feature = "CFBase")]
6959
#[doc(hidden)]
7060
#[macro_export]
71-
macro_rules! __cf_type_convert_cf_type {
61+
macro_rules! __cf_type_needs_cf_base {
7262
($ty:ty) => {
7363
// Allow converting to CFType.
7464

@@ -85,13 +75,42 @@ macro_rules! __cf_type_convert_cf_type {
8575
self // Through Deref of self or superclass
8676
}
8777
}
78+
79+
impl $crate::__cf_macro_helpers::PartialEq for $ty {
80+
#[inline]
81+
fn eq(&self, other: &Self) -> $crate::__cf_macro_helpers::bool {
82+
let this: &$crate::CFType = self; // Through Deref
83+
let other: &$crate::CFType = other; // Through Deref
84+
$crate::__cf_macro_helpers::PartialEq::eq(this, other)
85+
}
86+
}
87+
88+
impl $crate::__cf_macro_helpers::Eq for $ty {}
89+
90+
impl $crate::__cf_macro_helpers::Hash for $ty {
91+
#[inline]
92+
fn hash<H: $crate::__cf_macro_helpers::Hasher>(&self, state: &mut H) {
93+
let this: &$crate::CFType = self; // Through Deref
94+
$crate::__cf_macro_helpers::Hash::hash(this, state);
95+
}
96+
}
97+
98+
impl $crate::__cf_macro_helpers::fmt::Debug for $ty {
99+
fn fmt(
100+
&self,
101+
f: &mut $crate::__cf_macro_helpers::fmt::Formatter<'_>,
102+
) -> $crate::__cf_macro_helpers::fmt::Result {
103+
let this: &$crate::CFType = self; // Through Deref
104+
$crate::__cf_macro_helpers::fmt::Debug::fmt(this, f)
105+
}
106+
}
88107
};
89108
}
90109

91110
#[cfg(not(feature = "CFBase"))]
92111
#[doc(hidden)]
93112
#[macro_export]
94-
macro_rules! __cf_type_convert_cf_type {
113+
macro_rules! __cf_type_needs_cf_base {
95114
($ty:ty) => {};
96115
}
97116

framework-crates/objc2-core-foundation/src/string.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,5 +303,16 @@ mod tests {
303303
assert_eq!(cf.to_string(), "xyz");
304304
}
305305

306+
#[test]
307+
fn eq() {
308+
assert_eq!(CFString::from_str("abc"), CFString::from_str("abc"));
309+
assert_ne!(CFString::from_str("abc"), CFString::from_str("xyz"));
310+
// Cross-type comparison
311+
assert_ne!(
312+
**CFString::from_str("abc"),
313+
**unsafe { kCFAllocatorNull }.unwrap()
314+
);
315+
}
316+
306317
// TODO: Test mutation while formatting.
307318
}

0 commit comments

Comments
 (0)