Do more traits need to be implemented for pyo3 types #3467
Unanswered
AdamWRichardson
asked this question in
Questions
Replies: 1 comment 5 replies
-
The problem with a lot of Rust traits is that they assume infallibility, whereas basically all Python code can fail at runtime. This rules out For the case of struct HashedObject { obj: PyObject, hash: usize }
impl HashedObject {
fn new(obj: &PyAny) -> PyResult<Self> {
Ok(Self { obj: obj.into(), hash: obj.hash()? as usize })
}
}
impl Hash for HashedObject {
fn hash<H: Hasher>(&self, state: &mut H) {
self.hash.hash(state);
}
} |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Firstly thanks for all the great work on pyo3! I'm still quite new to using it but so far it's been very useful for my projects. I've come across the following situation a couple of time however and I'm not sure of the best way forward.
The context is that I'm building a library in rust and want to expose some of the internals to python, that way it's not purely a rust or python library which I'm sure is a very common thing to do. In the rust library I have a generic struct which implements a trait and I thought the best way to expose this to python would be through the new type pattern - the code is analogous to the following:
My first question is: is this the best way to do this? And then secondly this doesn't compile because
Default
is not implemented for many (if not all) types in pyo3. There are other similar problems to this, the generic forStruct
is basically over the keys and values in an internalHashMap
so I require the key to implementHash
which again is not something that is super obvious how to do. Could anyone help please?!Beta Was this translation helpful? Give feedback.
All reactions