@@ -1138,6 +1138,12 @@ impl PythonBindGenerator {
11381138 self . write_str ( " }" ) ;
11391139 }
11401140
1141+ fn generate_enum_hash_method ( & mut self ) {
1142+ self . write_str ( " pub fn __hash__(&self) -> u64 {" ) ;
1143+ self . write_str ( " crate::hash_u64(*self as u64)" ) ;
1144+ self . write_str ( " }" ) ;
1145+ }
1146+
11411147 fn generate_py_methods ( & mut self ) {
11421148 self . write_str ( "#[pymethods]" ) ;
11431149 self . write_string ( format ! ( "impl {} {{" , self . struct_name) ) ;
@@ -1148,6 +1154,11 @@ impl PythonBindGenerator {
11481154 self . write_str ( "" ) ;
11491155 self . generate_repr_method ( ) ;
11501156
1157+ if self . bind_type == PythonBindType :: Enum {
1158+ self . write_str ( "" ) ;
1159+ self . generate_enum_hash_method ( ) ;
1160+ }
1161+
11511162 if self . bind_type != PythonBindType :: Union {
11521163 self . write_str ( "" ) ;
11531164 self . generate_pack_method ( ) ;
@@ -1358,7 +1369,9 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
13581369 if is_enum {
13591370 file_contents. push ( Cow :: Borrowed ( " def __init__(self, value: int = 0):" ) ) ;
13601371 file_contents. push ( Cow :: Borrowed ( " \" \" \" " ) ) ;
1361- file_contents. push ( Cow :: Borrowed ( " :raises ValueError: If the `value` is not a valid enum value" ) ) ;
1372+ file_contents. push ( Cow :: Borrowed (
1373+ " :raises ValueError: If the `value` is not a valid enum value" ,
1374+ ) ) ;
13621375 file_contents. push ( Cow :: Borrowed ( " \" \" \" " ) ) ;
13631376 } else {
13641377 file_contents. push ( Cow :: Borrowed ( " def __init__(" ) ) ;
@@ -1405,6 +1418,7 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
14051418
14061419 file_contents. push ( Cow :: Borrowed ( " def __str__(self) -> str: ..." ) ) ;
14071420 file_contents. push ( Cow :: Borrowed ( " def __repr__(self) -> str: ..." ) ) ;
1421+ file_contents. push ( Cow :: Borrowed ( " def __hash__(self) -> str: ..." ) ) ;
14081422
14091423 if is_enum {
14101424 file_contents. push ( Cow :: Borrowed ( " def __int__(self) -> int: ..." ) ) ;
0 commit comments