Closed xkortex closed 3 years ago
Good idea!
I'm not intimately familiar with PyO3 myself. Basically I learned it as I went with along with these bindings, and only the parts I needed, plus it was a major version ago. So I can't say exactly why you're getting the errors you're getting. But playing around with it a bit, this simplified version seems to work:
#[getter]
fn name(&self) -> &str {
"blake3"
}
fn copy(&self) -> Blake3Hasher {
Blake3Hasher {
rust_hasher: self.rust_hasher.clone(),
}
}
On the Rust side of things, the &self
as opposed to &mut self
indicates that only read access to the hasher is required. In this particular case, since Python doesn't really have a concept of shared vs mutable/unique borrows, I don't think it makes a difference to how the generated binding code actually behaves. But we'd definitely want to avoid unnecessary &mut
borrows in a regular Rust codebase, so it's probably a good idea here too.
To your question about thread safety, Rust's story here is pretty clean: If you have a &T
or a &mut T
, you are guaranteed that no other thread could possibly mutate that T
while the reference you're holding is alive. Or at least that it would require unsafe code and trigger undefined behavior to do so, and that the results would definitely not be your fault. (This papers over the possibility that T
might contain something like a std::sync::Mutex
, which could allow for safe "interior mutability", but for plain-old-data types like ours this is the whole story.) My understanding is that PyO3's job is kind of difficult in this respect, since it's hard to guarantee that no other Python threads are holding a reference to something if you release the GIL. But for our purposes that's PyO3's problem, as we're allowed to write code assuming Rust's usual guarantees are respected.
I'd like to implement a
copy()
method and aname
attribute (which just returns'blake3'
). This would bring its interface in line with the otherhashlib
functions. I think this might do it, but I am insufficiently rusty 🦀 , so I was hoping you could take a look before I PR it.This failed and I can't figure out why.
This seems to work:
Thanks,