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

Consolidate Param/Ref/InterfaceRef etc #3233

Open
kennykerr opened this issue Aug 29, 2024 · 2 comments · Fixed by #3386
Open

Consolidate Param/Ref/InterfaceRef etc #3233

kennykerr opened this issue Aug 29, 2024 · 2 comments · Fixed by #3386
Labels
enhancement New feature or request

Comments

@kennykerr
Copy link
Collaborator

kennykerr commented Aug 29, 2024

A number of conversion types and traits have been developed to solve different problems. Some like Param are used by windows-bindgen while others are more suitable for hand-authored APIs. It's just a bit confusing and hard to know what the right tool is where there are so many options.

@kennykerr kennykerr added the enhancement New feature or request label Aug 29, 2024
@kennykerr kennykerr changed the title Consolidate Param/Ref/InterfaceRef Consolidate Param/Ref/InterfaceRef etc Aug 29, 2024
@kennykerr
Copy link
Collaborator Author

kennykerr commented Dec 11, 2024

We should try and use Ref for interface traits and delegate closures. Its way simpler than the use of Option<&T> or &Option<T>.

Particularly, the difference in the closure signature between TypedEventHandler<T, A> and EventHandler<T> is quite confusing.

@kennykerr
Copy link
Collaborator Author

Ideally we could have an AsRef that worked something like this:

pub trait AsRef<T> where T: Type<T> {
    fn as_ref<'a>(&'a self) -> Ref<'a, T>;
}

impl<T: Type<T>> AsRef<T> for T {
    fn as_ref<'a>(&'a self) -> Ref<'a, T> {
        unsafe { transmute_copy(self) }
    }
}

impl<'a, T: Type<T>> AsRef<T> for &'a T {
    fn as_ref(&self) -> Ref<'a, T> {
        unsafe { transmute_copy(self) }
    }
}

impl<'a, T: Type<T>> AsRef<T> for Option<&'a T> {
    fn as_ref(&self) -> Ref<'a, T> {
        unsafe {
            match self {
                Some(value) => transmute_copy(value),
                None => Ref::zeroed(),
            }
        }
    }
}

This would then provide more uniform parameter binding across regular and generic parameters and be far simpler in general.

As noted in #3394 this doesn't support polymorphism the way that Param does though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant