Skip to content

Commit 295c754

Browse files
committed
Update to latest flatbuffers spec
1 parent b203746 commit 295c754

6 files changed

Lines changed: 184 additions & 133 deletions

File tree

Cargo.lock

Lines changed: 23 additions & 23 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.3.0"
3+
version = "0.3.1"
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: 75 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ impl PythonBindGenerator {
274274
"Vec<Py<super::{}>>",
275275
variable_type.trim_start_matches("Vec<").trim_end_matches("T>")
276276
);
277+
} else if variable_type.starts_with("Vec<") && variable_type.ends_with('>') {
278+
variable_type = String::from("Py<PyBytes>");
277279
} else if variable_type.starts_with("Box<") && variable_type.ends_with('>') {
278280
variable_type = format!(
279281
"Py<super::{}>",
@@ -315,8 +317,11 @@ impl PythonBindGenerator {
315317
let variable_type = &variable_info[1];
316318

317319
if variable_type.starts_with("Vec<") {
318-
self.file_contents
319-
.push(Cow::Owned(format!(" {variable_name}: Vec::new(),")));
320+
self.file_contents.push(Cow::Owned(if variable_type == "Vec<u8>" {
321+
format!(" {variable_name}: PyBytes::new_bound(py, &[]).unbind(),")
322+
} else {
323+
format!(" {variable_name}: Vec::new(),")
324+
}));
320325
} else if variable_type.starts_with("Option<") {
321326
self.file_contents
322327
.push(Cow::Owned(format!(" {variable_name}: None,")));
@@ -495,19 +500,22 @@ impl PythonBindGenerator {
495500
let variable_type = variable_info[1].as_str();
496501

497502
if variable_type.starts_with("Vec<") {
498-
let inner_type = variable_type.trim_start_matches("Vec<").trim_end_matches('>');
499-
if Self::BASE_TYPES.contains(&inner_type) {
500-
self.file_contents
501-
.push(Cow::Owned(format!(" {variable_name}: flat_t.{variable_name},")));
503+
self.file_contents
504+
.push(Cow::Owned(if variable_type == "Vec<u8>" {
505+
format!(" {variable_name}: PyBytes::new_bound(py, &flat_t.{variable_name}).unbind(),")
502506
} else {
503-
self.file_contents.push(Cow::Owned(format!(
507+
format!(
504508
" {variable_name}: flat_t.{variable_name}.into_iter().map(|x| x.into_gil(py)).collect(),",
505-
)));
506-
}
509+
)
510+
}));
507511
} else if variable_type.starts_with("Option<") {
508-
self.file_contents.push(Cow::Owned(format!(
509-
" {variable_name}: flat_t.{variable_name}.map(|x| x.into_gil(py)),",
510-
)));
512+
self.file_contents.push(Cow::Owned(
513+
if variable_type.trim_start_matches("Option<").trim_end_matches('>') == "String" {
514+
format!(" {variable_name}: flat_t.{variable_name},")
515+
} else {
516+
format!(" {variable_name}: flat_t.{variable_name}.map(|x| x.into_gil(py)),")
517+
},
518+
));
511519
} else if variable_type.starts_with("Box<") || variable_type.ends_with('T') {
512520
self.file_contents.push(Cow::Owned(format!(
513521
" {variable_name}: flat_t.{variable_name}.into_gil(py),",
@@ -642,19 +650,25 @@ impl PythonBindGenerator {
642650
let variable_type = variable_info[1].as_str();
643651

644652
if variable_type.starts_with("Vec<") {
645-
let inner_type = variable_type.trim_start_matches("Vec<").trim_end_matches('>');
646-
if Self::BASE_TYPES.contains(&inner_type) {
647-
self.file_contents
648-
.push(Cow::Owned(format!(" {variable_name}: py_type.{variable_name},")));
653+
self.file_contents.push(Cow::Owned(if variable_type == "Vec<u8>" {
654+
format!(
655+
" {variable_name}: py_type.{variable_name}.as_bytes(py).to_vec(),"
656+
)
649657
} else {
650-
self.file_contents.push(Cow::Owned(format!(
658+
format!(
651659
" {variable_name}: py_type.{variable_name}.iter().map(|x| x.into_gil(py)).collect(),",
652-
)));
653-
}
660+
)
661+
}));
654662
} else if variable_type.starts_with("Option<") {
655-
self.file_contents.push(Cow::Owned(format!(
656-
" {variable_name}: py_type.{variable_name}.as_ref().map(|x| x.into_gil(py)),",
657-
)));
663+
self.file_contents.push(Cow::Owned(
664+
if variable_type.trim_start_matches("Option<").trim_end_matches('>') == "String" {
665+
format!(" {variable_name}: py_type.{variable_name}.clone(),")
666+
} else {
667+
format!(
668+
" {variable_name}: py_type.{variable_name}.as_ref().map(|x| x.into_gil(py)),"
669+
)
670+
},
671+
));
658672
} else if variable_type == "String" {
659673
self.file_contents.push(Cow::Owned(format!(
660674
" {variable_name}: py_type.{variable_name}.clone(),",
@@ -702,19 +716,25 @@ impl PythonBindGenerator {
702716
let variable_type = variable_info[1].as_str();
703717

704718
if variable_type.starts_with("Vec<") {
705-
let inner_type = variable_type.trim_start_matches("Vec<").trim_end_matches('>');
706-
if Self::BASE_TYPES.contains(&inner_type) {
707-
self.file_contents
708-
.push(Cow::Owned(format!(" {variable_name}: borrow.{variable_name},")));
719+
if variable_type == "Vec<u8>" {
720+
self.file_contents.push(Cow::Owned(format!(
721+
" {variable_name}: borrow.{variable_name}.as_bytes(py).to_vec(),"
722+
)));
709723
} else {
710724
self.file_contents.push(Cow::Owned(format!(
711725
" {variable_name}: borrow.{variable_name}.iter().map(|x| x.into_gil(py)).collect(),",
712726
)));
713727
}
714728
} else if variable_type.starts_with("Option<") {
715-
self.file_contents.push(Cow::Owned(format!(
716-
" {variable_name}: borrow.{variable_name}.as_ref().map(|x| x.into_gil(py)),",
717-
)));
729+
self.file_contents.push(Cow::Owned(
730+
if variable_type.trim_start_matches("Option<").trim_end_matches('>') == "String" {
731+
format!(" {variable_name}: borrow.{variable_name}.clone(),")
732+
} else {
733+
format!(
734+
" {variable_name}: borrow.{variable_name}.as_ref().map(|x| x.into_gil(py)),"
735+
)
736+
},
737+
));
718738
} else if variable_type == "String" {
719739
self.file_contents.push(Cow::Owned(format!(
720740
" {variable_name}: borrow.{variable_name}.clone(),",
@@ -845,6 +865,8 @@ impl PythonBindGenerator {
845865
&& (variable_type.starts_with("Box<") || variable_type.ends_with('T'))
846866
{
847867
signature_parts.push(format!("{variable_name}=crate::get_py_default()"));
868+
} else if variable_type == "Vec<u8>" {
869+
signature_parts.push(format!("{variable_name}=crate::get_empty_pybytes()"));
848870
} else {
849871
signature_parts.push(format!("{variable_name}=Default::default()"));
850872
}
@@ -866,6 +888,8 @@ impl PythonBindGenerator {
866888
"Vec<Py<super::{}>>",
867889
variable_type.trim_start_matches("Vec<").trim_end_matches("T>")
868890
);
891+
} else if variable_type == "Vec<u8>" {
892+
variable_type = String::from("Py<PyBytes>");
869893
} else if variable_type.starts_with("Box<") && variable_type.ends_with('>') {
870894
variable_type = format!(
871895
"Py<super::{}>",
@@ -978,7 +1002,7 @@ impl PythonBindGenerator {
9781002
return;
9791003
}
9801004

981-
self.write_str("#[allow(unused_variables)]");
1005+
self.write_str(" #[allow(unused_variables)]");
9821006
self.write_str(" pub fn __repr__(&self, py: Python) -> String {");
9831007
self.write_str(" format!(");
9841008

@@ -991,6 +1015,8 @@ impl PythonBindGenerator {
9911015

9921016
if variable_type == "String" {
9931017
format!("{variable_name}={{:?}}")
1018+
} else if variable_type == "Vec<u8>" {
1019+
format!("{variable_name}=bytes([{{}}])")
9941020
} else if variable_type.starts_with("Vec<") {
9951021
format!("{variable_name}=[{{}}]")
9961022
} else {
@@ -1015,16 +1041,28 @@ impl PythonBindGenerator {
10151041
self.file_contents
10161042
.push(Cow::Owned(format!(" self.{variable_name}")));
10171043
self.file_contents.push(Cow::Borrowed(" .as_ref()"));
1018-
self.file_contents
1019-
.push(Cow::Borrowed(" .map(|x| x.borrow(py).__repr__(py))"));
1044+
if Self::BASE_TYPES.into_iter().any(|t| variable_type.contains(t)) {
1045+
self.file_contents
1046+
.push(Cow::Borrowed(" .map(ToString::to_string)"));
1047+
} else {
1048+
self.file_contents
1049+
.push(Cow::Borrowed(" .map(|x| x.borrow(py).__repr__(py))"));
1050+
}
10201051
self.file_contents
10211052
.push(Cow::Borrowed(" .unwrap_or_else(crate::none_str),"));
10221053
} else if variable_type.starts_with("Vec<") {
10231054
self.file_contents
10241055
.push(Cow::Owned(format!(" self.{variable_name}")));
1025-
self.file_contents.push(Cow::Borrowed(" .iter()"));
1026-
self.file_contents
1027-
.push(Cow::Borrowed(" .map(|x| x.borrow(py).__repr__(py))"));
1056+
if Self::BASE_TYPES.into_iter().any(|t| variable_type.contains(t)) {
1057+
self.file_contents.push(Cow::Borrowed(" .as_bytes(py)"));
1058+
self.file_contents.push(Cow::Borrowed(" .iter()"));
1059+
self.file_contents
1060+
.push(Cow::Borrowed(" .map(ToString::to_string)"));
1061+
} else {
1062+
self.file_contents.push(Cow::Borrowed(" .iter()"));
1063+
self.file_contents
1064+
.push(Cow::Borrowed(" .map(|x| x.borrow(py).__repr__(py))"));
1065+
}
10281066
self.file_contents
10291067
.push(Cow::Borrowed(" .collect::<Vec<String>>()"));
10301068
self.file_contents.push(Cow::Borrowed(" .join(\", \"),"));
@@ -1196,6 +1234,7 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
11961234
("f32", "float"),
11971235
("String", "str"),
11981236
("u8", "int"),
1237+
("Vec<u8>", "bytes"),
11991238
];
12001239

12011240
for (_, type_name, types) in type_data {
@@ -1326,6 +1365,7 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
13261365
"bool" => Cow::Borrowed("False"),
13271366
"i32" | "u32" | "f32" | "u8" => Cow::Borrowed("0"),
13281367
"String" => Cow::Borrowed("\"\""),
1368+
"Vec<u8>" => Cow::Borrowed("b\"\""),
13291369
t => {
13301370
if t.starts_with("Vec<") {
13311371
Cow::Borrowed("[]")

flatbuffers-schema

0 commit comments

Comments
 (0)