-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
add file_prefix method to std::path #85166
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dtolnay (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
This comment has been minimized.
This comment has been minimized.
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.
Thanks for the PR. I am on board with adding a method like this. Please update the implementation and open a new tracking issue.
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.
Ping from triage: |
@JohnCSimon I have already marked all of the comments as resolved...? Is there some other way of marking the changes as resolved? |
library/std/src/path.rs
Outdated
pub fn file_prefix(&self) -> Option<&OsStr> { | ||
self.file_name() | ||
.map(split_file_at_dot) | ||
.and_then(|(before, after)| if before.is_empty() { after } else { Some(before) }) |
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 first branch on this line (when before
is empty) has no test coverage. What is this case for?
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.
Good point. I had it in there for the case where the filename was a single .
or empty, but this is inherently handled by file_name
already, so we never actually try and split on .
for these cases. I've removed the branch and all tests pass. I also added another test case.
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.
Looks good. Thanks!
@bors r+ |
📌 Commit 51cf318 has been approved by |
☀️ Test successful - checks-actions |
Hello, I just wanted to inquire about this specific function. According to the above, it was merged and should (if I'm understanding this correctly) be available as of rust v1.56.0. However, that doesn't appear to be the case, since as of rust 1.58.1, cargo will not compile code with that function, claiming it's not found:
Just wondering if perhaps I'm simply misunderstanding when this function will be available? Thank you for your time. |
It's not stable yet: #86319 |
Thanks for the quick response! The original author seems unsure what he's supposed to do next with regards to making it stable:
He may need some assistance/guidance in that regard 🙂 |
add file_prefix method to std::path This is an initial implementation of `std::path::Path::file_prefix`. It is effectively a "left" variant of the existing [`file_stem`](https://doc.rust-lang.org/std/path/struct.Path.html#method.file_stem) method. An illustration of the difference is ```rust use std::path::Path; let path = Path::new("foo.tar.gz"); assert_eq!(path.file_stem(), Some("foo.tar")); assert_eq!(path.file_prefix(), Some("foo")); ``` In my own development, I generally find I almost always want the prefix, rather than the stem, so I thought it might be best to suggest it's addition to libstd. Of course, as this is my first contribution, I expect there is probably more work that needs to be done. Additionally, if the libstd team feel this isn't appropriate then so be it. There has been some [discussion about this on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/file_lstem/near/238076313) and a user there suggested I open a PR to see whether someone in the libstd team thinks it is worth pursuing.
This is an initial implementation of
std::path::Path::file_prefix
. It is effectively a "left" variant of the existingfile_stem
method. An illustration of the difference isIn my own development, I generally find I almost always want the prefix, rather than the stem, so I thought it might be best to suggest it's addition to libstd.
Of course, as this is my first contribution, I expect there is probably more work that needs to be done. Additionally, if the libstd team feel this isn't appropriate then so be it.
There has been some discussion about this on Zulip and a user there suggested I open a PR to see whether someone in the libstd team thinks it is worth pursuing.