Skip to content

Commit a1be634

Browse files
committed
Simplify internal structure of union (2/2)
1 parent a349a8c commit a1be634

1 file changed

Lines changed: 35 additions & 160 deletions

File tree

build.rs

Lines changed: 35 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ impl PythonBindGenerator {
371371
}
372372

373373
fn generate_union_definition(&mut self) {
374-
self.write_str("#[derive(pyo3::FromPyObject)]");
374+
self.write_str("#[derive(Debug, Clone, pyo3::FromPyObject)]");
375375
self.write_string(format!("pub enum {}Union {{", self.struct_name));
376376

377377
for variable_info in self.types.iter().skip(1) {
@@ -385,47 +385,8 @@ impl PythonBindGenerator {
385385
self.write_str("#[pyclass(module = \"rlbot_flatbuffers\")]");
386386
self.write_str("#[derive(Debug, Default, Clone)]");
387387
self.write_string(format!("pub struct {} {{", self.struct_name));
388-
389-
for variable_info in &self.types {
390-
let variable_type = &variable_info[1];
391-
392-
if variable_type.is_empty() {
393-
continue;
394-
}
395-
396-
let snake_case_name = &variable_info[2];
397-
398-
self.file_contents.push(Cow::Owned(format!(
399-
" {snake_case_name}: Option<Py<super::{variable_type}>>,",
400-
)));
401-
}
402-
403-
self.write_str("}");
404-
self.write_str("");
405-
self.write_string(format!("impl {} {{", self.struct_name));
406-
self.write_string(format!(" pub fn get(&self) -> Option<{}Union> {{", self.struct_name));
407-
408-
for variable_info in &self.types {
409-
let variable_name = &variable_info[0];
410-
411-
if variable_name == "NONE" {
412-
continue;
413-
}
414-
415-
let wanted_snake_case_name = &variable_info[2];
416-
417-
self.file_contents.push(Cow::Owned(format!(
418-
" if let Some(item) = self.{wanted_snake_case_name}.as_ref() {{"
419-
)));
420-
self.file_contents.push(Cow::Owned(format!(
421-
" return Some({}Union::{}(item.clone()));",
422-
self.struct_name, variable_name
423-
)));
424-
self.file_contents.push(Cow::Borrowed(" }"));
425-
self.file_contents.push(Cow::Borrowed(""));
426-
}
427-
self.write_str(" None");
428-
self.write_str(" }");
388+
self.write_str(" #[pyo3(set)]");
389+
self.write_string(format!(" pub item: Option<{}Union>,", self.struct_name));
429390
self.write_str("}");
430391
self.write_str("");
431392
}
@@ -446,21 +407,35 @@ impl PythonBindGenerator {
446407

447408
for impl_type in from_impl_types {
448409
self.write_string(format!("impl FromGil<{impl_type}> for Py<{}> {{", self.struct_name));
449-
self.write_string(format!(" fn from_gil(py: Python, mut flat_t: {impl_type}) -> Self {{"));
410+
self.write_string(format!(" fn from_gil(py: Python, flat_t: {impl_type}) -> Self {{"));
450411
self.write_str(" Py::new(");
451412
self.write_str(" py,");
452-
self.write_string(format!(" {} {{", self.struct_name));
453413

454-
for variable_info in &self.types {
455-
if variable_info[1].is_empty() {
456-
continue;
457-
}
414+
if impl_type.starts_with("Box<") {
415+
self.write_str(" match *flat_t {");
416+
} else {
417+
self.write_str(" match flat_t {");
418+
}
458419

459-
let snake_case_name = &variable_info[2];
420+
for variable_info in &self.types {
421+
let variable_name = &variable_info[0];
460422

461-
self.file_contents.push(Cow::Owned(format!(
462-
" {snake_case_name}: flat_t.take_{snake_case_name}().map(|x| x.into_gil(py)),",
463-
)));
423+
if variable_name == "NONE" {
424+
self.file_contents.push(Cow::Owned(format!(
425+
" flat::{}::NONE => {}::default(),",
426+
self.struct_t_name, self.struct_name
427+
)));
428+
} else {
429+
self.file_contents.push(Cow::Owned(format!(
430+
" flat::{}::{variable_name}(item) => {} {{",
431+
self.struct_t_name, self.struct_name,
432+
)));
433+
self.file_contents.push(Cow::Owned(format!(
434+
" item: Some({}Union::{variable_name}(item.into_gil(py)))",
435+
self.struct_name,
436+
)));
437+
self.file_contents.push(Cow::Borrowed(" },"));
438+
}
464439
}
465440

466441
self.write_str(" },");
@@ -569,9 +544,9 @@ impl PythonBindGenerator {
569544

570545
let is_box_type = impl_type.contains("Box<");
571546
if is_box_type {
572-
self.write_str(" Self::new(match py_type.borrow(py).get() {");
547+
self.write_str(" Self::new(match py_type.borrow(py).item.as_ref() {");
573548
} else {
574-
self.write_str(" match py_type.borrow(py).get() {");
549+
self.write_str(" match py_type.borrow(py).item.as_ref() {");
575550
}
576551

577552
for variable_info in &self.types {
@@ -587,9 +562,8 @@ impl PythonBindGenerator {
587562
}
588563
} else {
589564
self.file_contents.push(Cow::Owned(format!(
590-
" Some({}Union::{variable_value}(item)) => flat::{}::{variable_name}((&item).into_gil(py)),",
591-
self.struct_name,
592-
self.struct_t_name,
565+
" Some({}Union::{variable_value}(item)) => flat::{}::{variable_name}(item.into_gil(py)),",
566+
self.struct_name, self.struct_t_name
593567
)));
594568
}
595569
}
@@ -786,50 +760,12 @@ impl PythonBindGenerator {
786760
self.write_str(" #[new]");
787761
self.write_str(" #[pyo3(signature = (item = None))]");
788762
self.write_string(format!(" pub fn new(item: Option<{}Union>) -> Self {{", self.struct_name));
789-
self.write_str(" match item {");
790-
791-
for variable_info in &self.types {
792-
let variable_name = &variable_info[0];
793-
794-
if variable_name == "NONE" {
795-
self.file_contents.push(Cow::Borrowed(" None => Self::default(),"));
796-
} else {
797-
let wanted_snake_case_name = &variable_info[2];
798-
799-
self.file_contents.push(Cow::Owned(format!(
800-
" Some({}Union::{}({wanted_snake_case_name})) => Self {{",
801-
self.struct_name, variable_name
802-
)));
803-
804-
for variable_info in &self.types {
805-
let variable_type = &variable_info[1];
806-
807-
if variable_type.is_empty() {
808-
continue;
809-
}
810-
811-
let snake_case_name = &variable_info[2];
812-
813-
if wanted_snake_case_name == snake_case_name {
814-
self.file_contents.push(Cow::Owned(format!(
815-
" {snake_case_name}: Some({snake_case_name}),",
816-
)));
817-
} else {
818-
self.file_contents
819-
.push(Cow::Owned(format!(" {snake_case_name}: None,",)));
820-
}
821-
}
822-
823-
self.file_contents.push(Cow::Borrowed(" },"));
824-
}
825-
}
826-
827-
self.write_str(" }");
763+
self.write_str(" Self { item }");
828764
self.write_str(" }");
829765
self.write_str("");
830766
self.write_str(" #[getter(item)]");
831-
self.write_str(" pub fn get_item(&self, py: Python) -> Option<PyObject> {");
832-
self.write_str(" match self.get() {");
767+
self.write_str(" pub fn get(&self, py: Python) -> Option<PyObject> {");
768+
self.write_str(" match self.item.as_ref() {");
833769

834770
for variable_info in &self.types {
835771
let variable_name = &variable_info[0];
@@ -844,67 +780,6 @@ impl PythonBindGenerator {
844780
}
845781
}
846782

847-
self.write_str(" }");
848-
self.write_str(" }");
849-
self.write_str("");
850-
self.write_str(" #[setter(item)]");
851-
self.write_string(format!(
852-
" pub fn set_item(&mut self, item: Option<{}Union>) {{",
853-
self.struct_name
854-
));
855-
self.write_str(" match item {");
856-
857-
for variable_info in &self.types {
858-
let variable_name = &variable_info[0];
859-
860-
if variable_name == "NONE" {
861-
self.file_contents.push(Cow::Borrowed(" None => {"));
862-
863-
for variable_info in &self.types {
864-
let variable_type = &variable_info[1];
865-
866-
if variable_type.is_empty() {
867-
continue;
868-
}
869-
870-
let snake_case_name = &variable_info[2];
871-
872-
self.file_contents
873-
.push(Cow::Owned(format!(" self.{snake_case_name} = None;",)));
874-
}
875-
876-
self.file_contents.push(Cow::Borrowed(" }"));
877-
} else {
878-
let wanted_snake_case_name = &variable_info[2];
879-
880-
self.file_contents.push(Cow::Owned(format!(
881-
" Some({}Union::{}({wanted_snake_case_name})) => {{",
882-
self.struct_name, variable_name
883-
)));
884-
885-
for variable_info in &self.types {
886-
let variable_type = &variable_info[1];
887-
888-
if variable_type.is_empty() {
889-
continue;
890-
}
891-
892-
let snake_case_name = &variable_info[2];
893-
894-
if wanted_snake_case_name == snake_case_name {
895-
self.file_contents.push(Cow::Owned(format!(
896-
" self.{snake_case_name} = Some({snake_case_name});",
897-
)));
898-
} else {
899-
self.file_contents
900-
.push(Cow::Owned(format!(" self.{snake_case_name} = None;",)));
901-
}
902-
}
903-
904-
self.file_contents.push(Cow::Borrowed(" }"));
905-
}
906-
}
907-
908783
self.write_str(" }");
909784
self.write_str(" }");
910785
}
@@ -1066,7 +941,7 @@ impl PythonBindGenerator {
1066941

1067942
fn generate_union_repr_method(&mut self) {
1068943
self.write_str(" pub fn __repr__(&self, py: Python) -> String {");
1069-
self.write_str(" match self.get() {");
944+
self.write_str(" match self.item.as_ref() {");
1070945

1071946
for variable_info in &self.types {
1072947
let variable_name = &variable_info[0];

0 commit comments

Comments
 (0)