diff options
author | Aleksey Kladov <[email protected]> | 2020-06-24 12:34:24 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-06-24 13:39:34 +0100 |
commit | 154cb8243b4212fddf73b3b3c48b5f5e8a712876 (patch) | |
tree | f4ec79c17c8a9432b2d2cfc615ce8b7205d76ed7 /crates/paths | |
parent | dff62def2ea31e7eb0b010c35d60cd53318eab80 (diff) |
Be more explicit about absolute paths at various places
Diffstat (limited to 'crates/paths')
-rw-r--r-- | crates/paths/src/lib.rs | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/crates/paths/src/lib.rs b/crates/paths/src/lib.rs index 45b19c45a..1b259682d 100644 --- a/crates/paths/src/lib.rs +++ b/crates/paths/src/lib.rs | |||
@@ -28,6 +28,12 @@ impl AsRef<Path> for AbsPathBuf { | |||
28 | } | 28 | } |
29 | } | 29 | } |
30 | 30 | ||
31 | impl AsRef<AbsPath> for AbsPathBuf { | ||
32 | fn as_ref(&self) -> &AbsPath { | ||
33 | self.as_path() | ||
34 | } | ||
35 | } | ||
36 | |||
31 | impl TryFrom<PathBuf> for AbsPathBuf { | 37 | impl TryFrom<PathBuf> for AbsPathBuf { |
32 | type Error = PathBuf; | 38 | type Error = PathBuf; |
33 | fn try_from(path_buf: PathBuf) -> Result<AbsPathBuf, PathBuf> { | 39 | fn try_from(path_buf: PathBuf) -> Result<AbsPathBuf, PathBuf> { |
@@ -45,9 +51,19 @@ impl TryFrom<&str> for AbsPathBuf { | |||
45 | } | 51 | } |
46 | } | 52 | } |
47 | 53 | ||
54 | impl PartialEq<AbsPath> for AbsPathBuf { | ||
55 | fn eq(&self, other: &AbsPath) -> bool { | ||
56 | self.as_path() == other | ||
57 | } | ||
58 | } | ||
59 | |||
48 | impl AbsPathBuf { | 60 | impl AbsPathBuf { |
61 | pub fn assert(path: PathBuf) -> AbsPathBuf { | ||
62 | AbsPathBuf::try_from(path) | ||
63 | .unwrap_or_else(|path| panic!("expected absolute path, got {}", path.display())) | ||
64 | } | ||
49 | pub fn as_path(&self) -> &AbsPath { | 65 | pub fn as_path(&self) -> &AbsPath { |
50 | AbsPath::new_unchecked(self.0.as_path()) | 66 | AbsPath::assert(self.0.as_path()) |
51 | } | 67 | } |
52 | pub fn pop(&mut self) -> bool { | 68 | pub fn pop(&mut self) -> bool { |
53 | self.0.pop() | 69 | self.0.pop() |
@@ -77,15 +93,19 @@ impl<'a> TryFrom<&'a Path> for &'a AbsPath { | |||
77 | if !path.is_absolute() { | 93 | if !path.is_absolute() { |
78 | return Err(path); | 94 | return Err(path); |
79 | } | 95 | } |
80 | Ok(AbsPath::new_unchecked(path)) | 96 | Ok(AbsPath::assert(path)) |
81 | } | 97 | } |
82 | } | 98 | } |
83 | 99 | ||
84 | impl AbsPath { | 100 | impl AbsPath { |
85 | fn new_unchecked(path: &Path) -> &AbsPath { | 101 | pub fn assert(path: &Path) -> &AbsPath { |
102 | assert!(path.is_absolute()); | ||
86 | unsafe { &*(path as *const Path as *const AbsPath) } | 103 | unsafe { &*(path as *const Path as *const AbsPath) } |
87 | } | 104 | } |
88 | 105 | ||
106 | pub fn parent(&self) -> Option<&AbsPath> { | ||
107 | self.0.parent().map(AbsPath::assert) | ||
108 | } | ||
89 | pub fn join(&self, path: impl AsRef<Path>) -> AbsPathBuf { | 109 | pub fn join(&self, path: impl AsRef<Path>) -> AbsPathBuf { |
90 | self.as_ref().join(path).try_into().unwrap() | 110 | self.as_ref().join(path).try_into().unwrap() |
91 | } | 111 | } |