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

Accept mut prefix before argument names #2

Merged
merged 1 commit into from
Apr 13, 2024
Merged
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
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)),
}
);
}
}
Loading