From 154cb8243b4212fddf73b3b3c48b5f5e8a712876 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 24 Jun 2020 13:34:24 +0200 Subject: Be more explicit about absolute paths at various places --- crates/paths/src/lib.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'crates/paths') 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 for AbsPathBuf { } } +impl AsRef for AbsPathBuf { + fn as_ref(&self) -> &AbsPath { + self.as_path() + } +} + impl TryFrom for AbsPathBuf { type Error = PathBuf; fn try_from(path_buf: PathBuf) -> Result { @@ -45,9 +51,19 @@ impl TryFrom<&str> for AbsPathBuf { } } +impl PartialEq for AbsPathBuf { + fn eq(&self, other: &AbsPath) -> bool { + self.as_path() == other + } +} + impl AbsPathBuf { + pub fn assert(path: PathBuf) -> AbsPathBuf { + AbsPathBuf::try_from(path) + .unwrap_or_else(|path| panic!("expected absolute path, got {}", path.display())) + } pub fn as_path(&self) -> &AbsPath { - AbsPath::new_unchecked(self.0.as_path()) + AbsPath::assert(self.0.as_path()) } pub fn pop(&mut self) -> bool { self.0.pop() @@ -77,15 +93,19 @@ impl<'a> TryFrom<&'a Path> for &'a AbsPath { if !path.is_absolute() { return Err(path); } - Ok(AbsPath::new_unchecked(path)) + Ok(AbsPath::assert(path)) } } impl AbsPath { - fn new_unchecked(path: &Path) -> &AbsPath { + pub fn assert(path: &Path) -> &AbsPath { + assert!(path.is_absolute()); unsafe { &*(path as *const Path as *const AbsPath) } } + pub fn parent(&self) -> Option<&AbsPath> { + self.0.parent().map(AbsPath::assert) + } pub fn join(&self, path: impl AsRef) -> AbsPathBuf { self.as_ref().join(path).try_into().unwrap() } -- cgit v1.2.3