-
Notifications
You must be signed in to change notification settings - Fork 640
Remove dependency on os_string_truncate nightly feature
#176
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7cb8608
Remove `os_string_truncate` usage from `path::normalize`
matthew-e-brown 3c99160
Remove `os_string_truncate` from features lists
matthew-e-brown 95e6e36
Replace `.into()` with more explicit `PathBuf::from()`
matthew-e-brown 7ef4c31
Fix typo in comment
matthew-e-brown File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we keep the old check here? This is 100% subjective, but I personally find it a bit more readable.
Another 110% purely subjective thing is that I think the comments could be a bit shorter. This may be because I wrote the original code and/or because I know the involved stdlib functions well, but I think the old comments were "sufficient" at least. The new ones are way better of course, but I think there's a balance in between that could be struck. I think I need to clarify again: This is purely subjective so please disagree. 😅
(I'll wait for you to respond before merging this.)
That said, ironically your new code is shorter than the old version, despite moving a temporary object around (Good job, optimizing compiler! 😄): https://godbolt.org/z/cT5Gvzb36
The missing bit is the
check_public_boundarycall which is part of thetruncateimplementation if you check it out. It checks if the given index is part of a WTF8 character boundary. I think it's okay for us to not do that check. Our truncation length comes straight from the parent path length.Uh oh!
There was an error while loading. Please reload this page.
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.
The reason I changed the comparison was to differentiate between
OsStrlengths and.as_encoded_bytes()lengths. The old version comparedp.as_os_str().as_encoded_bytes().len()toroot_len = res.as_os_str().len(): one with and one without the call to.as_encoded_bytes()first. Can we be sure that the two lengths are equivalent? The documentation forOsStrandOsStringwere kind of confusing to me on this topic.I went down a bit of a rabbit hole with this...
OsStr::len()specifically states that it does not return the byte-length of the string as represented on the underlying OS, but instead returns the length of the underlying in-Rust container; the strings may be stored in a different manner to make conversions more efficient. It summarizes the supposedlen-related oddities by saying that, "this value is simply useful for passing to other methods, likeOsString::with_capacity." It calls back toOsString's introduction for more information.OsString's introduction, then, notes that, "on Windows, strings are stores as sequences of 16-bit values." This note almost seems to imply that anOsStringcould be stored as such, and that it may therefore have a length different than its byte-length; but the section very quickly corrects that anOsStringis still "actually stored as a sequence of 8-bit values."The next section on capacity of an
OsStringfurther clarifies thatOsStringcapacity (and therefore length) uses a combination of "units of UTF-8 bytes" and "units of bytes in an unspecified encoding."So...
OsStr::len()makes a point (a whole paragraph's worth!) of noting that.len()is not necessarily the same as the number of bytes, and that the type-level docs should be checked; but then the type-level docs explain that, no,OsStrings' lengths are always bytes anyways!In this particular case, the
root_lenbeing compared to will always have been obtained through.as_os_str().len()right afterres.push(OsStr::new(MAIN_SEPARATOR_STR))—which, according toPathBuf's docs, should always truncate the path to only ever be thatMAIN_SEPARATOR_STR, meaning it should always now be either/or\... so it shouldn't even matter anyways, since both those cases should always have the same "native-length" and byte-length... And, in my testing, I couldn't come up with anOsStrwhose.len()and.as_encoded_bytes().len()didn't match (though I didn't test any ill-formed Unicode or anything like that).But, with the
OsStr::len()'s documentation (combined with my lack of experience working with Win32,wchar_t, etc.) making as much of a point as it did about the length, I just couldn't shake the feeling that there was some edge-case where the difference mattered.So... to avoid that headache, I figured it would be easier (and possibly even, more semantically correct?) to just make sure to only compare one
OsStr::len()withOsStr::len, and to keep the.as_encoded_bytes().len()length only for when it was time to actually deal with bytes. This is what myNB:comment about "encoded lengths" was talking about. Doing it that way meant that I needed.as_os_str().len()for the check, but not for the truncate anymore; so mapping the option made less sense thanlet Some'ing the whole parent chunk again.If none of that makes any sense or holds any water, then... I'm not particularly attached to either approach 😅 We can swap it back if you'd prefer. You probably know more than I do about the potential pitfalls of wide-chars, WTF8, and
OsStrings, so please let me know if I'm missing anything obvious 🙂Though, I did notice when coming back to this that the check can probably be
>instead of>=, since there isn't much need to truncate a string to lengthnif its length is already exactlyn...Oh yeah, they definitely could be 😄 I think that's just a result of my particular coding style. I can try and trim them down a bit. Though, I myself am not sure which ones I'd get rid of or shorten (since they're just sort of "my voice," so to speak). How would you write them? 🙂
Uh oh!
There was an error while loading. Please reload this page.
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.
Here's how I'd do it: 078230f
I'm not concerned about
.len()VS.as_encoded_bytes().len(), as it's the same thing under the hood. Basically, I consider it a theoretical, not practical difference. I do know that some people really don't enjoy such hot takes. 😅 I don't expect this part of the stdlib to change in the future anymore though. WTF8 is just an overall good solution.But I did try to address your concern as you're genuinely correct about it. I did it in the opposite way though: By making everything consistently use
.as_encoded_bytes().len(), this should not result in a mismatch anymore, right?