Skip to content

Commit 162e49f

Browse files
committed
Generic support for FromArgs
1 parent dc69601 commit 162e49f

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

derive/src/from_args.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ enum ParameterKind {
1616

1717
impl ParameterKind {
1818
fn from_ident(ident: &Ident) -> Option<ParameterKind> {
19-
if ident == "positional_only" {
20-
Some(ParameterKind::PositionalOnly)
21-
} else if ident == "positional_or_keyword" {
22-
Some(ParameterKind::PositionalOrKeyword)
23-
} else if ident == "keyword_only" {
24-
Some(ParameterKind::KeywordOnly)
25-
} else {
26-
None
19+
match ident.to_string().as_str() {
20+
"positional_only" => Some(ParameterKind::PositionalOnly),
21+
"positional_or_keyword" => Some(ParameterKind::PositionalOrKeyword),
22+
"keyword_only" => Some(ParameterKind::KeywordOnly),
23+
_ => None,
2724
}
2825
}
2926
}
@@ -150,6 +147,13 @@ fn generate_field(field: &Field) -> Result<TokenStream2, Diagnostic> {
150147
};
151148

152149
let name = &field.ident;
150+
if let Some(name) = name {
151+
if name.to_string().starts_with("_phantom") {
152+
return Ok(quote! {
153+
#name: std::marker::PhantomData,
154+
});
155+
}
156+
}
153157
let middle = quote! {
154158
.map(|x| ::rustpython_vm::pyobject::TryFromObject::try_from_object(vm, x)).transpose()?
155159
};
@@ -210,8 +214,9 @@ pub fn impl_from_args(input: DeriveInput) -> Result<TokenStream2, Diagnostic> {
210214
};
211215

212216
let name = input.ident;
217+
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
213218
let output = quote! {
214-
impl ::rustpython_vm::function::FromArgs for #name {
219+
impl #impl_generics ::rustpython_vm::function::FromArgs for #name #ty_generics #where_clause {
215220
fn from_args(
216221
vm: &::rustpython_vm::VirtualMachine,
217222
args: &mut ::rustpython_vm::function::PyFuncArgs

0 commit comments

Comments
 (0)