Skip to content

Commit

Permalink
Accept mut prefix before argument names
Browse files Browse the repository at this point in the history
  • Loading branch information
ozars committed Apr 13, 2024
1 parent f7d81a0 commit 1188cb3
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ impl ToTokens for FnArgName {
/// Function argument with name and type.
#[derive(Debug, Eq, PartialEq, Clone)]
struct FnArg {
r#mut: Option<Token![mut]>,
name: FnArgName,
ty: Type,
}

impl Parse for FnArg {
fn parse(input: ParseStream) -> Result<Self> {
let r#mut = input.parse()?;
let name = input.parse()?;
let _ = input.parse::<Token![:]>()?;
let ty = input.parse()?;
Ok(Self { name, ty })
Ok(Self { r#mut, name, ty })
}
}

Expand Down Expand Up @@ -185,6 +187,7 @@ fn generate_trait_implementation(
trait_name: &Ident,
generic_params: Option<&Punctuated<GenericParam, Token![,]>>,
FnArg {
r#mut: input_expr_mut,
name: input_expr_name,
ty: input_expr_type,
}: &FnArg,
Expand All @@ -195,7 +198,7 @@ fn generate_trait_implementation(
let generics = generic_params.map(|g| quote! {<#g>});
quote! {
impl #generics #trait_name<#input_expr_type> for #input_expr_type {
#default fn dispatch(#input_expr_name: #input_expr_type #(, #extra_args)*) -> #return_type {
#default fn dispatch(#input_expr_mut #input_expr_name: #input_expr_type #(, #extra_args)*) -> #return_type {
#body
}
}
Expand Down Expand Up @@ -407,4 +410,17 @@ mod tests {
}
);
}

#[test]
fn parse_mut_arg() {
let arg: FnArg = parse_quote!(mut v: u8);
assert_eq!(
arg,
FnArg {
r#mut: Some(parse_quote!(mut)),
ty: parse_quote!(u8),
name: FnArgName::Ident(parse_quote!(v)),
}
);
}
}

0 comments on commit 1188cb3

Please sign in to comment.