Skip to content

Commit

Permalink
Add SubclassOf
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 22, 2022
1 parent 4b7e392 commit da8724a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions objc2/src/class_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,32 @@ pub unsafe trait ClassType: Message {
/// Get a mutable reference to the superclass.
fn as_super_mut(&mut self) -> &mut Self::Super;
}

/// 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 objc2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,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};
#[cfg(feature = "malloc")]
pub use crate::verify::VerificationError;
Expand Down

0 comments on commit da8724a

Please sign in to comment.