Skip to content

Commit

Permalink
Implement __hash__ for all classes
Browse files Browse the repository at this point in the history
  • Loading branch information
VirxEC committed May 14, 2024
1 parent 71419d5 commit 38458ef
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rlbot-flatbuffers-py"
version = "0.3.4"
version = "0.3.5"
edition = "2021"
description = "A Python module implemented in Rust for serializing and deserializing RLBot's flatbuffers"
repository = "https://github.com/VirxEC/rlbot-flatbuffers-py"
Expand Down
16 changes: 15 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,12 @@ impl PythonBindGenerator {
self.write_str(" }");
}

fn generate_enum_hash_method(&mut self) {
self.write_str(" pub fn __hash__(&self) -> u64 {");
self.write_str(" crate::hash_u64(*self as u64)");
self.write_str(" }");
}

fn generate_py_methods(&mut self) {
self.write_str("#[pymethods]");
self.write_string(format!("impl {} {{", self.struct_name));
Expand All @@ -1148,6 +1154,11 @@ impl PythonBindGenerator {
self.write_str("");
self.generate_repr_method();

if self.bind_type == PythonBindType::Enum {
self.write_str("");
self.generate_enum_hash_method();
}

if self.bind_type != PythonBindType::Union {
self.write_str("");
self.generate_pack_method();
Expand Down Expand Up @@ -1358,7 +1369,9 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
if is_enum {
file_contents.push(Cow::Borrowed(" def __init__(self, value: int = 0):"));
file_contents.push(Cow::Borrowed(" \"\"\""));
file_contents.push(Cow::Borrowed(" :raises ValueError: If the `value` is not a valid enum value"));
file_contents.push(Cow::Borrowed(
" :raises ValueError: If the `value` is not a valid enum value",
));
file_contents.push(Cow::Borrowed(" \"\"\""));
} else {
file_contents.push(Cow::Borrowed(" def __init__("));
Expand Down Expand Up @@ -1405,6 +1418,7 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result

file_contents.push(Cow::Borrowed(" def __str__(self) -> str: ..."));
file_contents.push(Cow::Borrowed(" def __repr__(self) -> str: ..."));
file_contents.push(Cow::Borrowed(" def __hash__(self) -> str: ..."));

if is_enum {
file_contents.push(Cow::Borrowed(" def __int__(self) -> int: ..."));
Expand Down
6 changes: 6 additions & 0 deletions pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def __add__(self, other):
print(vec1 + vec2)

ready_message = ReadyMessage(True, wants_game_messages=True)
print(hash(ready_message))
print(repr(ready_message))
print(ready_message)
eval(repr(ready_message))
Expand All @@ -27,6 +28,7 @@ def __add__(self, other):
dgs.console_commands = [ConsoleCommand("dump_items")]
dgs.ball_state = DesiredBallState()

print(hash(dgs))
print(repr(dgs))
print(dgs)
eval(repr(dgs))
Expand All @@ -40,18 +42,22 @@ def __add__(self, other):
else:
raise ValueError("Expected Line3D")

print(hash(render_type))
print(repr(render_type))
print(render_type)
eval(repr(render_type))
print()

comm = MatchComm(3, 1, False, "Ready!", b"Hello, world!")
print(hash(comm))
print(repr(comm))
print(comm)
eval(repr(comm))
print(comm.content.decode("utf-8"))
print()

print(hash(AirState.Dodging))

try:
AirState(8)
except ValueError as e:
Expand Down
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ mod python;

use pyo3::{create_exception, exceptions::PyValueError, prelude::*, types::PyBytes, PyClass};
use python::*;
use std::panic::Location;
use std::{
hash::{DefaultHasher, Hash, Hasher},
panic::Location,
};

create_exception!(rlbot_flatbuffers, InvalidFlatbuffer, PyValueError, "Invalid FlatBuffer");

Expand All @@ -25,6 +28,12 @@ pub fn flat_err_to_py(err: flatbuffers::InvalidFlatbuffer) -> PyErr {
InvalidFlatbuffer::new_err(err_msg)
}

pub fn hash_u64(num: u64) -> u64 {
let mut hasher = DefaultHasher::new();
num.hash(&mut hasher);
hasher.finish()
}

pub trait FromGil<T> {
fn from_gil(py: Python, obj: T) -> Self;
}
Expand Down

0 comments on commit 38458ef

Please sign in to comment.