@@ -668,7 +668,7 @@ impl PythonBindGenerator {
668668 let snake_case_name = & variable_info[ 2 ] ;
669669
670670 self . file_contents . push ( Cow :: Owned ( format ! (
671- " {}Type::{variable_value} => flat::{}::{variable_name}(Box::from(&* borrow.{snake_case_name}.as_ref().unwrap()))," ,
671+ " {}Type::{variable_value} => flat::{}::{variable_name}(Box::from(borrow.{snake_case_name}.as_ref().unwrap()))," ,
672672 self . struct_name,
673673 self . struct_t_name,
674674 ) ) ) ;
@@ -780,55 +780,62 @@ impl PythonBindGenerator {
780780 }
781781
782782 self . write_string ( format ! ( "impl From<&Py<{}>> for {impl_type} {{" , self . struct_name) ) ;
783- self . write_string ( format ! ( " fn from(py_type: &Py<{}>) -> Self {{" , self . struct_name) ) ;
784- self . write_str ( " Python::with_gil(|py| {" ) ;
785- self . write_str ( " let borrow = py_type.borrow(py);" ) ;
786783
787- if is_box_type {
788- self . write_string ( format ! ( " Self::new(flat::{} {{" , self . struct_t_name) ) ;
784+ if self . types . is_empty ( ) {
785+ self . write_string ( format ! ( " fn from(_: &Py<{}>) -> Self {{" , self . struct_name) ) ;
786+ self . write_str ( " Self::default()" ) ;
789787 } else {
790- self . write_str ( " Self {" ) ;
791- }
788+ self . write_string ( format ! ( " fn from(py_type: &Py<{}>) -> Self {{" , self . struct_name) ) ;
789+ self . write_str ( " Python::with_gil(|py| {" ) ;
790+ self . write_str ( " let borrow = py_type.borrow(py);" ) ;
792791
793- for variable_info in & self . types {
794- let variable_name = & variable_info[ 0 ] ;
795- let variable_type = variable_info[ 1 ] . as_str ( ) ;
792+ if is_box_type {
793+ self . write_string ( format ! ( " Self::new(flat::{} {{" , self . struct_t_name) ) ;
794+ } else {
795+ self . write_str ( " Self {" ) ;
796+ }
796797
797- if variable_type. starts_with ( "Vec<" ) {
798- let inner_type = variable_type. trim_start_matches ( "Vec<" ) . trim_end_matches ( '>' ) ;
799- if Self :: BASE_TYPES . contains ( & inner_type) {
798+ for variable_info in & self . types {
799+ let variable_name = & variable_info[ 0 ] ;
800+ let variable_type = variable_info[ 1 ] . as_str ( ) ;
801+
802+ if variable_type. starts_with ( "Vec<" ) {
803+ let inner_type = variable_type. trim_start_matches ( "Vec<" ) . trim_end_matches ( '>' ) ;
804+ if Self :: BASE_TYPES . contains ( & inner_type) {
805+ self . file_contents
806+ . push ( Cow :: Owned ( format ! ( " {variable_name}: borrow.{variable_name}," ) ) ) ;
807+ } else {
808+ self . file_contents . push ( Cow :: Owned ( format ! (
809+ " {variable_name}: borrow.{variable_name}.iter().map(Into::into).collect()," ,
810+ ) ) ) ;
811+ }
812+ } else if variable_type. starts_with ( "Option<" ) {
813+ self . file_contents . push ( Cow :: Owned ( format ! (
814+ " {variable_name}: borrow.{variable_name}.as_ref().map(Into::into)," ,
815+ ) ) ) ;
816+ } else if variable_type == "String" {
817+ self . file_contents . push ( Cow :: Owned ( format ! (
818+ " {variable_name}: borrow.{variable_name}.clone()," ,
819+ ) ) ) ;
820+ } else if Self :: BASE_TYPES . contains ( & variable_type) {
800821 self . file_contents
801822 . push ( Cow :: Owned ( format ! ( " {variable_name}: borrow.{variable_name}," ) ) ) ;
802823 } else {
803824 self . file_contents . push ( Cow :: Owned ( format ! (
804- " {variable_name}: borrow.{variable_name}.iter().map(Into:: into).collect ()," ,
825+ " {variable_name}: (& borrow.{variable_name}). into()," ,
805826 ) ) ) ;
806827 }
807- } else if variable_type. starts_with ( "Option<" ) {
808- self . file_contents . push ( Cow :: Owned ( format ! (
809- " {variable_name}: borrow.{variable_name}.as_ref().map(Into::into)," ,
810- ) ) ) ;
811- } else if variable_type == "String" {
812- self . file_contents . push ( Cow :: Owned ( format ! (
813- " {variable_name}: borrow.{variable_name}.clone()," ,
814- ) ) ) ;
815- } else if Self :: BASE_TYPES . contains ( & variable_type) {
816- self . file_contents
817- . push ( Cow :: Owned ( format ! ( " {variable_name}: borrow.{variable_name}," ) ) ) ;
828+ }
829+
830+ if is_box_type {
831+ self . write_str ( " })" ) ;
818832 } else {
819- self . file_contents . push ( Cow :: Owned ( format ! (
820- " {variable_name}: (&borrow.{variable_name}).into()," ,
821- ) ) ) ;
833+ self . write_str ( " }" ) ;
822834 }
823- }
824835
825- if is_box_type {
826836 self . write_str ( " })" ) ;
827- } else {
828- self . write_str ( " }" ) ;
829837 }
830838
831- self . write_str ( " })" ) ;
832839 self . write_str ( " }" ) ;
833840 self . write_str ( "}" ) ;
834841 self . write_str ( "" ) ;
@@ -1084,8 +1091,10 @@ impl PythonBindGenerator {
10841091 let variable_name = & variable_info[ 0 ] ;
10851092 let variable_type = variable_info[ 1 ] . as_str ( ) ;
10861093
1087- if variable_type. starts_with ( "Vec<" ) {
1094+ if variable_type == "String" {
10881095 format ! ( "{variable_name}={{:?}}" )
1096+ } else if variable_type. starts_with ( "Vec<" ) {
1097+ format ! ( "{variable_name}=[{{}}]" )
10891098 } else {
10901099 format ! ( "{variable_name}={{}}" )
10911100 }
@@ -1119,7 +1128,8 @@ impl PythonBindGenerator {
11191128 self . file_contents
11201129 . push ( Cow :: Borrowed ( " .map(|x| x.borrow(py).__repr__(py))" ) ) ;
11211130 self . file_contents
1122- . push ( Cow :: Borrowed ( " .collect::<Box<[_]>>()," ) ) ;
1131+ . push ( Cow :: Borrowed ( " .collect::<Vec<String>>()" ) ) ;
1132+ self . file_contents . push ( Cow :: Borrowed ( " .join(\" , \" )," ) ) ;
11231133 } else if variable_type. ends_with ( 'T' ) || variable_type. starts_with ( "Box<" ) {
11241134 self . file_contents . push ( Cow :: Owned ( format ! (
11251135 " self.{variable_name}.borrow(py).__repr__(py),"
0 commit comments