Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SubclassOf trait #271

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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