Skip to content

Commit

Permalink
Add NSObjectProtocol::is_member_of and some related "downcasting" hel…
Browse files Browse the repository at this point in the history
…pers
  • Loading branch information
cynecx committed Jul 26, 2023
1 parent 6849481 commit 65c7182
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
24 changes: 23 additions & 1 deletion crates/objc2/src/rc/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ use core::panic::{RefUnwindSafe, UnwindSafe};
use core::ptr::{self, NonNull};

use super::AutoreleasePool;
use crate::mutability::{IsIdCloneable, IsMutable};
use crate::{ffi, ClassType, Message};
use crate::{
mutability::{IsIdCloneable, IsMutable},
runtime::NSObjectProtocol,
};

/// A reference counted pointer type for Objective-C objects.
///
Expand Down Expand Up @@ -602,6 +605,25 @@ where
}
}

impl<T: ClassType + NSObjectProtocol + 'static> Id<T> {
/// Tries to cast the object to the class `C` by checking whether the object
/// is a direct member of the class (through [`NSObjectProtocol::is_member_of`]).
pub fn member_cast<C>(this: Self) -> Result<Id<C>, Self>
where
C: ClassType + NSObjectProtocol + 'static,
{
if this.is_member_of::<C>() {
Ok(unsafe {
// SAFETY: This cast should be safe because the object is a direct member
// of class `C`.
Id::cast(this)
})
} else {
Err(this)
}
}
}

// TODO: Add ?Sized bound
impl<T: IsIdCloneable> Clone for Id<T> {
/// Makes a clone of the shared object.
Expand Down
38 changes: 38 additions & 0 deletions crates/objc2/src/runtime/nsobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ pub unsafe trait NSObjectProtocol {
unsafe { msg_send![self, isKindOfClass: cls] }
}

#[doc(hidden)]
fn __isMemberOfClass(&self, cls: &AnyClass) -> bool
where
Self: Sized + Message,
{
unsafe { msg_send![self, isMemberOfClass: cls] }
}

/// Check if the object is an instance of the class, or one of it's
/// subclasses.
///
Expand All @@ -155,6 +163,36 @@ pub unsafe trait NSObjectProtocol {
// For example, something may have a return type of `NSString`, while
// behind the scenes they really return `NSMutableString` and expect it to
// not be modified.

/// Checks if the object is a member of the class.
///
/// See [Apple's documentation][apple-doc] for more details on what you
/// may (and what you may not) do with this information.
///
/// [apple-doc]: https://developer.apple.com/documentation/objectivec/1418956-nsobject/1418766-ismemberofclass
#[doc(alias = "isMemberOfClass")]
fn is_member_of<T: ClassType>(&self) -> bool
where
Self: Sized + Message,
{
self.__isMemberOfClass(T::class())
}

/// Tries to cast the object to the class `T` by checking whether the object
/// is a direct member of the class (through [`is_member_of`](`Self::is_member_of`)).
fn member_cast<T: ClassType>(&self) -> Option<&T>
where
Self: Sized + Message + 'static,
T: 'static,
{
if self.is_member_of::<T>() {
// SAFETY: self is a direct member of the specified (`T`) class.
// Therefore casting should be safe.
Some(unsafe { &*(self as *const Self).cast::<T>() })
} else {
None
}
}
}

crate::__inner_extern_protocol!(
Expand Down

0 comments on commit 65c7182

Please sign in to comment.