-
Notifications
You must be signed in to change notification settings - Fork 14k
Make ByteStr/ByteString a more-opaque wrapper like OsStr/OsString #146219
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
base: main
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
7704e59 to
1bc5256
Compare
This comment has been minimized.
This comment has been minimized.
1bc5256 to
19e3fa8
Compare
This comment has been minimized.
This comment has been minimized.
19e3fa8 to
1ed1f43
Compare
This comment has been minimized.
This comment has been minimized.
1ed1f43 to
e423893
Compare
This comment has been minimized.
This comment has been minimized.
e423893 to
8bc7cca
Compare
This comment has been minimized.
This comment has been minimized.
8bc7cca to
46c5b9e
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Okay, I truly don't understand this rustdoc error but ByteStr is not present at all in rustdoc's code so I'm going to assume it's unrelated for now. |
|
What an odd one. However, I don't see how this test can possibly be spurious and it does seem related to your changes in a roundabout way. You might've exposed a rustdoc bug, but I haven't really investigated anything deeply. The test fails because apparently we no longer display a "notable impl" tooltip advertising |
|
That is a very strange bug. I will definitely investigate later, but I definitely think that this PR is good enough to review for now. |
|
So, these types were modeled after the equivalents in bstr, which switched from an opaque-type model to a model that largely just has Deref, trait impls, and adds all the relevant methods to extension traits. I'd like to understand better why this seems to be doing the reverse. In particular, why shouldn't most of these inherent methods live on |
|
Not to be rude, but did you read the PR description at all? Because I feel like I went over my justification decently well and it's harder to respond to "I'd like to understand better" when you don't acknowledge any of the things I said to help people understand and why they didn't actually help. |
|
I did, in fact, read the entire PR description before asking that question, and I wouldn't have asked the question if the PR description had answered it. For instance:
That was the intended design of ByteStr/ByteString. Any additional methods can go on
If you add methods to Hence my question: why shouldn't most of these inherent methods live on |
|
Pretty much all of these methods are on
This is just an aside:
I also mention this a bit more on my comment on the tracking issue:
Also, while I admit that asking whether you actually read the description was mostly said in frustration, I was not trying to accuse you of actually not reading the description: the point was that it's not useful to simply ask for more explanation when you don't acknowledge the explanation I did give. Here, it's understandable that you honed in on the (understandably large) part of the explanation that focused on Basically, I see this as a comparison of costs: on one hand, this solution I propose makes user actions on these types generally simpler while requiring additional methods to call more complicated methods directly. On the other, the existing solution makes the implementation simpler while requiring additional methods to call any by-value methods on In my mind, increasing the cost of implementation is justified because we already bear the cost of implementation elsewhere. As mentioned, we basically implement our own versions of I think that it's very reasonable to consider this a compromise over the current state of the Rust compiler and ask whether we really need a stable In that case, these problems would not exist and the implementation would remain simple, although the "issue" would still remain that And ultimately, the final decision to consider is whether we think that |
|
☔ The latest upstream changes (presumably #135634) made this pull request unmergeable. Please resolve the merge conflicts. |
46c5b9e to
672e9cb
Compare
|
Some changes occurred in src/tools/cargo cc @ehuss |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
672e9cb to
47b058c
Compare
This comment has been minimized.
This comment has been minimized.
47b058c to
f9b4ad6
Compare
This comment has been minimized.
This comment has been minimized.
f9b4ad6 to
06592da
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
The job Click to see the possible cause of the failure (guessed by this bot) |
This attempts to resolve the issues I brought up in the tracking issue (#134915) a few weeks ago by making
ByteStrandByteStringinto "more opaque" wrapper types likeOsStrandOsString. I decided to put together a set of methods that are mostly drawn fromOsStrandOsString, but include a few extras. I originally asked if an ACP would be required to do this, but without a response from libs-api and reasonable support from passers-by, I'm just making a PR instead.The main issue with
ByteStris that it's not a "real" string type: they're just wrappers over[u8]andVec<u8>with barely any methods of their own, relying instead onDerefimpls. The inability to move out of deref here prevents methods likeVec::leakfrom being used without much effort, and the fact that the two types aren't linked viaDerefmeans that any extra methods added toByteStrwould have to be added toByteStringtoo.The new wrapper types act more in line with a "real" string type while still allowing easy access to the underlying
[u8]andVec<u8>without any complaining. In particular, this means thatByteStringhas anas_mut_vecmethod that can be executed safely (unlikeString::as_mut_vec) andByteStrhas anas_bytes_mutmethod that can be executed safely. Any methods added toByteStrcan be accessed directly byByteStringdue to the deref implementation, avoiding the need to add new methods to both types.Note that these changes parallel
OsStrandOsStringfor a reason: I was hoping to updateOsStrandOsStringto internally use these types on non-Windows targets. This feels like an obvious case where these types would be useful, and considering how I totally failed due to the issues I mentioned, I decided to at least start with what would be necessary to achieve that and we can build from there.While the trait implementations remain untouched (except
Deref, which now followsByteString -> ByteStr), here is the new set of inherent methods added:You may have also noticed these omitted, which I'm pointing out separately due to a bit of an API complication.
CStrandCStringdecide to provide full access to the UTF-8 error here, butOsStrandOsStringdo not. I decided to go withOsStrandOsString's API because they were simpler, but I personally think that the decision on what to do here should also be paired with one on what to do with the existing API discrepancy as well:I also included these methods on
[u8]andVec<u8>because, currently, a lot of conversions from these types toByteStrandByteStringare not available due to inference issues. Previously, these were resolved by making the actual type constructors public, but that forbids modifying the wrappers in the future, for example, to cache whether the string is valid UTF-8 likeOsStringcurrently does. So:I think that the inference issues blocking all the relevant
AsRef,AsMut, andFromimpls for these types should be seriously considered in terms of the future of this API. I'm avoiding this question for now since I think the changes toDerefwill be needed regardless, but I added these methods to get around some of the issues in the meantime. If you'd like me to change the feature gate to put them under a different one, I can do that too, just to point out the issues here.I would like to also apologise because I was mostly copying examples for half of them, then just got tired of it and copied the documentation for the methods only. We can add examples before stabilisation, and tests are unnecessary due to the fact that these are all shallow methods.
r? libs-api