Skip to content

Commit a349a8c

Browse files
committed
Simplify internal structure of RenderType (1/2)
1 parent c186291 commit a349a8c

1 file changed

Lines changed: 48 additions & 115 deletions

File tree

build.rs

Lines changed: 48 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -380,29 +380,11 @@ impl PythonBindGenerator {
380380
.push(Cow::Owned(format!(" {variable_name}(Py<super::{variable_name}>),")));
381381
}
382382

383-
self.write_str("}");
384-
self.write_str("");
385-
386-
self.write_str("#[derive(Debug, Default, Clone, Copy)]");
387-
self.write_string(format!("pub enum {}Type {{", self.struct_name));
388-
self.write_str(" #[default]");
389-
390-
for variable_info in &self.types {
391-
let variable_name = &variable_info[0];
392-
393-
if variable_name == "NONE" {
394-
self.file_contents.push(Cow::Borrowed(" None,"));
395-
} else {
396-
self.file_contents.push(Cow::Owned(format!(" {variable_name},")));
397-
}
398-
}
399-
400383
self.write_str("}");
401384
self.write_str("");
402385
self.write_str("#[pyclass(module = \"rlbot_flatbuffers\")]");
403386
self.write_str("#[derive(Debug, Default, Clone)]");
404387
self.write_string(format!("pub struct {} {{", self.struct_name));
405-
self.write_string(format!(" item_type: {}Type,", self.struct_name));
406388

407389
for variable_info in &self.types {
408390
let variable_type = &variable_info[1];
@@ -420,6 +402,32 @@ impl PythonBindGenerator {
420402

421403
self.write_str("}");
422404
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(" }");
429+
self.write_str("}");
430+
self.write_str("");
423431
}
424432

425433
fn generate_from_flat_impls(&mut self) {
@@ -433,55 +441,15 @@ impl PythonBindGenerator {
433441
fn generate_union_from_flat_impls(&mut self) {
434442
let from_impl_types = [
435443
format!("flat::{}", self.struct_t_name),
436-
format!("&flat::{}", self.struct_t_name),
437444
format!("Box<flat::{}>", self.struct_t_name),
438445
];
439446

440447
for impl_type in from_impl_types {
441-
self.write_string(format!("impl From<{impl_type}> for {}Type {{", self.struct_name));
442-
self.write_string(format!(" fn from(flat_t: {impl_type}) -> Self {{"));
443-
444-
let is_box = impl_type.starts_with("Box<");
445-
if is_box {
446-
self.write_str(" match *flat_t {");
447-
} else {
448-
self.write_str(" match flat_t {");
449-
}
450-
451-
for variable_info in &self.types {
452-
let variable_name = &variable_info[0];
453-
454-
if variable_name == "NONE" {
455-
self.file_contents.push(Cow::Owned(format!(
456-
" flat::{}::{variable_name} => Self::None,",
457-
self.struct_t_name,
458-
)));
459-
} else {
460-
self.file_contents.push(Cow::Owned(format!(
461-
" flat::{}::{variable_name}(_) => Self::{variable_name},",
462-
self.struct_t_name,
463-
)));
464-
}
465-
}
466-
467-
self.write_str(" }");
468-
self.write_str(" }");
469-
self.write_str("}");
470-
self.write_str("");
471-
472-
if impl_type.starts_with('&') {
473-
continue;
474-
}
475-
476448
self.write_string(format!("impl FromGil<{impl_type}> for Py<{}> {{", self.struct_name));
477449
self.write_string(format!(" fn from_gil(py: Python, mut flat_t: {impl_type}) -> Self {{"));
478-
self.write_string(format!(" Py::new(py, {} {{", self.struct_name));
479-
480-
if is_box {
481-
self.write_str(" item_type: flat_t.as_ref().into(),");
482-
} else {
483-
self.write_str(" item_type: (&flat_t).into(),");
484-
}
450+
self.write_str(" Py::new(");
451+
self.write_str(" py,");
452+
self.write_string(format!(" {} {{", self.struct_name));
485453

486454
for variable_info in &self.types {
487455
if variable_info[1].is_empty() {
@@ -491,11 +459,13 @@ impl PythonBindGenerator {
491459
let snake_case_name = &variable_info[2];
492460

493461
self.file_contents.push(Cow::Owned(format!(
494-
" {snake_case_name}: flat_t.take_{snake_case_name}().map(|x| x.into_gil(py)),",
462+
" {snake_case_name}: flat_t.take_{snake_case_name}().map(|x| x.into_gil(py)),",
495463
)));
496464
}
497465

498-
self.write_str(" }).unwrap()");
466+
self.write_str(" },");
467+
self.write_str(" )");
468+
self.write_str(" .unwrap()");
499469
self.write_str(" }");
500470
self.write_str("}");
501471
self.write_str("");
@@ -596,13 +566,12 @@ impl PythonBindGenerator {
596566
" fn from_gil(py: Python, py_type: &Py<{}>) -> Self {{",
597567
self.struct_name
598568
));
599-
self.write_str(" let borrow = py_type.borrow(py);");
600569

601570
let is_box_type = impl_type.contains("Box<");
602571
if is_box_type {
603-
self.write_str(" Self::new(match borrow.item_type {");
572+
self.write_str(" Self::new(match py_type.borrow(py).get() {");
604573
} else {
605-
self.write_str(" match borrow.item_type {");
574+
self.write_str(" match py_type.borrow(py).get() {");
606575
}
607576

608577
for variable_info in &self.types {
@@ -611,21 +580,14 @@ impl PythonBindGenerator {
611580

612581
if variable_value.is_empty() {
613582
if is_box_type {
614-
self.file_contents.push(Cow::Owned(format!(
615-
" {}Type::None => flat::{}::NONE,",
616-
self.struct_name, self.struct_t_name
617-
)));
583+
self.file_contents
584+
.push(Cow::Owned(format!(" None => flat::{}::NONE,", self.struct_t_name)));
618585
} else {
619-
self.file_contents.push(Cow::Owned(format!(
620-
" {}Type::None => Self::NONE,",
621-
self.struct_name
622-
)));
586+
self.file_contents.push(Cow::Borrowed(" None => Self::NONE,"));
623587
}
624588
} else {
625-
let snake_case_name = &variable_info[2];
626-
627589
self.file_contents.push(Cow::Owned(format!(
628-
" {}Type::{variable_value} => flat::{}::{variable_name}(borrow.{snake_case_name}.as_ref().unwrap().into_gil(py)),",
590+
" Some({}Union::{variable_value}(item)) => flat::{}::{variable_name}((&item).into_gil(py)),",
629591
self.struct_name,
630592
self.struct_t_name,
631593
)));
@@ -838,10 +800,6 @@ impl PythonBindGenerator {
838800
" Some({}Union::{}({wanted_snake_case_name})) => Self {{",
839801
self.struct_name, variable_name
840802
)));
841-
self.file_contents.push(Cow::Owned(format!(
842-
" item_type: {}Type::{variable_name},",
843-
self.struct_name
844-
)));
845803

846804
for variable_info in &self.types {
847805
let variable_type = &variable_info[1];
@@ -870,20 +828,17 @@ impl PythonBindGenerator {
870828
self.write_str(" }");
871829
self.write_str("");
872830
self.write_str(" #[getter(item)]");
873-
self.write_str(" pub fn get_item(&mut self, py: Python) -> Option<PyObject> {");
874-
self.write_str(" match self.item_type {");
831+
self.write_str(" pub fn get_item(&self, py: Python) -> Option<PyObject> {");
832+
self.write_str(" match self.get() {");
875833

876834
for variable_info in &self.types {
877835
let variable_name = &variable_info[0];
878836

879837
if variable_name == "NONE" {
880-
self.file_contents
881-
.push(Cow::Owned(format!(" {}Type::None => None,", self.struct_name)));
838+
self.file_contents.push(Cow::Borrowed(" None => None,"));
882839
} else {
883-
let wanted_snake_case_name = &variable_info[2];
884-
885840
self.file_contents.push(Cow::Owned(format!(
886-
" {}Type::{variable_name} => self.{wanted_snake_case_name}.as_ref().map(|x| x.to_object(py)),",
841+
" Some({}Union::{variable_name}(item)) => Some(item.to_object(py)),",
887842
self.struct_name
888843
)));
889844
}
@@ -904,10 +859,6 @@ impl PythonBindGenerator {
904859

905860
if variable_name == "NONE" {
906861
self.file_contents.push(Cow::Borrowed(" None => {"));
907-
self.file_contents.push(Cow::Owned(format!(
908-
" self.item_type = {}Type::None;",
909-
self.struct_name
910-
)));
911862

912863
for variable_info in &self.types {
913864
let variable_type = &variable_info[1];
@@ -930,10 +881,6 @@ impl PythonBindGenerator {
930881
" Some({}Union::{}({wanted_snake_case_name})) => {{",
931882
self.struct_name, variable_name
932883
)));
933-
self.file_contents.push(Cow::Owned(format!(
934-
" self.item_type = {}Type::{variable_name};",
935-
self.struct_name
936-
)));
937884

938885
for variable_info in &self.types {
939886
let variable_type = &variable_info[1];
@@ -960,7 +907,6 @@ impl PythonBindGenerator {
960907

961908
self.write_str(" }");
962909
self.write_str(" }");
963-
self.write_str("");
964910
}
965911

966912
fn generate_enum_new_method(&mut self) {
@@ -1120,35 +1066,22 @@ impl PythonBindGenerator {
11201066

11211067
fn generate_union_repr_method(&mut self) {
11221068
self.write_str(" pub fn __repr__(&self, py: Python) -> String {");
1123-
self.write_str(" match self.item_type {");
1069+
self.write_str(" match self.get() {");
11241070

11251071
for variable_info in &self.types {
11261072
let variable_name = &variable_info[0];
11271073
let variable_type = &variable_info[1];
11281074

11291075
if variable_type.is_empty() {
11301076
self.file_contents.push(Cow::Owned(format!(
1131-
" {}Type::None => String::from(\"{}()\"),",
1132-
self.struct_name, self.struct_name
1077+
" None => String::from(\"{}()\"),",
1078+
self.struct_name
11331079
)));
11341080
} else {
1135-
let snake_case_name = &variable_info[2];
1136-
11371081
self.file_contents.push(Cow::Owned(format!(
1138-
" {}Type::{variable_name} => format!(",
1139-
self.struct_name
1082+
" Some({}Union::{variable_name}(item)) => format!(\"{}({{}})\", item.borrow(py).__repr__(py)),",
1083+
self.struct_name, self.struct_name
11401084
)));
1141-
self.file_contents
1142-
.push(Cow::Owned(format!(" \"{}({{}})\",", self.struct_name)));
1143-
1144-
self.file_contents
1145-
.push(Cow::Owned(format!(" self.{snake_case_name}")));
1146-
self.file_contents.push(Cow::Borrowed(" .as_ref()"));
1147-
self.file_contents
1148-
.push(Cow::Borrowed(" .map(|x| x.borrow(py).__repr__(py))"));
1149-
self.file_contents
1150-
.push(Cow::Borrowed(" .unwrap_or_else(crate::none_str),"));
1151-
self.file_contents.push(Cow::Borrowed(" ),"));
11521085
}
11531086
}
11541087

0 commit comments

Comments
 (0)