- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
          Add Path::has_trailing_sep and related methods
          #142506
        
          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
Conversation
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
39ebcb7    to
    1df54e4      
    Compare
  
    
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
9547ab7    to
    c5f456e      
    Compare
  
    
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
c5f456e    to
    f2e3481      
    Compare
  
    | @rustbot author | 
| Reminder, once the PR becomes ready for a review, use  | 
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
| Now that I know the workaround for the  There are a few edge cases, namely removing the trailing separator from  | 
f2e3481    to
    95dfe2b      
    Compare
  
    
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
95dfe2b    to
    9b6cd12      
    Compare
  
    | @rustbot ready | 
22c4da0    to
    840b3a0      
    Compare
  
    | ☔ The latest upstream changes (presumably #145334) made this pull request unmergeable. Please resolve the merge conflicts. | 
| /// assert_eq!(Path::new("dir/").trim_trailing_sep().as_os_str(), OsStr::new("dir")); | ||
| /// assert_eq!(Path::new("dir").trim_trailing_sep().as_os_str(), OsStr::new("dir")); | ||
| /// assert_eq!(Path::new("/").trim_trailing_sep().as_os_str(), OsStr::new("/")); | ||
| /// assert_eq!(Path::new("//").trim_trailing_sep().as_os_str(), OsStr::new("//")); | 
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.
Is this correct? I would have thought that since // is the same as /, this would trim // to / (but feel free to correct me).
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 idea here is that removing any number of /s would not affect the final path, and so, the path is left as-is. Essentially, any sequence of separators is treated as a single separator for the logic.
I could change it so that it returns / here but it felt better to be conservative: the logic is that trailing separators are removed if doing so would result in the same path without a trailing separator. Since there will always be trailing separators here, it doesn't make a difference to remove any. If your goal is "cleaning up" the path, then normalize and normalize_lexically exist instead.
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.
Also another way to word this: x.trim_trailing_sep().as_os_str() == x.as_os_str() and x.trim_trailing_sep().has_trailing_sep() are equivalent.
(Note that the as_os_str is required since the trailing separator is ignored when comparing paths.)
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.
This makes sense to me, but I'll ask for libs-api feedback just in 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.
A bit of an aside but I'd just to note this from the POSIX spec:
If a pathname begins with two successive <slash> characters, the first component following the leading <slash> characters may be interpreted in an implementation-defined manner, although more than two leading characters shall be treated as a single character.
So the path \\ can be different from the path \ (although \\\ is the same as \), I do not know that this is used by any platform (maybe cygwin?) so in practice \\ will be the same as \ on supported platforms, as far as I'm aware.
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.
Hmm, that is very strange. It doesn't seem to really follow from the way Rust treats paths, though, since in this case we would have to genuinely treat / and // as separate paths and we do not.
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.
Well on the one hand the way rust treats paths already diverges from the spec by making a trialling \ essentially invisible (which has_trailing_sep and trim_trailing_sep seeks to rectify).  On the other hand, as I said, I'm not sure that \\ makes any practical difference for the platforms we support seeing as they will all treat it the same as \.
Additional note: I implemented std::path::absolute to conform with the POSIX the spec but I don't feel especially strongly about that other than it's nice to have the same behaviour across platforms (including future platforms) where it's possible.
840b3a0    to
    41c47ad      
    Compare
  
    
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
41c47ad    to
    af3123d      
    Compare
  
    af3123d    to
    1552a71      
    Compare
  
    
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
1552a71    to
    5fb1684      
    Compare
  
    
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
| ☔ The latest upstream changes (presumably #146185) made this pull request unmergeable. Please resolve the merge conflicts. | 
5fb1684    to
    38bd3ff      
    Compare
  
    
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
38bd3ff    to
    7ce8e28      
    Compare
  
    | This PR was rebased onto a different master 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. | 
| I take it from the libs-api meeting notes that the current behaviour is fine, therefore this seems good to go. @bors r+ | 
Rollup of 6 pull requests Successful merges: - #142506 (Add `Path::has_trailing_sep` and related methods) - #146886 (Add repr(align(2)) to RcInner and ArcInner) - #147166 (several small `proc_macro` cleanups) - #147172 (bootstrap: build bootstrap docs with in-tree rustdoc) - #147181 (cg_llvm: Replace enum `MetadataType` with a list of `MetadataKindId` constants) - #147187 (remove unnecessary test directives) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of #142506 - clarfonthey:path-trailing-sep, r=joboet Add `Path::has_trailing_sep` and related methods Implements rust-lang/libs-team#335. Tracking issue: #142503 Notable differences from ACP: * `trim_trailing_sep` was added to `Path` since it felt reasonable to ensure that the inverse operation was available. * Per suggestion of `@kennytm,` added `push_trailing_sep` and `pop_trailing_sep` to `PathBuf` in addition to `set_trailing_sep`. This also updates some of the docs on various `Path` methods to use the term "trailing separator" instead of "trailing slash" for consistency.
…oboet Add `Path::has_trailing_sep` and related methods Implements rust-lang/libs-team#335. Tracking issue: rust-lang#142503 Notable differences from ACP: * `trim_trailing_sep` was added to `Path` since it felt reasonable to ensure that the inverse operation was available. * Per suggestion of `@kennytm,` added `push_trailing_sep` and `pop_trailing_sep` to `PathBuf` in addition to `set_trailing_sep`. This also updates some of the docs on various `Path` methods to use the term "trailing separator" instead of "trailing slash" for consistency.
Implements rust-lang/libs-team#335.
Tracking issue: #142503
Notable differences from ACP:
trim_trailing_sepwas added toPathsince it felt reasonable to ensure that the inverse operation was available.push_trailing_sepandpop_trailing_septoPathBufin addition toset_trailing_sep.This also updates some of the docs on various
Pathmethods to use the term "trailing separator" instead of "trailing slash" for consistency.