Skip to content

Commit

Permalink
Auto merge of #16697 - regexident:relpath-to-relpathbuf, r=Veykril
Browse files Browse the repository at this point in the history
Add `to_path_buf()` method for `RelPath`

There seems to be no ergonomic way to obtain a `RelPathBuf` from a corresponding `&RelPath` at the moment, making the latter sort of a dead end.

The `AbsPath` type provides the following:

```rust
impl AbsPath {
    // ...

    /// Equivalent of [`Path::to_path_buf`] for `AbsPath`.
    pub fn to_path_buf(&self) -> AbsPathBuf {
        AbsPathBuf::try_from(self.0.to_path_buf()).unwrap()
    }

    // ...
}
```

So I took the liberty of adding a corresponding equivalent for `RelPath:

```rust
impl RelPath {
    // ...

    /// Equivalent of [`Path::to_path_buf`] for `RelPath`.
    pub fn to_path_buf(&self) -> RelPathBuf {
        RelPathBuf::try_from(self.0.to_path_buf()).unwrap()
    }

    // ...
}
```

(the change is motivated by an outside use of the `ra_ap_paths` crate that would benefit from being able to use `RelPath` and `AbsPath` over `Path`)
  • Loading branch information
bors committed Feb 27, 2024
2 parents a41cec9 + 0005794 commit 0ac05c0
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/paths/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ impl RelPath {
pub fn new_unchecked(path: &Path) -> &RelPath {
unsafe { &*(path as *const Path as *const RelPath) }
}

/// Equivalent of [`Path::to_path_buf`] for `RelPath`.
pub fn to_path_buf(&self) -> RelPathBuf {
RelPathBuf::try_from(self.0.to_path_buf()).unwrap()
}
}

/// Taken from <https://github.com/rust-lang/cargo/blob/79c769c3d7b4c2cf6a93781575b7f592ef974255/src/cargo/util/paths.rs#L60-L85>
Expand Down

0 comments on commit 0ac05c0

Please sign in to comment.