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

Support generic bounds on implement macro #2984

Open
roblabla opened this issue Apr 11, 2024 · 1 comment
Open

Support generic bounds on implement macro #2984

roblabla opened this issue Apr 11, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@roblabla
Copy link
Contributor

Suggestion

Currently, the implement macro does not support generic bounds. For instance, the following:

trait MyClass {}

#[implement(IClassFactory)]
struct MyClassFactory<T>(PhantomData<T>);

impl<T: MyClass> IClassFactory_Impl for MyClassFactory<T> {
    // ...
}

Will fail to compile with the following error:

error[E0277]: the trait bound `T: MyClass` is not satisfied
    --> src/lib.rs:185:1
     |
185  | #[implement(IClassFactory)]
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `MyClass` is not implemented for `T`, which is required by `MyClassFactory<T>: IClassFactory_Impl`
     |
note: required for `MyClassFactory<T>` to implement `IClassFactory_Impl`
    --> src/lib.rs:188:23
     |
188  | impl<T: MyClass> IClassFactory_Impl for MyClassFactory<T> {
     |         -------  ^^^^^^^^^^^^^^^^^^     ^^^^^^^^^^^^^^^^^
     |         |
     |         unsatisfied trait bound introduced here
note: required by a bound in `IClassFactory_Vtbl::new`
    --> /Users/roblabla/.cargo_isengard/registry/src/index.crates.io-6f17d22bba15001f/windows-0.52.0/src/Windows/Win32/System/Com/impl.rs:1191:81
     |
1191 |     pub const fn new<Identity: ::windows_core::IUnknownImpl<Impl = Impl>, Impl: IClassFactory_Impl, const OFFSET: isize>() -> IClassFactory_Vtbl {
     |                                                                                 ^^^^^^^^^^^^^^^^^^ required by this bound in `IClassFactory_Vtbl::new`
     = note: this error originates in the attribute macro `implement` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter `T`
     |
186  | struct MyClassFactory<T: MyClassProvider>(PhantomData<T>);
     |                          ++++++++++++++

Doing the suggested change (adding the bound on the struct) doesn't help:

error[E0107]: struct takes 1 generic argument but 0 generic arguments were supplied
   --> src/lib.rs:186:8
    |
186 | struct MyClassFactory<T: MyClass>(PhantomData<T>);
    |        ^^^^^^^^^^^^^^^^ expected 1 generic argument
    |
note: struct defined here, with 1 generic parameter: `T`
   --> src/lib.rs:186:8
    |
186 | struct MyClassFactory<T: MyClass>(PhantomData<T>);
    |        ^^^^^^^^^^^^^^^^ -
help: add missing generic argument
    |
185 | #T, [implement(IClassFactory)]
    |  ++

IMO, if a generic bound is found on the struct that implements is on, it should use it for all the generated impl block.


My use-case is to provide a safe wrapper around a COM interface. I'm creating a trait that mirrors the COM interface's trait, but wrapping all the structures in safe wrappers, to hopefully make implementing it easier and safer.

@roblabla roblabla added the enhancement New feature or request label Apr 11, 2024
@kennykerr
Copy link
Collaborator

Keep in mind that the generic support provided by the implement macro is intended to handle WinRT generic interfaces, but I'm certainly happy to expand it to support generic programming in general so I welcome the improvement.

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.

2 participants