PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Apache License 2.0
12.06k stars 744 forks source link

refactor: use `ptr_from_ref` and ptr `.cast()` #4240

Closed davidhewitt closed 3 months ago

davidhewitt commented 3 months ago

Typically these days I like to use x.cast::<Y>() instead of x as *const Y to perform pointer casting (and in many contexts it's also convenient to let type inference work on just x.cast(), which reads a lot nicer than x as *const _). It also avoids const <-> mut casting by accident.

I just discovered ptr::from_ref and am now similarly excited to replace &x as *const X with ptr_from_ref(&x).

... so this PR is just a few tidy-ups across the codebase!

davidhewitt commented 3 months ago

Yes, as well as cast_mut there's ptr_from_mut, which I didn't try here (I think it's less relevant) but might also be handy a little.