-
Notifications
You must be signed in to change notification settings - Fork 763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pybytes specialization slices #4442
Conversation
Thinking about this a bit further; we already have |
It's probably ok to go this route and not have That said, the compiler error currently doesn't suggest that. Maybe we can suggest rustc to improve the diagnostic here: error[E0277]: `&u8` cannot be converted to a Python object
--> test.rs:10:43
|
10 | let obj = takes_into_pyobject(x);
| ------------------- ^ the trait `conversion::IntoPyObject<'_>` is not implemented for `&u8`
| |
| required by a bound introduced by this call
|
= note: `IntoPyObject` is automatically implemented by the `#[pyclass]` macro
= note: if you do not wish to have a corresponding Python type, implement it manually
= note: if you do not own `&u8` you can perform a manual conversion to one of the types in `pyo3::types::*`
= help: the trait `conversion::IntoPyObject<'_>` is implemented for `u8`
= help: for that trait implementation, expected `u8`, found `&u8` |
Very interesting approach! I think not providing Maybe we could combine this approach with #4424. That way we have the fast and efficient |
Agreed, we need the
This turned out to be a fantastic suggestion; and running with it managed to do one step further. I had to split the private method into two, That lets me restore Now I think we have all the implementations we want, and everything safely uses This has consumed far too much of my spare brain cycles in the last week, but it's been fun 😂 If you're happy with how this looks, I think this might now be good to merge? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Woo Hoo 🥳 Very clever using another trait to specialize on references and referring to their owned type.
This has consumed far too much of my spare brain cycles in the last week, but it's been fun 😂
Thank you very much for putting in that time. I think now we landed on a pretty good solution that we can make good use of the future. I think borrowed_sequence_into_pyobject
will come in handy for a few more impls on reference types.
If you're happy with how this looks, I think this might now be good to merge?
I think so too 🎉
This is yet another attempt to optimise #4417. This time, I was able to achieve near-zero performance abstraction over the top of
PyBytes::new
.The realisation was that the
&[T]
andCow<[T]>
implementations were dependent on having a new implementationimpl IntoPyObject for &u8
. If we remove that implementation, then we can instead have specialized implementations directly for&[u8]
andCow<[u8]>
which don't conflict with the blanket implementation for&[T]
orCow<[T]>
.The downside of this approach is that we permanently lose the ability to implement
IntoPyObject for &u8
. But personally I don't see that as much of a problem; we didn't have that implementation before. This also prevents us from supporting sequences likeVec<&u8>
, but again I sort of find that type weird and it doesn't bother me too much if we ask users to do something special with it themselves.