|
1 | 1 | use crate::{generator::Generator, PythonBindType}; |
2 | | -use std::{borrow::Cow, fs, path::Path}; |
| 2 | +use std::{borrow::Cow, fs, iter::repeat, path::Path}; |
3 | 3 |
|
4 | 4 | #[derive(Debug, PartialEq, Eq)] |
5 | 5 | pub enum InnerVecType { |
@@ -388,7 +388,10 @@ impl StructBindGenerator { |
388 | 388 | return; |
389 | 389 | } |
390 | 390 |
|
391 | | - write_str!(self, " #[allow(unused_variables)]"); |
| 391 | + if !self.frozen_needs_py { |
| 392 | + write_str!(self, " #[allow(unused_variables)]"); |
| 393 | + } |
| 394 | + |
392 | 395 | write_str!(self, " pub fn __repr__(&self, py: Python) -> String {"); |
393 | 396 | write_str!(self, " format!("); |
394 | 397 |
|
@@ -496,6 +499,47 @@ impl StructBindGenerator { |
496 | 499 | write_str!(self, " }"); |
497 | 500 | } |
498 | 501 |
|
| 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 | + |
499 | 543 | fn generate_pack_method(&mut self) { |
500 | 544 | write_str!( |
501 | 545 | self, |
@@ -939,6 +983,9 @@ impl Generator for StructBindGenerator { |
939 | 983 | self.generate_repr_method(); |
940 | 984 | write_str!(self, ""); |
941 | 985 |
|
| 986 | + self.generate_match_args(); |
| 987 | + write_str!(self, ""); |
| 988 | + |
942 | 989 | self.generate_pack_method(); |
943 | 990 | write_str!(self, ""); |
944 | 991 |
|
|
0 commit comments