I'm using Py03 to build a python module in Rust. I have a class in Rust which accepts a PyAny
to refer to an object in Python. As part of the hash function for the rust class, I want to use the Python ID for this object in the hash function in Rust so I can deduplicate the rust class if the same Python object is referenced in multiple versions of the Rust class. I can see the python ID in the PyAny
object in Rust, but can't figure out how to get it into a plain number that I can pass to the hasher.
For example, I have the following in Rust:
#[pyclass]
pub struct MyClass {
obj: Option<Py<PyAny>>,
}
#[pymethods]
impl MyClass {
#[new]
fn new(obj: Option<Py<PyAny>>) -> Self {
if obj.is_some() {
println!("Obj: {:?}", obj.as_ref());
}
Self { obj }
}
}
Then, I can run in Python:
obj = [1,2,3,4]
print(hex(id(obj)))
# '0x103da9100'
MyClass(obj)
# Obj: Some(Py(0x103da9100))
Both Python and Rust are showing the same number for the ID, which is great, but how can I get this number 0x103da9100
into a Rust variable? It looks like PyAny
is just a tuple struct, so I tried the following but Rust complains that the fields of PyAny
are private:
let obj_id = obj?.0;