@@ -541,33 +541,6 @@ impl PythonBindGenerator {
541541 self . write_str ( " }" ) ;
542542 self . write_str ( "}" ) ;
543543 self . write_str ( "" ) ;
544-
545- self . write_string ( format ! ( "impl From<{impl_type}> for {} {{" , self . struct_name) ) ;
546- self . write_string ( format ! ( " fn from(mut flat_t: {impl_type}) -> Self {{" ) ) ;
547- self . write_str ( " Self {" ) ;
548-
549- if is_box {
550- self . write_str ( " item_type: flat_t.as_ref().into()," ) ;
551- } else {
552- self . write_str ( " item_type: (&flat_t).into()," ) ;
553- }
554-
555- for variable_info in & self . types {
556- if variable_info[ 1 ] . is_empty ( ) {
557- continue ;
558- }
559-
560- let snake_case_name = & variable_info[ 2 ] ;
561-
562- self . file_contents . push ( Cow :: Owned ( format ! (
563- " {snake_case_name}: flat_t.take_{snake_case_name}().map(Into::into)," ,
564- ) ) ) ;
565- }
566-
567- self . write_str ( " }" ) ;
568- self . write_str ( " }" ) ;
569- self . write_str ( "}" ) ;
570- self . write_str ( "" ) ;
571544 }
572545 }
573546
@@ -610,62 +583,12 @@ impl PythonBindGenerator {
610583 self . write_str ( " }" ) ;
611584 self . write_str ( "}" ) ;
612585 self . write_str ( "" ) ;
613- } else {
614- self . write_string ( format ! ( " fn from(flat_t: {impl_type}) -> Self {{" ) ) ;
615- self . write_str ( " Python::with_gil(|py| {" ) ;
616- self . write_string ( format ! ( " Py::new(py, {} {{" , self . struct_name) ) ;
617-
618- for variable_info in & self . types {
619- let variable_name = & variable_info[ 0 ] ;
620- let variable_type = variable_info[ 1 ] . as_str ( ) ;
621-
622- if variable_type. starts_with ( "Vec<" ) {
623- let inner_type = variable_type. trim_start_matches ( "Vec<" ) . trim_end_matches ( '>' ) ;
624- if Self :: BASE_TYPES . contains ( & inner_type) {
625- self . file_contents
626- . push ( Cow :: Owned ( format ! ( " {variable_name}: flat_t.{variable_name}," ) ) ) ;
627- } else {
628- self . file_contents . push ( Cow :: Owned ( format ! (
629- " {variable_name}: flat_t.{variable_name}.into_iter().map(Into::into).collect()," ,
630- ) ) ) ;
631- }
632- } else if variable_type. starts_with ( "Option<" ) {
633- self . file_contents . push ( Cow :: Owned ( format ! (
634- " {variable_name}: flat_t.{variable_name}.map(Into::into)," ,
635- ) ) ) ;
636- } else if Self :: BASE_TYPES . contains ( & variable_type)
637- && !variable_type. starts_with ( "Box<" )
638- && !variable_type. ends_with ( 'T' )
639- {
640- self . file_contents
641- . push ( Cow :: Owned ( format ! ( " {variable_name}: flat_t.{variable_name}," ) ) ) ;
642- } else {
643- self . file_contents . push ( Cow :: Owned ( format ! (
644- " {variable_name}: flat_t.{variable_name}.into()," ,
645- ) ) ) ;
646- }
647- }
648-
649- self . write_str ( " }).unwrap()" ) ;
650- self . write_str ( " })" ) ;
651- self . write_str ( " }" ) ;
652- self . write_str ( "}" ) ;
653- self . write_str ( "" ) ;
654- }
655-
656- self . write_string ( format ! ( "impl From<{impl_type}> for {} {{" , self . struct_name) ) ;
657-
658- if self . types . is_empty ( ) {
659- self . write_string ( format ! ( " fn from(_: {impl_type}) -> Self {{" ) ) ;
660- self . write_str ( " Self {}" ) ;
661- self . write_str ( " }" ) ;
662- self . write_str ( "}" ) ;
663- self . write_str ( "" ) ;
664586 continue ;
665587 }
666588
667589 self . write_string ( format ! ( " fn from(flat_t: {impl_type}) -> Self {{" ) ) ;
668- self . write_str ( " Self {" ) ;
590+ self . write_str ( " Python::with_gil(|py| {" ) ;
591+ self . write_string ( format ! ( " Py::new(py, {} {{" , self . struct_name) ) ;
669592
670593 for variable_info in & self . types {
671594 let variable_name = & variable_info[ 0 ] ;
@@ -685,7 +608,10 @@ impl PythonBindGenerator {
685608 self . file_contents . push ( Cow :: Owned ( format ! (
686609 " {variable_name}: flat_t.{variable_name}.map(Into::into)," ,
687610 ) ) ) ;
688- } else if Self :: BASE_TYPES . contains ( & variable_type) {
611+ } else if Self :: BASE_TYPES . contains ( & variable_type)
612+ && !variable_type. starts_with ( "Box<" )
613+ && !variable_type. ends_with ( 'T' )
614+ {
689615 self . file_contents
690616 . push ( Cow :: Owned ( format ! ( " {variable_name}: flat_t.{variable_name}," ) ) ) ;
691617 } else {
@@ -695,7 +621,8 @@ impl PythonBindGenerator {
695621 }
696622 }
697623
698- self . write_str ( " }" ) ;
624+ self . write_str ( " }).unwrap()" ) ;
625+ self . write_str ( " })" ) ;
699626 self . write_str ( " }" ) ;
700627 self . write_str ( "}" ) ;
701628 self . write_str ( "" ) ;
@@ -741,7 +668,7 @@ impl PythonBindGenerator {
741668 let snake_case_name = & variable_info[ 2 ] ;
742669
743670 self . file_contents . push ( Cow :: Owned ( format ! (
744- " {}Type::{variable_value} => flat::{}::{variable_name}(Box::from(&*borrow.{snake_case_name}.as_ref().unwrap().borrow(py) ))," ,
671+ " {}Type::{variable_value} => flat::{}::{variable_name}(Box::from(&*borrow.{snake_case_name}.as_ref().unwrap()))," ,
745672 self . struct_name,
746673 self . struct_t_name,
747674 ) ) ) ;
@@ -797,75 +724,61 @@ impl PythonBindGenerator {
797724 ] ;
798725
799726 for impl_type in from_impl_types {
800- self . write_string ( format ! ( "impl From<&{}> for {impl_type} {{" , self . struct_name) ) ;
801-
802727 let is_box_type = impl_type. contains ( "Box<" ) ;
803728
804- if self . types . is_empty ( ) {
805- self . write_string ( format ! ( " fn from(_: &{}) -> Self {{" , self . struct_name) ) ;
729+ if !is_box_type {
730+ self . write_string ( format ! ( "impl From< &{}> for {impl_type} {{" , self . struct_name) ) ;
806731
807- if is_box_type {
808- self . write_str ( " Self::default()" ) ;
809- } else {
732+ if self . types . is_empty ( ) {
733+ self . write_string ( format ! ( " fn from(_: &{}) -> Self {{" , self . struct_name) ) ;
810734 self . write_str ( " Self {}" ) ;
735+ self . write_str ( " }" ) ;
736+ self . write_str ( "}" ) ;
737+ self . write_str ( "" ) ;
738+ continue ;
811739 }
812740
813- self . write_str ( " }" ) ;
814- self . write_str ( "}" ) ;
815- self . write_str ( "" ) ;
816- continue ;
817- }
818-
819- self . write_string ( format ! ( " fn from(py_type: &{}) -> Self {{" , self . struct_name) ) ;
820-
821- if is_box_type {
822- self . write_string ( format ! ( " Self::new(flat::{} {{" , self . struct_t_name) ) ;
823- } else {
741+ self . write_string ( format ! ( " fn from(py_type: &{}) -> Self {{" , self . struct_name) ) ;
824742 self . write_str ( " Self {" ) ;
825- }
826743
827- for variable_info in & self . types {
828- let variable_name = & variable_info[ 0 ] ;
829- let variable_type = variable_info[ 1 ] . as_str ( ) ;
744+ for variable_info in & self . types {
745+ let variable_name = & variable_info[ 0 ] ;
746+ let variable_type = variable_info[ 1 ] . as_str ( ) ;
830747
831- if variable_type. starts_with ( "Vec<" ) {
832- let inner_type = variable_type. trim_start_matches ( "Vec<" ) . trim_end_matches ( '>' ) ;
833- if Self :: BASE_TYPES . contains ( & inner_type) {
748+ if variable_type. starts_with ( "Vec<" ) {
749+ let inner_type = variable_type. trim_start_matches ( "Vec<" ) . trim_end_matches ( '>' ) ;
750+ if Self :: BASE_TYPES . contains ( & inner_type) {
751+ self . file_contents
752+ . push ( Cow :: Owned ( format ! ( " {variable_name}: py_type.{variable_name}," ) ) ) ;
753+ } else {
754+ self . file_contents . push ( Cow :: Owned ( format ! (
755+ " {variable_name}: py_type.{variable_name}.iter().map(Into::into).collect()," ,
756+ ) ) ) ;
757+ }
758+ } else if variable_type. starts_with ( "Option<" ) {
759+ self . file_contents . push ( Cow :: Owned ( format ! (
760+ " {variable_name}: py_type.{variable_name}.as_ref().map(Into::into)," ,
761+ ) ) ) ;
762+ } else if variable_type == "String" {
763+ self . file_contents . push ( Cow :: Owned ( format ! (
764+ " {variable_name}: py_type.{variable_name}.clone()," ,
765+ ) ) ) ;
766+ } else if Self :: BASE_TYPES . contains ( & variable_type) {
834767 self . file_contents
835768 . push ( Cow :: Owned ( format ! ( " {variable_name}: py_type.{variable_name}," ) ) ) ;
836769 } else {
837770 self . file_contents . push ( Cow :: Owned ( format ! (
838- " {variable_name}: py_type.{variable_name}.iter().map(Into:: into).collect ()," ,
771+ " {variable_name}: (& py_type.{variable_name}). into()," ,
839772 ) ) ) ;
840773 }
841- } else if variable_type. starts_with ( "Option<" ) {
842- self . file_contents . push ( Cow :: Owned ( format ! (
843- " {variable_name}: py_type.{variable_name}.as_ref().map(Into::into)," ,
844- ) ) ) ;
845- } else if variable_type == "String" {
846- self . file_contents . push ( Cow :: Owned ( format ! (
847- " {variable_name}: py_type.{variable_name}.clone()," ,
848- ) ) ) ;
849- } else if Self :: BASE_TYPES . contains ( & variable_type) {
850- self . file_contents
851- . push ( Cow :: Owned ( format ! ( " {variable_name}: py_type.{variable_name}," ) ) ) ;
852- } else {
853- self . file_contents . push ( Cow :: Owned ( format ! (
854- " {variable_name}: (&py_type.{variable_name}).into()," ,
855- ) ) ) ;
856774 }
857- }
858775
859- if is_box_type {
860- self . write_str ( " })" ) ;
861- } else {
862776 self . write_str ( " }" ) ;
777+ self . write_str ( " }" ) ;
778+ self . write_str ( "}" ) ;
779+ self . write_str ( "" ) ;
863780 }
864781
865- self . write_str ( " }" ) ;
866- self . write_str ( "}" ) ;
867- self . write_str ( "" ) ;
868-
869782 self . write_string ( format ! ( "impl From<&Py<{}>> for {impl_type} {{" , self . struct_name) ) ;
870783 self . write_string ( format ! ( " fn from(py_type: &Py<{}>) -> Self {{" , self . struct_name) ) ;
871784 self . write_str ( " Python::with_gil(|py| {" ) ;
@@ -1252,11 +1165,12 @@ impl PythonBindGenerator {
12521165
12531166 fn generate_unpack_method ( & mut self ) {
12541167 self . write_str ( " #[staticmethod]" ) ;
1255- self . write_str ( " fn unpack(data: &[u8]) -> Self {" ) ;
12561168
12571169 if self . bind_type == PythonBindType :: Enum {
1170+ self . write_str ( " fn unpack(data: &[u8]) -> Self {" ) ;
12581171 self . write_string ( format ! ( " root::<flat::{}>(data).unwrap().into()" , self . struct_name) ) ;
12591172 } else {
1173+ self . write_str ( " fn unpack(data: &[u8]) -> Py<Self> {" ) ;
12601174 self . write_string ( format ! (
12611175 " root::<flat::{}>(data).unwrap().unpack().into()" ,
12621176 self . struct_name
0 commit comments