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 safe upcasting #518

Open
madsmtm opened this issue Sep 22, 2023 · 5 comments
Open

Add safe upcasting #518

madsmtm opened this issue Sep 22, 2023 · 5 comments
Labels
A-framework Affects the framework crates and the translator for them A-objc2 Affects the `objc2`, `objc-sys` and/or `objc2-encode` crates enhancement New feature or request

Comments

@madsmtm
Copy link
Owner

madsmtm commented Sep 22, 2023

We already have the ability to convert from NSView to its superclasses NSResponder using Deref, ClassType::as_super or Id::into_super. In general, we should maybe consider an UpCast trait of some sort, to be able to do this fully generically? Even better if it could integrate with a From implementation for Id.

It'd also be nice to have similar conversions from NSArray<NSView> to NSArray<NSResponder>.

Note that this is only valid for __covariant generic types, e.g. NSArray, but not NSMutableArray (see also this video from WWDC2015, timestamp around 23:40).

See also the inverse of this in #474.

@madsmtm madsmtm added enhancement New feature or request A-framework Affects the framework crates and the translator for them labels Sep 22, 2023
@madsmtm madsmtm added this to the Polish icrate milestone Sep 22, 2023
@madsmtm madsmtm changed the title Add safe casting between arrays Add safe casting Jan 13, 2024
@madsmtm madsmtm added the A-objc2 Affects the `objc2`, `objc-sys` and/or `objc2-encode` crates label Jan 13, 2024
@madsmtm madsmtm changed the title Add safe casting Add safe upcasting Jan 13, 2024
@madsmtm
Copy link
Owner Author

madsmtm commented Jan 13, 2024

The trait wasm_bindgen::JsCast shows some of the functionality that people seem to expect from this.

@madsmtm
Copy link
Owner Author

madsmtm commented Jan 13, 2024

Also valuable from wasm_bindgen: They don't implement Borrow, BorrowMut nor AsMut, I've asked about the former here.

@madsmtm
Copy link
Owner Author

madsmtm commented Jan 18, 2024

Tried implementing From for Id, but we can't do that generically as it conflicts with the reflexive From<T> for T; so we'll probably need FromId and IntoId traits :/ (see #48 previously).

Perhaps an inherent method on Id to access such traits could still make sense? Do we want to change Id::into_super to that? Maybe call it Id::upcast, and deprecate Id::into_super?

@madsmtm
Copy link
Owner Author

madsmtm commented Feb 4, 2024

Some sort of UpCast trait (or maybe SubclassOf) could also be used as a bound when implementing external categories. It would allow us to emit something like:

extern_category!(
    unsafe trait MyCategory { ... }
    
    unsafe impl<T: SubclassOf<MyType>> MyCategory for T {}
);

Which would make it such that all subclasses of a given type MyType would now implement MyCategory.

@madsmtm
Copy link
Owner Author

madsmtm commented Feb 12, 2024

Would also like to figure out how Id's and class type's impls of AsRef/Borrow (and mutable variants) should be. See also https://internals.rust-lang.org/t/semantics-of-asref/17016 for discussion around these traits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-framework Affects the framework crates and the translator for them A-objc2 Affects the `objc2`, `objc-sys` and/or `objc2-encode` crates enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant