-
Notifications
You must be signed in to change notification settings - Fork 782
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
[RFC] standardize PyBytes <-> Vec<u8> or &[u8] or Cow<[u8]> #4182
Conversation
CodSpeed Performance ReportMerging #4182 will not alter performanceComparing 🎉 Hooray!
|
Thanks for the PR, and sorry it's taken a little while to get a review! This is definitely a topic with a situation in PyO3 which is currently unsatisfying, see also #2888 with some past discussion in this area. (cc @adamreichold) I think as much as it feels impure, it would be desirable for Regarding the conversion traits we currently also have #4060 for the to-python direction which would reduce the number of to-python traits down to just one. Maybe while we're making changes this is a good moment to explore a solution for this. (cc @Icxolu) I definitely want this case to be better, and I still can't see any solution to implement the desired behaviour other than hanging the implementation off I wonder, can we maybe solve this by adding additional methods to those traits which are effectively private? Basically we make one or more of the types in their signature PyO3-private and then we give a default implementation so that users can't touch it. e.g. trait FromPyObject<'py> {
/* existing stuff ... */
fn extract_bytes(&self, override: pyo3::impl_::Override) -> ... {
// default implementation returns None or fails or similar
}
} where the |
Sounds like Sealed trait methods, just put the |
Yeah, this is kind of problem for every kind of generic (de)serialization currently. The real solution would be
This would definitely be an opportunity to improve the type conversion pit falls together with a new simplified and more powerful conversion mechanism. I think I'm going to rebase #4060 and think a bit how these would interact. |
After rebasing #4060 and giving this a first look, I have some observations about combining these two ideas:
|
I don't entirely follow why this is necessary, though I guess it would become clearer to me in time.
Ah, yes. That might actually be a good thing to help users realise that there's something a bit funny going on. I also sort of wonder if there's a way to exploit the type system to achieve this statically, though that'll be an experiment all on its own.
👍 I'm looking forward to carving out some time to play around with #4060 in detail as soon as we've shipped 0.22 (which I think is mostly waiting on me to rebase stuff...) |
Thanks for all the issue links and overview of the problems with
From a user's perspective this would be a great improvement since it makes the code more readable and whenever there is "just one way of doing X", makes using the said code more ergonomic.
This would also be amazing for the |
When generating
PyBytes
fromVec<u8>
or&[u8]
orCow<[u8]>
somewhat different behaviors are observed:Ideally, I would like to be able to handle all 3 convesions the same way, with either
into_py
and/orto_object
. I'm not 100% what the best approach would be but I took a stab atToPyObject
to showcase how that might work. Would love some feedback on the approach as well as pointers on how to best proceed to addressIntoPy
andFromPyObject
, assuming all this doable.