Skip to content

Commit 51b7660

Browse files
committed
Allow an enum with #[pyclass]
1 parent 53b46a7 commit 51b7660

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

derive/src/pyclass.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ pub fn impl_pyimpl(attr: AttributeArgs, item: Item) -> TokenStream2 {
137137
}
138138

139139
pub fn impl_pyclass(attr: AttributeArgs, item: Item) -> TokenStream2 {
140-
let struc = if let Item::Struct(struc) = item {
141-
struc
142-
} else {
143-
panic!("#[pyclass] can only be on a struct declaration");
140+
let (ident, attrs) = match item.clone() {
141+
Item::Struct(struc) => (struc.ident, struc.attrs),
142+
Item::Enum(enu) => (enu.ident, enu.attrs),
143+
_ => panic!("#[pyclass] can only be on a struct or enum declaration"),
144144
};
145145

146146
let rp_path = rustpython_path_attr(&attr);
@@ -159,10 +159,10 @@ pub fn impl_pyclass(attr: AttributeArgs, item: Item) -> TokenStream2 {
159159
}
160160
}
161161
}
162-
let class_name = class_name.unwrap_or_else(|| struc.ident.to_string());
162+
let class_name = class_name.unwrap_or_else(|| ident.to_string());
163163

164164
let mut doc: Option<Vec<String>> = None;
165-
for attr in struc.attrs.iter() {
165+
for attr in attrs.iter() {
166166
if attr.path.is_ident("doc") {
167167
let meta = attr.parse_meta().expect("expected doc attr to be a meta");
168168
if let Meta::NameValue(name_value) = meta {
@@ -186,11 +186,9 @@ pub fn impl_pyclass(attr: AttributeArgs, item: Item) -> TokenStream2 {
186186
None => quote!(None),
187187
};
188188

189-
let ty = &struc.ident;
190-
191189
quote! {
192-
#struc
193-
impl #rp_path::pyobject::PyClassDef for #ty {
190+
#item
191+
impl #rp_path::pyobject::PyClassDef for #ident {
194192
const NAME: &'static str = #class_name;
195193
const DOC: Option<&'static str> = #doc;
196194
}

0 commit comments

Comments
 (0)