From 0855d0aad685353944799dfc8898a4918638b8e4 Mon Sep 17 00:00:00 2001 From: Virx Date: Thu, 20 Feb 2025 13:04:42 -0500 Subject: [PATCH] Fix repr on raw strings Rust 2024 --- Cargo.lock | 18 +++---- Cargo.toml | 4 +- codegen/main.rs | 18 +++---- codegen/pyi.rs | 32 ++++++------- codegen/structs.rs | 117 ++++++++++++++++++++++++++++++++++----------- codegen/unions.rs | 12 +++-- pytest.py | 2 +- src/lib.rs | 9 ++-- 8 files changed, 139 insertions(+), 73 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a6fa1c4..2a28189 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -279,7 +279,7 @@ dependencies = [ [[package]] name = "rlbot_flatbuffers" -version = "0.13.3" +version = "0.13.4" dependencies = [ "flatbuffers", "get-size", @@ -304,18 +304,18 @@ checksum = "f79dfe2d285b0488816f30e700a7438c5a73d816b5b7d3ac72fbc48b0d185e03" [[package]] name = "serde" -version = "1.0.217" +version = "1.0.218" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" +checksum = "e8dfc9d19bdbf6d17e22319da49161d5d0108e4188e8b680aef6299eed22df60" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.217" +version = "1.0.218" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" +checksum = "f09503e191f4e797cb8aac08e9a4a4695c5edf6a2e70e376d961ddd5c969f82b" dependencies = [ "proc-macro2", "quote", @@ -324,9 +324,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.13.2" +version = "1.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" +checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" [[package]] name = "syn" @@ -347,9 +347,9 @@ checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" [[package]] name = "unicode-ident" -version = "1.0.16" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" +checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe" [[package]] name = "unindent" diff --git a/Cargo.toml b/Cargo.toml index 7e87f35..ffd5c5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rlbot_flatbuffers" -version = "0.13.3" -edition = "2021" +version = "0.13.4" +edition = "2024" description = "A Python module implemented in Rust for serializing and deserializing RLBot's flatbuffers" repository = "https://github.com/VirxEC/rlbot_flatbuffers_py" build = "codegen/main.rs" diff --git a/codegen/main.rs b/codegen/main.rs index 8a0170b..4a8e1bc 100644 --- a/codegen/main.rs +++ b/codegen/main.rs @@ -144,25 +144,25 @@ impl PythonBindType { pub fn filename(&self) -> &str { match self { - Self::Struct(gen) => gen.filename(), - Self::Enum(gen) => gen.filename(), - Self::Union(gen) => gen.filename(), + Self::Struct(bind) => bind.filename(), + Self::Enum(bind) => bind.filename(), + Self::Union(bind) => bind.filename(), } } pub fn struct_name(&self) -> &str { match self { - Self::Struct(gen) => gen.struct_name(), - Self::Enum(gen) => gen.struct_name(), - Self::Union(gen) => gen.struct_name(), + Self::Struct(bind) => bind.struct_name(), + Self::Enum(bind) => bind.struct_name(), + Self::Union(bind) => bind.struct_name(), } } pub fn generate(&mut self, filepath: &Path) -> io::Result<()> { match self { - Self::Struct(gen) => gen.generate(filepath), - Self::Enum(gen) => gen.generate(filepath), - Self::Union(gen) => gen.generate(filepath), + Self::Struct(bind) => bind.generate(filepath), + Self::Enum(bind) => bind.generate(filepath), + Self::Union(bind) => bind.generate(filepath), } } } diff --git a/codegen/pyi.rs b/codegen/pyi.rs index 8403c64..82b1cfb 100644 --- a/codegen/pyi.rs +++ b/codegen/pyi.rs @@ -1,7 +1,7 @@ use crate::{ + PythonBindType, generator::Generator, structs::{InnerOptionType, InnerVecType, RustType}, - PythonBindType, }; use std::{borrow::Cow, fs, io}; @@ -49,8 +49,8 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> { write_fmt!(file, "class {type_name}:"); match item { - PythonBindType::Union(gen) => { - let types = gen + PythonBindType::Union(bind) => { + let types = bind .types .iter() .map(|variable_info| variable_info.name.as_str()) @@ -68,8 +68,8 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> { write_fmt!(file, " self, item: {union_str} = {default_value}()"); write_str!(file, " ): ...\n"); } - PythonBindType::Enum(gen) => { - for variable_info in &gen.types { + PythonBindType::Enum(bind) => { + for variable_info in &bind.types { let variable_name = variable_info.name.as_str(); if variable_name == "NONE" { continue; @@ -105,8 +105,8 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> { ); write_str!(file, " def __hash__(self) -> str: ..."); } - PythonBindType::Struct(gen) => { - if let Some(docs) = gen.struct_doc_str.as_ref() { + PythonBindType::Struct(bind) => { + if let Some(docs) = bind.struct_doc_str.as_ref() { write_str!(file, " \"\"\""); for line in docs { @@ -118,7 +118,7 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> { let mut python_types = Vec::new(); - 'outer: for variable_info in &gen.types { + 'outer: for variable_info in &bind.types { let variable_name = variable_info.name.as_str(); let variable_type = variable_info.raw_type.as_str(); @@ -215,11 +215,11 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> { let union_types = type_data .iter() .find_map(|item| match item { - PythonBindType::Union(gen) - if gen.struct_name() == type_name => + PythonBindType::Union(bind) + if bind.struct_name() == type_name => { Some( - gen.types + bind.types .iter() .skip(1) .map(|v| v.name.as_str()) @@ -250,17 +250,17 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> { } } - if !gen.types.is_empty() { + if !bind.types.is_empty() { write_str!(file, ""); write_str!(file, " __match_args__ = ("); - for variable_info in &gen.types { + for variable_info in &bind.types { write_fmt!(file, " \"{}\",", variable_info.name); } write_str!(file, " )"); } - if gen.types.is_empty() { + if bind.types.is_empty() { write_str!(file, " def __init__(self): ..."); } else { write_str!(file, ""); @@ -271,7 +271,7 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> { write_fmt!(file, " def __{func}__("); write_fmt!(file, " {first_arg},"); - for (variable_info, python_type) in gen.types.iter().zip(&python_types) { + for (variable_info, python_type) in bind.types.iter().zip(&python_types) { let variable_name = variable_info.name.as_str(); let default_value = match variable_info.raw_type.as_str() { @@ -315,7 +315,7 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> { write_str!(file, " Serializes this instance into a byte array"); write_str!(file, " \"\"\""); - if !gen.is_frozen { + if !bind.is_frozen { write_str!(file, " def unpack_with(self, data: bytes):"); write_str!(file, " \"\"\""); write_str!(file, " Deserializes the data into this instance\n"); diff --git a/codegen/structs.rs b/codegen/structs.rs index f3b4081..be84174 100644 --- a/codegen/structs.rs +++ b/codegen/structs.rs @@ -1,5 +1,5 @@ -use crate::{generator::Generator, PythonBindType}; -use std::{borrow::Cow, fs, iter::repeat, path::Path}; +use crate::{PythonBindType, generator::Generator}; +use std::{borrow::Cow, fs, iter::repeat_n, path::Path}; #[derive(Debug, PartialEq, Eq)] pub enum InnerVecType { @@ -410,7 +410,10 @@ impl StructBindGenerator { ); } RustType::Box(inner_type) | RustType::Custom(inner_type) => { - write_fmt!(self, " {variable_name}: {variable_name}.unwrap_or_else(|| super::{inner_type}::py_default(py)),"); + write_fmt!( + self, + " {variable_name}: {variable_name}.unwrap_or_else(|| super::{inner_type}::py_default(py))," + ); } RustType::Vec(InnerVecType::U8) => { write_fmt!( @@ -507,7 +510,10 @@ impl StructBindGenerator { InnerVecType::Custom(type_name) => { write_str!(self, " .bind_borrowed(py)"); write_str!(self, " .iter()"); - write_fmt!(self, " .map(|x| x.downcast_into::().unwrap().borrow().__repr__(py))"); + write_fmt!( + self, + " .map(|x| x.downcast_into::().unwrap().borrow().__repr__(py))" + ); } } write_str!(self, " .collect::>()"); @@ -522,7 +528,10 @@ impl StructBindGenerator { write_str!(self, " .map(|i| format!(\"{i:?}\"))"); } InnerOptionType::String => { - write_str!(self, " .map(|i| format!(\"{:?}\", i.to_str(py).unwrap().to_string()))"); + write_str!( + self, + " .map(|i| format!(\"{:?}\", i.to_str(py).unwrap().to_string()))" + ); } _ => { write_str!(self, " .map(|x| x.borrow(py).__repr__(py))"); @@ -554,7 +563,10 @@ impl StructBindGenerator { } } RustType::String => { - write_fmt!(self, " self.{variable_name},"); + write_fmt!( + self, + " self.{variable_name}.bind(py).to_cow().unwrap()," + ); } RustType::Other(_) => { write_fmt!(self, " self.{variable_name}.__repr__(),"); @@ -599,7 +611,7 @@ impl StructBindGenerator { return; } - let sig_parts: Vec<_> = repeat("&'static str").take(self.types.len()).collect(); + let sig_parts: Vec<_> = repeat_n("&'static str", self.types.len()).collect(); let sig = sig_parts.join(", "); write_str!(self, " #[classattr]"); @@ -671,17 +683,29 @@ impl StructBindGenerator { match &variable_info.rust_type { RustType::Vec(InnerVecType::Custom(inner_type)) => { - write_fmt!(self, " crate::update_list::<_, super::{inner_type}>(py, self.{variable_name}.bind_borrowed(py), flat_t.{variable_name});",); + write_fmt!( + self, + " crate::update_list::<_, super::{inner_type}>(py, self.{variable_name}.bind_borrowed(py), flat_t.{variable_name});", + ); } RustType::Vec(InnerVecType::U8) => { - write_fmt!(self, " self.{variable_name} = PyBytes::new(py, &flat_t.{variable_name}).unbind();"); + write_fmt!( + self, + " self.{variable_name} = PyBytes::new(py, &flat_t.{variable_name}).unbind();" + ); } RustType::Option(InnerOptionType::Box, _) => { write_fmt!(self, " match flat_t.{variable_name} {{"); write_str!(self, " Some(x) => {"); write_fmt!(self, " match &mut self.{variable_name} {{"); - write_str!(self, " Some(item) => item.bind_borrowed(py).borrow_mut().unpack_from(py, *x),"); - write_fmt!(self, " None => self.{variable_name} = Some(crate::into_py_from(py, *x)),"); + write_str!( + self, + " Some(item) => item.bind_borrowed(py).borrow_mut().unpack_from(py, *x)," + ); + write_fmt!( + self, + " None => self.{variable_name} = Some(crate::into_py_from(py, *x))," + ); write_str!(self, " }"); write_str!(self, " }"); write_fmt!(self, " None => self.{variable_name} = None,"); @@ -691,21 +715,36 @@ impl StructBindGenerator { write_fmt!(self, " match flat_t.{variable_name} {{"); write_str!(self, " Some(x) => {"); write_fmt!(self, " match &mut self.{variable_name} {{"); - write_str!(self, " Some(item) => item.bind_borrowed(py).borrow_mut().unpack_from(py, x),"); - write_fmt!(self, " None => self.{variable_name} = Some(crate::into_py_from(py, x)),"); + write_str!( + self, + " Some(item) => item.bind_borrowed(py).borrow_mut().unpack_from(py, x)," + ); + write_fmt!( + self, + " None => self.{variable_name} = Some(crate::into_py_from(py, x))," + ); write_str!(self, " }"); write_str!(self, " }"); write_fmt!(self, " None => self.{variable_name} = None,"); write_str!(self, " }"); } RustType::Option(InnerOptionType::String, _) => { - write_fmt!(self, " self.{variable_name} = flat_t.{variable_name}.map(|s| PyString::new(py, &s).unbind());"); + write_fmt!( + self, + " self.{variable_name} = flat_t.{variable_name}.map(|s| PyString::new(py, &s).unbind());" + ); } RustType::Box(_) => { - write_fmt!(self, " self.{variable_name}.bind_borrowed(py).borrow_mut().unpack_from(py, *flat_t.{variable_name});") + write_fmt!( + self, + " self.{variable_name}.bind_borrowed(py).borrow_mut().unpack_from(py, *flat_t.{variable_name});" + ) } RustType::Custom(_) => { - write_fmt!(self, " self.{variable_name}.bind_borrowed(py).borrow_mut().unpack_from(py, flat_t.{variable_name});") + write_fmt!( + self, + " self.{variable_name}.bind_borrowed(py).borrow_mut().unpack_from(py, flat_t.{variable_name});" + ) } RustType::Union(_) => { let conv_str = if variable_info.is_frozen { @@ -727,7 +766,10 @@ impl StructBindGenerator { } RustType::Base(inner_type) => match inner_type.as_str() { "f32" => { - write_fmt!(self, " self.{variable_name} = crate::float_to_py(py, flat_t.{variable_name});"); + write_fmt!( + self, + " self.{variable_name} = crate::float_to_py(py, flat_t.{variable_name});" + ); } _ => { write_fmt!( @@ -999,7 +1041,10 @@ impl Generator for StructBindGenerator { ); } RustType::Option(InnerOptionType::String, _) => { - write_fmt!(self, " {variable_name}: flat_t.{variable_name}.map(|s| PyString::new(py, &s).unbind()),"); + write_fmt!( + self, + " {variable_name}: flat_t.{variable_name}.map(|s| PyString::new(py, &s).unbind())," + ); } RustType::Option(_, _) => { write_fmt!( @@ -1008,17 +1053,29 @@ impl Generator for StructBindGenerator { ); } RustType::Box(_) => { - write_fmt!(self, " {variable_name}: crate::into_py_from(py, *flat_t.{variable_name}),",); + write_fmt!( + self, + " {variable_name}: crate::into_py_from(py, *flat_t.{variable_name}),", + ); } RustType::Union(_) | RustType::Custom(_) => { - write_fmt!(self, " {variable_name}: crate::into_py_from(py, flat_t.{variable_name}),",); + write_fmt!( + self, + " {variable_name}: crate::into_py_from(py, flat_t.{variable_name}),", + ); } RustType::String => { - write_fmt!(self, " {variable_name}: PyString::new(py, &flat_t.{variable_name}).unbind(),"); + write_fmt!( + self, + " {variable_name}: PyString::new(py, &flat_t.{variable_name}).unbind()," + ); } RustType::Base(inner_type) => match inner_type.as_str() { "f32" => { - write_fmt!(self, " {variable_name}: crate::float_to_py(py, flat_t.{variable_name}),"); + write_fmt!( + self, + " {variable_name}: crate::float_to_py(py, flat_t.{variable_name})," + ); } _ => { write_fmt!(self, " {variable_name}: flat_t.{variable_name},"); @@ -1107,10 +1164,16 @@ impl Generator for StructBindGenerator { ); } RustType::Box(_) => { - write_fmt!(self, " {variable_name}: Box::new(crate::from_py_into(py, &py_type.{variable_name})),",); + write_fmt!( + self, + " {variable_name}: Box::new(crate::from_py_into(py, &py_type.{variable_name})),", + ); } RustType::Union(_) | RustType::Custom(_) => { - write_fmt!(self, " {variable_name}: crate::from_py_into(py, &py_type.{variable_name}),",); + write_fmt!( + self, + " {variable_name}: crate::from_py_into(py, &py_type.{variable_name}),", + ); } RustType::String => { write_fmt!( @@ -1121,9 +1184,9 @@ impl Generator for StructBindGenerator { RustType::Base(inner_type) => match inner_type.as_str() { "f32" => { write_fmt!( - self, - " {variable_name}: crate::float_from_py(py, &py_type.{variable_name})," - ); + self, + " {variable_name}: crate::float_from_py(py, &py_type.{variable_name})," + ); } _ => { write_fmt!( diff --git a/codegen/unions.rs b/codegen/unions.rs index 91e9e07..2decc7a 100644 --- a/codegen/unions.rs +++ b/codegen/unions.rs @@ -1,4 +1,4 @@ -use crate::{enums::CustomEnumType, generator::Generator, PythonBindType}; +use crate::{PythonBindType, enums::CustomEnumType, generator::Generator}; use std::{borrow::Cow, fs, path::Path}; pub struct UnionBindGenerator { @@ -170,14 +170,20 @@ impl UnionBindGenerator { " if let {}Union::{variable_name}(item) = &self.item {{", self.struct_name ); - write_fmt!(self, " item.bind_borrowed(py).borrow_mut().unpack_from(py, *flat_item);"); + write_fmt!( + self, + " item.bind_borrowed(py).borrow_mut().unpack_from(py, *flat_item);" + ); write_str!(self, " } else {"); write_fmt!( self, " self.item = {}Union::{variable_name}(", self.struct_name ); - write_fmt!(self, " Py::new(py, super::{variable_name}::from_gil(py, *flat_item)).unwrap()"); + write_fmt!( + self, + " Py::new(py, super::{variable_name}::from_gil(py, *flat_item)).unwrap()" + ); write_str!(self, " );"); write_str!(self, " }"); write_str!(self, " },"); diff --git a/pytest.py b/pytest.py index ad0fa49..59f1c60 100644 --- a/pytest.py +++ b/pytest.py @@ -39,7 +39,7 @@ def random_script_config(): vec2 = Vector3(4, 5, 6) print(vec1 + vec2) - player_info = PlayerInfo(accolades=["MVP", "Hat Trick"]) + player_info = PlayerInfo(name="HELLO", accolades=["MVP", "Hat Trick"]) eval(repr(player_info)) print() diff --git a/src/lib.rs b/src/lib.rs index bee4ca4..70eddd2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ clippy::size_of_in_element_count, clippy::needless_lifetimes, clippy::too_long_first_doc_paragraph, + unsafe_op_in_unsafe_fn, non_snake_case, unused_imports )] @@ -14,7 +15,7 @@ pub mod generated; #[allow(clippy::enum_variant_names, clippy::useless_conversion, unused_imports)] mod python; -use pyo3::{create_exception, exceptions::PyValueError, prelude::*, types::*, PyClass}; +use pyo3::{PyClass, create_exception, exceptions::PyValueError, prelude::*, types::*}; use python::*; use std::{panic::Location, path::MAIN_SEPARATOR}; @@ -127,11 +128,7 @@ pub fn none_str() -> String { #[must_use] #[inline(never)] pub const fn bool_to_str(b: bool) -> &'static str { - if b { - "True" - } else { - "False" - } + if b { "True" } else { "False" } } #[inline(never)]