-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #474 from cynecx/member-cast
Add `AnyObject::downcast_ref` and `Retained::downcast`
- Loading branch information
Showing
24 changed files
with
410 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::ClassType; | ||
|
||
/// Classes that can be safely downcasted to. | ||
/// | ||
/// [`DowncastTarget`] is an unsafe marker trait that can be implemented on | ||
/// types that also implement [`ClassType`]. | ||
/// | ||
/// Ideally, every type that implements `ClassType` would also be a valid | ||
/// downcast target, however this would be unsound when used with generics, | ||
/// because we can only trivially decide whether the "base container" is an | ||
/// instance of some class type, but anything related to the generic arguments | ||
/// is unknown. | ||
/// | ||
/// This trait is implemented automatically by the [`extern_class!`] and | ||
/// [`declare_class!`] macros. | ||
/// | ||
/// [`extern_class!`]: crate::extern_class | ||
/// [`declare_class!`]: crate::declare_class | ||
/// | ||
/// | ||
/// # Safety | ||
/// | ||
/// The type must not have any generic arguments other than [`AnyObject`]. | ||
/// | ||
/// [`AnyObject`]: crate::runtime::AnyObject | ||
/// | ||
/// | ||
/// # Examples | ||
/// | ||
/// Implementing [`DowncastTarget`] for `NSString`: | ||
/// | ||
/// ```ignore | ||
/// // SAFETY: NSString does not have any generic parameters. | ||
/// unsafe impl DowncastTarget for NSString {} | ||
/// ``` | ||
/// | ||
/// However, implementing it for `NSArray` can only be done when the object | ||
/// type is `AnyObject`. | ||
/// | ||
/// ```ignore | ||
/// // SAFETY: NSArray does not have any generic parameters set (the generic | ||
/// // defaults to `AnyObject`). | ||
/// unsafe impl DowncastTarget for NSArray {} | ||
/// | ||
/// // This would not be valid, since downcasting can only trivially determine | ||
/// // whether the base class (in this case `NSArray`) matches the receiver | ||
/// // class type. | ||
/// // unsafe impl<T: Message> DowncastTarget for NSArray<T> {} | ||
/// ``` | ||
pub unsafe trait DowncastTarget: ClassType + 'static {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.