diff --git a/Cargo.lock b/Cargo.lock index 8f6e362..2f47b92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,7 +11,7 @@ dependencies = [ "attribute-derive-macro", "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.72", ] [[package]] @@ -27,7 +27,7 @@ dependencies = [ "proc-macro2", "quote", "quote-use", - "syn 2.0.68", + "syn 2.0.72", ] [[package]] @@ -62,7 +62,7 @@ checksum = "62d671cc41a825ebabc75757b62d3d168c577f9149b2d49ece1dad1f72119d25" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.72", ] [[package]] @@ -92,7 +92,7 @@ checksum = "13a1bcfb855c1f340d5913ab542e36f25a1c56f57de79022928297632435dec2" dependencies = [ "attribute-derive", "quote", - "syn 2.0.68", + "syn 2.0.72", ] [[package]] @@ -136,9 +136,9 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "portable-atomic" -version = "1.6.0" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" +checksum = "da544ee218f0d287a911e9c99a39a8c9bc8fcad3cb8db5959940044ecfc67265" [[package]] name = "proc-macro-error" @@ -186,9 +186,9 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.22.0" +version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1962a33ed2a201c637fc14a4e0fd4e06e6edfdeee6a5fede0dab55507ad74cf7" +checksum = "831e8e819a138c36e212f3af3fd9eeffed6bf1510a805af35b0edee5ffa59433" dependencies = [ "cfg-if", "indoc", @@ -204,9 +204,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.22.0" +version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab7164b2202753bd33afc7f90a10355a719aa973d1f94502c50d06f3488bc420" +checksum = "1e8730e591b14492a8945cdff32f089250b05f5accecf74aeddf9e8272ce1fa8" dependencies = [ "once_cell", "target-lexicon", @@ -214,9 +214,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.22.0" +version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6424906ca49013c0829c5c1ed405e20e2da2dc78b82d198564880a704e6a7b7" +checksum = "5e97e919d2df92eb88ca80a037969f44e5e70356559654962cbb3316d00300c6" dependencies = [ "libc", "pyo3-build-config", @@ -224,27 +224,27 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.22.0" +version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82b2f19e153122d64afd8ce7aaa72f06a00f52e34e1d1e74b6d71baea396460a" +checksum = "eb57983022ad41f9e683a599f2fd13c3664d7063a3ac5714cae4b7bee7d3f206" dependencies = [ "proc-macro2", "pyo3-macros-backend", "quote", - "syn 2.0.68", + "syn 2.0.72", ] [[package]] name = "pyo3-macros-backend" -version = "0.22.0" +version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd698c04cac17cf0fe63d47790ab311b8b25542f5cb976b65c374035c50f1eef" +checksum = "ec480c0c51ddec81019531705acac51bcdbeae563557c982aa8263bb96880372" dependencies = [ "heck", "proc-macro2", "pyo3-build-config", "quote", - "syn 2.0.68", + "syn 2.0.72", ] [[package]] @@ -264,7 +264,7 @@ checksum = "a7b5abe3fe82fdeeb93f44d66a7b444dedf2e4827defb0a8e69c437b2de2ef94" dependencies = [ "quote", "quote-use-macros", - "syn 2.0.68", + "syn 2.0.72", ] [[package]] @@ -276,12 +276,12 @@ dependencies = [ "derive-where", "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.72", ] [[package]] name = "rlbot-flatbuffers-py" -version = "0.5.0" +version = "0.5.1" dependencies = [ "flatbuffers", "get-size", @@ -306,22 +306,22 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" [[package]] name = "serde" -version = "1.0.203" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.203" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.72", ] [[package]] @@ -342,9 +342,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.68" +version = "2.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" dependencies = [ "proc-macro2", "quote", @@ -353,9 +353,9 @@ dependencies = [ [[package]] name = "target-lexicon" -version = "0.12.14" +version = "0.12.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1fc403891a21bcfb7c37834ba66a547a8f402146eba7265b5a6d88059c9ff2f" +checksum = "4873307b7c257eddcb50c9bedf158eb669578359fb28428bef438fec8e6ba7c2" [[package]] name = "unicode-ident" diff --git a/Cargo.toml b/Cargo.toml index bc5d2f6..e541c8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rlbot-flatbuffers-py" -version = "0.5.0" +version = "0.5.1" edition = "2021" description = "A Python module implemented in Rust for serializing and deserializing RLBot's flatbuffers" repository = "https://github.com/VirxEC/rlbot_flatbuffers_py" diff --git a/codegen/pyi.rs b/codegen/pyi.rs index 13a1b42..caad0b8 100644 --- a/codegen/pyi.rs +++ b/codegen/pyi.rs @@ -60,9 +60,12 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> { write_fmt!(file, " item: Optional[{union_str}]"); write_str!(file, ""); + write_str!(file, " def __new__("); + write_fmt!(file, " cls, item: Optional[{union_str}] = None"); + write_str!(file, " ): ..."); write_str!(file, " def __init__("); write_fmt!(file, " self, item: Optional[{union_str}] = None"); - write_str!(file, " ): ..."); + write_str!(file, " ): ...\n"); } PythonBindType::Enum(gen) => { for variable_info in &gen.types { @@ -76,13 +79,14 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> { } write_str!(file, ""); + write_str!(file, " def __new__(cls, value: int = 0): ..."); write_str!(file, " def __init__(self, value: int = 0):"); write_str!(file, " \"\"\""); write_str!( file, " :raises ValueError: If the `value` is not a valid enum value" ); - write_str!(file, " \"\"\"\n"); + write_str!(file, " \"\"\""); write_str!(file, " def __int__(self) -> int: ..."); write_fmt!(file, " def __eq__(self, other: {type_name}) -> bool: ..."); write_str!(file, " def __hash__(self) -> str: ..."); @@ -192,36 +196,43 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> { write_str!(file, " def __init__(self): ..."); } else { write_str!(file, ""); - write_str!(file, " def __init__("); - write_str!(file, " self,"); - - for (variable_info, python_type) in gen.types.iter().zip(python_types) { - let variable_name = variable_info.name.as_str(); - - let default_value = match variable_info.raw_type.as_str() { - "bool" => Cow::Borrowed("False"), - "i32" | "u32" | "f32" | "u8" => Cow::Borrowed("0"), - "String" => Cow::Borrowed("\"\""), - "Vec" => Cow::Borrowed("b\"\""), - t => { - if python_type.starts_with("Optional") || t.starts_with("Option<") { - Cow::Borrowed("None") - } else if t.starts_with("Vec<") { - Cow::Borrowed("[]") - } else if t.starts_with("Box<") { - let inner_type = - t.trim_start_matches("Box<").trim_end_matches('>').trim_end_matches('T'); - Cow::Owned(format!("{inner_type}()")) - } else { - Cow::Owned(format!("{}()", t.trim_end_matches('T'))) + + let inits = [("new", "cls"), ("init", "self")]; + + for (func, first_arg) in inits { + write_fmt!(file, " def __{func}__("); + write_fmt!(file, " {first_arg},"); + + for (variable_info, python_type) in gen.types.iter().zip(&python_types) { + let variable_name = variable_info.name.as_str(); + + let default_value = match variable_info.raw_type.as_str() { + "bool" => Cow::Borrowed("False"), + "i32" | "u32" | "f32" | "u8" => Cow::Borrowed("0"), + "String" => Cow::Borrowed("\"\""), + "Vec" => Cow::Borrowed("b\"\""), + t => { + if python_type.starts_with("Optional") || t.starts_with("Option<") { + Cow::Borrowed("None") + } else if t.starts_with("Vec<") { + Cow::Borrowed("[]") + } else if t.starts_with("Box<") { + let inner_type = t + .trim_start_matches("Box<") + .trim_end_matches('>') + .trim_end_matches('T'); + Cow::Owned(format!("{inner_type}()")) + } else { + Cow::Owned(format!("{}()", t.trim_end_matches('T'))) + } } - } - }; + }; - write_fmt!(file, " {variable_name}: {python_type} = {default_value},"); - } + write_fmt!(file, " {variable_name}: {python_type} = {default_value},"); + } - write_str!(file, " ): ..."); + write_str!(file, " ): ..."); + } } write_str!(file, " def pack(self) -> bytes: ...");