File tree 4 files changed +48
-10
lines changed
4 files changed +48
-10
lines changed Original file line number Diff line number Diff line change @@ -1172,14 +1172,28 @@ fn impl_complex_enum_tuple_variant_match_args(
1172
1172
variant_cls_type : & syn:: Type ,
1173
1173
field_names : & mut Vec < Ident > ,
1174
1174
) -> Result < ( MethodAndMethodDef , syn:: ImplItemConst ) > {
1175
- let args_tp = field_names. iter ( ) . map ( |_| {
1176
- quote ! { & ' static str }
1177
- } ) ;
1178
-
1179
- let match_args_const_impl: syn:: ImplItemConst = parse_quote ! {
1180
- const __match_args__: ( #( #args_tp) , * ) = (
1181
- #( stringify!( #field_names) , ) *
1182
- ) ;
1175
+ let match_args_const_impl: syn:: ImplItemConst = match field_names. len ( ) {
1176
+ // This covers the case where the tuple variant has no fields (valid Rust)
1177
+ 0 => parse_quote ! {
1178
+ const __match_args__: ( ) = ( ) ;
1179
+ } ,
1180
+ 1 => {
1181
+ let ident = & field_names[ 0 ] ;
1182
+ // We need the trailing comma to make it a tuple
1183
+ parse_quote ! {
1184
+ const __match_args__: ( & ' static str , ) = ( stringify!( #ident) , ) ;
1185
+ }
1186
+ }
1187
+ _ => {
1188
+ let args_tp = field_names. iter ( ) . map ( |_| {
1189
+ quote ! { & ' static str }
1190
+ } ) ;
1191
+ parse_quote ! {
1192
+ const __match_args__: ( #( #args_tp) , * ) = (
1193
+ #( stringify!( #field_names) , ) *
1194
+ ) ;
1195
+ }
1196
+ }
1183
1197
} ;
1184
1198
1185
1199
let spec = ConstSpec {
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ use pyo3::{
8
8
pub fn enums ( m : & Bound < ' _ , PyModule > ) -> PyResult < ( ) > {
9
9
m. add_class :: < SimpleEnum > ( ) ?;
10
10
m. add_class :: < ComplexEnum > ( ) ?;
11
+ m. add_class :: < SimpleTupleEnum > ( ) ?;
11
12
m. add_class :: < TupleEnum > ( ) ?;
12
13
m. add_class :: < MixedComplexEnum > ( ) ?;
13
14
m. add_wrapped ( wrap_pyfunction_bound ! ( do_simple_stuff) ) ?;
@@ -84,6 +85,12 @@ pub fn do_complex_stuff(thing: &ComplexEnum) -> ComplexEnum {
84
85
}
85
86
}
86
87
88
+ #[ pyclass]
89
+ enum SimpleTupleEnum {
90
+ Int ( i32 ) ,
91
+ Str ( String ) ,
92
+ }
93
+
87
94
#[ pyclass]
88
95
pub enum TupleEnum {
89
96
Full ( i32 , f64 , bool ) ,
Original file line number Diff line number Diff line change @@ -96,6 +96,23 @@ def test_tuple_enum_match_statement(variant: enums.TupleEnum):
96
96
assert False
97
97
98
98
99
+ @pytest .mark .parametrize (
100
+ "variant" ,
101
+ [
102
+ enums .SimpleTupleEnum .Int (42 ),
103
+ enums .SimpleTupleEnum .Str ("hello" ),
104
+ ],
105
+ )
106
+ def test_simple_tuple_enum_match_statement (variant : enums .SimpleTupleEnum ):
107
+ match variant :
108
+ case enums .SimpleTupleEnum .Int (x ):
109
+ assert x == 42
110
+ case enums .SimpleTupleEnum .Str (x ):
111
+ assert x == "hello"
112
+ case _:
113
+ assert False
114
+
115
+
99
116
@pytest .mark .parametrize (
100
117
"variant" ,
101
118
[
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ error: Unit variant `UnitVariant` is not yet supported in a complex enum
25
25
| ^^^^^^^^^^^
26
26
27
27
error: `constructor` can't be used on a simple enum variant
28
- --> tests/ui/invalid_pyclass_enum.rs:32 :12
28
+ --> tests/ui/invalid_pyclass_enum.rs:26 :12
29
29
|
30
- 32 | #[pyo3(constructor = (a, b))]
30
+ 26 | #[pyo3(constructor = (a, b))]
31
31
| ^^^^^^^^^^^
You can’t perform that action at this time.
0 commit comments