Skip to content

Commit

Permalink
Add SubclassOf
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Nov 24, 2022
1 parent bf28404 commit 8383eb1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions crates/objc2/src/class_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,32 @@ pub unsafe trait ClassType: Message {
unsafe { msg_send_id![Self::class(), alloc] }
}
}

/// TODO
pub unsafe trait SubclassOf<Super: ClassType> {}

unsafe impl<T> SubclassOf<T> for T {}

unsafe impl<T: ClassType> SubclassOf<T::Super> for T where T::Super: ClassType {}

unsafe impl<T: ClassType> SubclassOf<<T::Super as ClassType>::Super> for T
where
T::Super: ClassType,
<T::Super as ClassType>::Super: ClassType,
{
}

#[cfg(test)]
mod tests {
use super::*;
use crate::foundation::{NSMutableString, NSObject, NSString};

fn assert_subclass_of<T: SubclassOf<C>, C: ClassType>() {}

#[test]
fn test_subclass_of() {
assert_subclass_of::<NSString, NSObject>();
assert_subclass_of::<NSMutableString, NSObject>();
assert_subclass_of::<NSMutableString, NSString>();
}
}
2 changes: 1 addition & 1 deletion crates/objc2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8383eb1

Please sign in to comment.