aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-06-22 14:04:16 +0100
committerGitHub <[email protected]>2020-06-22 14:04:16 +0100
commita783d2a1baf7b4a33c6d4a04860998a2af5e1d9a (patch)
tree0af9a258328a702193086136ea15dd337d12a0bf /crates
parentd8842e89e9053b62c081d2995cbf43b8fd5c51b2 (diff)
parent7c9f97ecb836e449d6d4e20b73c9697b2b63476e (diff)
Merge #4983
4983: Add paths::RelPath[Buf] r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/paths/src/lib.rs74
1 files changed, 74 insertions, 0 deletions
diff --git a/crates/paths/src/lib.rs b/crates/paths/src/lib.rs
index 190c50913..32267f2e6 100644
--- a/crates/paths/src/lib.rs
+++ b/crates/paths/src/lib.rs
@@ -95,6 +95,80 @@ impl AbsPath {
95 pub fn normalize(&self) -> AbsPathBuf { 95 pub fn normalize(&self) -> AbsPathBuf {
96 AbsPathBuf(normalize_path(&self.0)) 96 AbsPathBuf(normalize_path(&self.0))
97 } 97 }
98 pub fn to_path_buf(&self) -> AbsPathBuf {
99 AbsPathBuf::try_from(self.0.to_path_buf()).unwrap()
100 }
101 pub fn strip_prefix(&self, base: &AbsPath) -> Option<&RelPath> {
102 self.0.strip_prefix(base).ok().map(RelPath::new_unchecked)
103 }
104}
105
106#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
107pub struct RelPathBuf(PathBuf);
108
109impl From<RelPathBuf> for PathBuf {
110 fn from(RelPathBuf(path_buf): RelPathBuf) -> PathBuf {
111 path_buf
112 }
113}
114
115impl ops::Deref for RelPathBuf {
116 type Target = RelPath;
117 fn deref(&self) -> &RelPath {
118 self.as_path()
119 }
120}
121
122impl AsRef<Path> for RelPathBuf {
123 fn as_ref(&self) -> &Path {
124 self.0.as_path()
125 }
126}
127
128impl TryFrom<PathBuf> for RelPathBuf {
129 type Error = PathBuf;
130 fn try_from(path_buf: PathBuf) -> Result<RelPathBuf, PathBuf> {
131 if !path_buf.is_relative() {
132 return Err(path_buf);
133 }
134 Ok(RelPathBuf(path_buf))
135 }
136}
137
138impl TryFrom<&str> for RelPathBuf {
139 type Error = PathBuf;
140 fn try_from(path: &str) -> Result<RelPathBuf, PathBuf> {
141 RelPathBuf::try_from(PathBuf::from(path))
142 }
143}
144
145impl RelPathBuf {
146 pub fn as_path(&self) -> &RelPath {
147 RelPath::new_unchecked(self.0.as_path())
148 }
149}
150
151#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
152#[repr(transparent)]
153pub struct RelPath(Path);
154
155impl ops::Deref for RelPath {
156 type Target = Path;
157 fn deref(&self) -> &Path {
158 &self.0
159 }
160}
161
162impl AsRef<Path> for RelPath {
163 fn as_ref(&self) -> &Path {
164 &self.0
165 }
166}
167
168impl RelPath {
169 pub fn new_unchecked(path: &Path) -> &RelPath {
170 unsafe { &*(path as *const Path as *const RelPath) }
171 }
98} 172}
99 173
100// https://github.com/rust-lang/cargo/blob/79c769c3d7b4c2cf6a93781575b7f592ef974255/src/cargo/util/paths.rs#L60-L85 174// https://github.com/rust-lang/cargo/blob/79c769c3d7b4c2cf6a93781575b7f592ef974255/src/cargo/util/paths.rs#L60-L85