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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add SubclassOf trait #271

wants to merge 1 commit into from

Conversation

madsmtm
Copy link
Owner

@madsmtm madsmtm commented Sep 22, 2022

An idea for making a trait to tell whether a specific class is a subclass of another. Basically, the more generic version of the ClassType trait.

The need arises on NSMeasurement, which is defined in the Objective-C header as:

@interface NSMeasurement<UnitType: NSUnit *>

With this we could define the generic in Rust as:

pub struct NSMeasurement<UnitType: SubclassOf<NSUnit>>;

Unfortunately, automatically declaring it doesn't work, since we have no way of ensuring in the type-system, that Super is not recursive:

pub struct NSObject;
unsafe impl ClassType {
    type Super = NSObject;
}

// Now there's multiple impls of `SubclassOf<NSObject>`

So maybe it'd make sense to add it anyways, and then implement it in extern_class! instead? At least if the problem comes up in other places than NSMeasurement (NSUnit is an "abstract class", meaning it really acts more like a protocol than a class, which is why this case is kinda special).

See also #518.

@madsmtm madsmtm added enhancement New feature or request A-objc2 Affects the `objc2`, `objc-sys` and/or `objc2-encode` crates labels Sep 22, 2022
@madsmtm madsmtm changed the title Add SubclassOf (auto?) trait Add SubclassOf trait Jan 27, 2023
@madsmtm madsmtm mentioned this pull request Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-objc2 Affects the `objc2`, `objc-sys` and/or `objc2-encode` crates enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant