Skip to content

Commit 1224bd1

Browse files
committed
Fix union constructors
1 parent 0f53e83 commit 1224bd1

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rlbot-flatbuffers-py"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
edition = "2021"
55
description = "A Python module implemented in Rust for serializing and deserializing RLBot's flatbuffers"
66
repository = "https://github.com/VirxEC/rlbot-flatbuffers-py"

build.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl PythonBindGenerator {
651651
self.write_str(" #[new]");
652652
assert!(u8::try_from(self.types.len()).is_ok());
653653

654-
let mut signature_parts = vec!["item_type=Default::default()".to_string()];
654+
let mut signature_parts = Vec::new();
655655

656656
for variable_info in &self.types {
657657
let variable_type = &variable_info[1];
@@ -668,7 +668,6 @@ impl PythonBindGenerator {
668668
self.write_string(format!(" #[pyo3(signature = ({}))]", signature_parts.join(", ")));
669669
self.write_str(" pub fn new(");
670670

671-
self.write_string(format!(" item_type: {}Type,", self.struct_name));
672671
for variable_info in &self.types {
673672
let variable_type = &variable_info[1];
674673

@@ -684,9 +683,33 @@ impl PythonBindGenerator {
684683
}
685684

686685
self.write_str(" ) -> Self {");
687-
self.write_str(" Self {");
688686

687+
self.write_string(format!(" let mut item_type = {}Type::default();", self.struct_name));
688+
for variable_info in &self.types {
689+
let variable_name = &variable_info[0];
690+
let variable_type = &variable_info[1];
691+
692+
if variable_type.is_empty() {
693+
continue;
694+
}
695+
696+
let snake_case_name = &variable_info[2];
697+
698+
self.file_contents.push(Cow::Borrowed(""));
699+
self.file_contents.push(Cow::Owned(format!(
700+
" if {snake_case_name}.is_some() {{",
701+
)));
702+
self.file_contents.push(Cow::Owned(format!(
703+
" item_type = {}Type::{variable_name};",self.struct_name
704+
)));
705+
self.file_contents.push(Cow::Borrowed(" }"));
706+
}
707+
708+
709+
self.write_str("");
710+
self.write_str(" Self {");
689711
self.write_str(" item_type,");
712+
690713
for variable_info in &self.types {
691714
let variable_type = &variable_info[1];
692715

0 commit comments

Comments
 (0)