Skip to content

Commit

Permalink
Merge pull request #58 from dewert99/const_generics
Browse files Browse the repository at this point in the history
Fix bug when using a delagateable trait with const generics
  • Loading branch information
dewert99 authored Jun 19, 2024
2 parents 936ebd3 + 601bc28 commit 6164831
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ambassador/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn param_to_matcher(param: &GenericParam) -> TokenStream {
let ident = &lifetime.ident;
quote!($ #ident : lifetime,)
}
GenericParam::Const(ConstParam { ident, .. }) => quote!($ #ident : ident,),
GenericParam::Const(ConstParam { ident, .. }) => quote!($ #ident : expr,),
}
}

Expand Down Expand Up @@ -181,7 +181,7 @@ fn make_assoc_ty_bound(
}
}

// Replaces the identifiers in gen_idents with there macro args (X => $X) ('a => $a)
// Replaces the identifiers in gen_idents with their macro args (X => $X) ('a => $a)
fn replace_gen_idents(tokens: TokenStream, gen_idents: &[&Ident]) -> TokenStream {
let mut res = TokenStream::new();
let mut apostrophe: Option<proc_macro2::Punct> = None;
Expand Down Expand Up @@ -217,6 +217,7 @@ fn build_method(
) -> TokenStream {
quote! {
#[inline]
#[allow(unused_braces)]
#extr_attrs
#method_sig {
#method_invocation
Expand Down
31 changes: 31 additions & 0 deletions ambassador/tests/run-pass/const_generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
extern crate ambassador;

use ambassador::{delegatable_trait, Delegate};

#[delegatable_trait]
trait S<const N: usize> {
fn handle(&self, x: [u8; N]);
}

struct Base;

impl<const N: usize> S<N> for Base {
fn handle(&self, x: [u8; N]) {
println!("{x:?}");
}
}

const C: usize = 2;

#[derive(Delegate)]
#[delegate(S<1>)]
#[delegate(S<C>)]
#[delegate(S<{C+1}>)]
struct Wrap(Base);

fn main() {
let w = Wrap(Base);
w.handle([1]);
w.handle([1, 2]);
w.handle([1, 2, 3]);
}

0 comments on commit 6164831

Please sign in to comment.