about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-02-27 18:19:43 +0000
committerbors <bors@rust-lang.org>2024-02-27 18:19:43 +0000
commit0ac05c05271f31c43d31017cbd288e8737a0edb0 (patch)
treeff67bdc97009b2222764b4c92c5b063f2430274e
parenta41cec9f7c6dbbabdc9134879a640ceadd3a516d (diff)
parent000579477067d43787617c5e44cc02a7246e5cc8 (diff)
downloadrust-0ac05c05271f31c43d31017cbd288e8737a0edb0.tar.gz
rust-0ac05c05271f31c43d31017cbd288e8737a0edb0.zip
Auto merge of #16697 - regexident:relpath-to-relpathbuf, r=Veykril
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`)
-rw-r--r--crates/paths/src/lib.rs5
1 files changed, 5 insertions, 0 deletions
diff --git a/crates/paths/src/lib.rs b/crates/paths/src/lib.rs
index db705a7b69e..a63d251c20d 100644
--- a/crates/paths/src/lib.rs
+++ b/crates/paths/src/lib.rs
@@ -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>