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

Closed
madsmtm opened this issue Sep 22, 2023 · 6 comments
Closed

Add safe upcasting #518

madsmtm opened this issue Sep 22, 2023 · 6 comments
Labels
A-framework Affects the framework crates and the translator for them A-objc2 Affects the `objc2`, `objc2-exception-helper` 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.

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`, `objc2-exception-helper` 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.

@madsmtm
Copy link
Owner Author

madsmtm commented Sep 19, 2024

The impl<T: ..., U> From<Retained<U>> for Retained<T> is, as outlined in #518 (comment), not possible. But we can do impl<T, U: AsRef<T>> for From<&U> for Retained<T>, so that's what I've done in 84152d8. Combined with f51c2e3, this should make .into() "just work", albeit with the tiny performance cost of possibly an extra retain/release. In cases where the user doesn't want a retained reference back, .as_ref() is the way to go.

I believe this is nicer than a generic UpCast trait and a .into_bikeshed(), since it means there's fewer traits and methods for users to learn about for a case that will be very common.

.into_super() is also possible now since it's an inherent method, see b94961a, so that should make it easier for people that don't want the perf cost / want to be explicit about the inheritance chain.

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`, `objc2-exception-helper` and/or `objc2-encode` crates enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant