pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

impl WrappingCastTo<isize><HashCallbackOutput> for u128

Open tudorcebere opened this issue 3 years ago • 2 comments

🐛 Bug Reports

<warning - Rust newbie> Hi pyo3 community! Love your library!

Basically, I would like to export the __hash__ implementation from rust to python and to return an u128 hash out of it.

A snippet to reproduce the bug:

[pyproto]
impl PyObjectProtocol for MyStruct {
    fn __hash__(&self) -> u128 {
         0 as u128
    }

But it looks like the trait to do this is not implemented for u128. I could take a stab to implement it if it's something not too hardcore in the internals of the library.

🌍 Environment

  • Your operating system and version: Ubuntu 20:04
  • Your python version: 3.7
  • How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?: conda env
  • Your Rust version (rustc --version): rustc 1.51.0 (2fd73fabe 2021-03-23)
  • Your PyO3 version: 0.13.2
  • Have you tried using latest PyO3 main (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")?: yes

💥 Reproducing

Try to return an u128 from a __hash__ implementation.

tudorcebere avatar May 11 '21 15:05 tudorcebere

I don't think this is a bug - Python's hashes can never be 128-bit wide, so it doesn't make sense to allow 128-bit hash returns from __hash__. Implementing WrappingCastTo would throw away half the available bits of the hash, which is not the correct operation for hashes.

birkenfeld avatar May 11 '21 16:05 birkenfeld

Agreed, this is not something which I think we should implement. I think we could potentially add documentation to HashCallbackOutput to note that only types up to 64-bit can be used. Perhaps worth waiting until after #[pyproto] is merged into #[pymethods] in case the implementation changes a lot.

davidhewitt avatar May 24 '21 07:05 davidhewitt