-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add downcasting helper through
isKindOfClass:
- Loading branch information
Showing
8 changed files
with
150 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::ClassType; | ||
|
||
/// [`DowncastTarget`] is an unsafe marker trait that can be implemented on types that also | ||
/// implement [`ClassType`]. This is a promise that the self-type is a valid "downcast target" | ||
/// (What this exactly means is described in the Safety section). | ||
/// | ||
/// # Safety | ||
/// | ||
/// Ideally, every type that implements `ClassType` can 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. | ||
/// | ||
/// In this case: | ||
/// | ||
/// ```ignore | ||
/// let obj: Id<NSObject> = Id::into_super(NSString::new()); | ||
/// // This works and is safe. | ||
/// let obj: &NSString = obj.downcast::<NSString>().unwrap(); | ||
/// ``` | ||
/// | ||
/// `NSString` has a [`DowncastTarget`] implementation, which looks like: | ||
/// | ||
/// ```ignore | ||
/// // This is safe. | ||
/// unsafe impl Downcastable for NSString { } | ||
/// ``` | ||
/// | ||
/// However with generics: | ||
/// | ||
/// ```ignore | ||
/// let obj: Id<NSArray<NSString>> = NSArray::new(); | ||
/// // This is invalid and doesn't type check. | ||
/// let obj = obj.downcast::<NSArray<NSData>>(); | ||
/// ``` | ||
/// | ||
/// The example above doesn't work because [`DowncastTarget`] isn't implemented for | ||
/// `for<T> NSArray<T>`. Doing so would be unsound because downcasting can only trivially | ||
/// determine whether the base class (in this case `NSArray`) matches the receiver class type. | ||
/// | ||
pub unsafe trait DowncastTarget: ClassType {} |
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
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