From 8383eb11b0b21da0ff23c950658ab246e47f7785 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 22 Sep 2022 23:22:13 +0200 Subject: [PATCH] Add SubclassOf --- crates/objc2/src/class_type.rs | 29 +++++++++++++++++++++++++++++ crates/objc2/src/lib.rs | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/objc2/src/class_type.rs b/crates/objc2/src/class_type.rs index 42741f733..d715ff2cc 100644 --- a/crates/objc2/src/class_type.rs +++ b/crates/objc2/src/class_type.rs @@ -109,3 +109,32 @@ pub unsafe trait ClassType: Message { unsafe { msg_send_id![Self::class(), alloc] } } } + +/// TODO +pub unsafe trait SubclassOf {} + +unsafe impl SubclassOf for T {} + +unsafe impl SubclassOf for T where T::Super: ClassType {} + +unsafe impl SubclassOf<::Super> for T +where + T::Super: ClassType, + ::Super: ClassType, +{ +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::foundation::{NSMutableString, NSObject, NSString}; + + fn assert_subclass_of, C: ClassType>() {} + + #[test] + fn test_subclass_of() { + assert_subclass_of::(); + assert_subclass_of::(); + assert_subclass_of::(); + } +} diff --git a/crates/objc2/src/lib.rs b/crates/objc2/src/lib.rs index a49ee6664..8603eb234 100644 --- a/crates/objc2/src/lib.rs +++ b/crates/objc2/src/lib.rs @@ -195,7 +195,7 @@ pub use objc_sys as ffi; #[doc(no_inline)] pub use objc2_encode::{Encode, EncodeArguments, Encoding, RefEncode}; -pub use crate::class_type::ClassType; +pub use crate::class_type::{ClassType, SubclassOf}; pub use crate::message::{Message, MessageArguments, MessageReceiver}; pub use crate::protocol::{ConformsTo, ProtocolType}; pub use crate::verify::VerificationError;