Skip to content

Commit db80588

Browse files
committed
Add __match_args__ for structs
1 parent 0b9cb3b commit db80588

6 files changed

Lines changed: 88 additions & 13 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[package]
22
name = "rlbot-flatbuffers-py"
3-
version = "0.4.0"
3+
version = "0.4.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"
77
build = "codegen/main.rs"
88
license = "MIT"
99
readme = "README.md"
10-
exclude = [".github", "pytest.py", "rustfmt.toml", ".gitignore", ".gitmodules", "flatc_mac"]
10+
exclude = [".github", "pytest.py", "pybench.py", "rustfmt.toml", ".gitignore", ".gitmodules", "flatc_mac"]
1111
publish = false
1212

1313
[lints.clippy]

codegen/enums.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,6 @@ impl Generator for EnumBindGenerator {
324324
write_str!(self, "");
325325

326326
self.generate_repr_method();
327-
write_str!(self, "");
328-
329327
write_str!(self, "}");
330328
write_str!(self, "");
331329
}

codegen/pyi.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
174174
}
175175
}
176176

177+
if !gen.types.is_empty() {
178+
write_str!(file, "");
179+
write_str!(file, " __match_args__ = (");
180+
181+
for variable_info in &gen.types {
182+
write_fmt!(file, " \"{}\",", variable_info.name);
183+
}
184+
write_str!(file, " )");
185+
}
186+
177187
if gen.types.is_empty() {
178188
write_str!(file, " def __init__(self): ...");
179189
} else {

codegen/structs.rs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{generator::Generator, PythonBindType};
2-
use std::{borrow::Cow, fs, path::Path};
2+
use std::{borrow::Cow, fs, iter::repeat, path::Path};
33

44
#[derive(Debug, PartialEq, Eq)]
55
pub enum InnerVecType {
@@ -388,7 +388,10 @@ impl StructBindGenerator {
388388
return;
389389
}
390390

391-
write_str!(self, " #[allow(unused_variables)]");
391+
if !self.frozen_needs_py {
392+
write_str!(self, " #[allow(unused_variables)]");
393+
}
394+
392395
write_str!(self, " pub fn __repr__(&self, py: Python) -> String {");
393396
write_str!(self, " format!(");
394397

@@ -496,6 +499,47 @@ impl StructBindGenerator {
496499
write_str!(self, " }");
497500
}
498501

502+
fn generate_long_match_args(&mut self) {
503+
write_str!(self, " #[classattr]");
504+
write_str!(
505+
self,
506+
" fn __match_args__(py: Python) -> Bound<pyo3::types::PyTuple> {"
507+
);
508+
write_str!(self, " pyo3::types::PyTuple::new_bound(py, [");
509+
510+
for variable_info in &self.types {
511+
write_fmt!(self, " \"{}\",", variable_info.name);
512+
}
513+
514+
write_str!(self, " ])");
515+
write_str!(self, " }");
516+
}
517+
518+
fn generate_match_args(&mut self) {
519+
if self.types.is_empty() {
520+
return;
521+
}
522+
523+
if self.types.len() > 12 {
524+
self.generate_long_match_args();
525+
return;
526+
}
527+
528+
let sig_parts: Vec<_> = repeat("&'static str").take(self.types.len()).collect();
529+
let sig = sig_parts.join(", ");
530+
531+
write_str!(self, " #[classattr]");
532+
write_fmt!(self, " fn __match_args__() -> ({sig},) {{",);
533+
write_str!(self, " (");
534+
535+
for variable_info in &self.types {
536+
write_fmt!(self, " \"{}\",", variable_info.name);
537+
}
538+
539+
write_str!(self, " )");
540+
write_str!(self, " }");
541+
}
542+
499543
fn generate_pack_method(&mut self) {
500544
write_str!(
501545
self,
@@ -939,6 +983,9 @@ impl Generator for StructBindGenerator {
939983
self.generate_repr_method();
940984
write_str!(self, "");
941985

986+
self.generate_match_args();
987+
write_str!(self, "");
988+
942989
self.generate_pack_method();
943990
write_str!(self, "");
944991

pytest.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,19 @@ def random_script_config():
4949
dgs = DesiredGameState(
5050
game_info_state=DesiredGameInfoState(game_speed=2, end_match=Bool())
5151
)
52-
dgs.game_info_state.world_gravity_z = Float(-650)
53-
dgs.game_info_state.end_match.val = True
52+
53+
match dgs.game_info_state:
54+
case DesiredGameInfoState():
55+
dgs.game_info_state.world_gravity_z = Float(-650)
56+
case _:
57+
assert False
58+
59+
match dgs.game_info_state.end_match:
60+
case Bool(val):
61+
dgs.game_info_state.end_match.val = not val
62+
case _:
63+
assert False
64+
5465
dgs.console_commands = [ConsoleCommand("dump_items")]
5566
dgs.ball_state = DesiredBallState()
5667

@@ -88,7 +99,14 @@ def random_script_config():
8899
print(comm.content.decode("utf-8"))
89100
print()
90101

91-
print(hash(AirState.Dodging))
102+
air_state = AirState.Dodging
103+
print(hash(air_state))
104+
105+
match air_state:
106+
case AirState.Dodging:
107+
pass
108+
case _:
109+
assert False
92110

93111
try:
94112
AirState(8)
@@ -117,12 +135,14 @@ def random_script_config():
117135
renderPolyLine = RenderMessage(
118136
PolyLine3D(
119137
[Vector3() for _ in range(2048)],
120-
)
138+
Color(255),
139+
),
121140
)
122141

123142
match renderPolyLine.variety.item:
124-
case PolyLine3D():
125-
assert len(renderPolyLine.variety.item.points) == 2048
143+
case PolyLine3D(points, clr):
144+
assert len(points) == 2048
145+
assert clr.a == 255
126146
case _:
127147
assert False
128148

0 commit comments

Comments
 (0)