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

Forward type parameters #50

Merged
merged 1 commit into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ambassador/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ fn build_method_invocation(
})
.collect();

let method_invocation = quote! { #field_ident.#method_ident(#argument_list) };
let generics = method_sig.generics.split_for_impl().1;
let turbofish = generics.as_turbofish();

let method_invocation = quote! { #field_ident.#method_ident #turbofish(#argument_list) };
method_invocation
}
46 changes: 46 additions & 0 deletions ambassador/tests/run-pass/generic_method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
extern crate ambassador;

use ambassador::{delegatable_trait, Delegate};

trait StaticFn {
fn call() -> bool;
}

struct StaticTrue;
struct StaticFalse;

impl StaticFn for StaticTrue {
fn call() -> bool {
true
}
}

impl StaticFn for StaticFalse {
fn call() -> bool {
false
}
}

#[delegatable_trait]
pub trait StaticCall {
fn call<C: StaticFn>(&self) -> bool;
}

pub struct BaseCaller;

impl StaticCall for BaseCaller {
fn call<C: StaticFn>(&self) -> bool {
C::call()
}
}

#[derive(Delegate)]
#[delegate(StaticCall)]
pub struct WrappedCaller(BaseCaller);

fn main() {
let c = WrappedCaller(BaseCaller);
// Verify that the generic type is correctly passed through without relying on type inference.
assert!(c.call::<StaticTrue>());
assert!(!c.call::<StaticFalse>());
}
Loading