1 //! Thin wrappers around `std::path`, distinguishing between absolute and
4 convert::{TryFrom, TryInto},
6 path::{Component, Path, PathBuf},
9 #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
10 pub struct AbsPathBuf(PathBuf);
12 impl From<AbsPathBuf> for PathBuf {
13 fn from(AbsPathBuf(path_buf): AbsPathBuf) -> PathBuf {
18 impl ops::Deref for AbsPathBuf {
19 type Target = AbsPath;
20 fn deref(&self) -> &AbsPath {
25 impl AsRef<Path> for AbsPathBuf {
26 fn as_ref(&self) -> &Path {
31 impl TryFrom<PathBuf> for AbsPathBuf {
33 fn try_from(path_buf: PathBuf) -> Result<AbsPathBuf, PathBuf> {
34 if !path_buf.is_absolute() {
37 Ok(AbsPathBuf(path_buf))
41 impl TryFrom<&str> for AbsPathBuf {
43 fn try_from(path: &str) -> Result<AbsPathBuf, PathBuf> {
44 AbsPathBuf::try_from(PathBuf::from(path))
49 pub fn canonicalized(path: &Path) -> io::Result<AbsPathBuf> {
50 path.canonicalize().map(|it| AbsPathBuf::try_from(it).unwrap())
52 pub fn as_path(&self) -> &AbsPath {
53 AbsPath::new_unchecked(self.0.as_path())
55 pub fn pop(&mut self) -> bool {
60 #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
62 pub struct AbsPath(Path);
64 impl ops::Deref for AbsPath {
66 fn deref(&self) -> &Path {
71 impl AsRef<Path> for AbsPath {
72 fn as_ref(&self) -> &Path {
77 impl<'a> TryFrom<&'a Path> for &'a AbsPath {
78 type Error = &'a Path;
79 fn try_from(path: &'a Path) -> Result<&'a AbsPath, &'a Path> {
80 if !path.is_absolute() {
83 Ok(AbsPath::new_unchecked(path))
88 fn new_unchecked(path: &Path) -> &AbsPath {
89 unsafe { &*(path as *const Path as *const AbsPath) }
92 pub fn join(&self, path: impl AsRef<Path>) -> AbsPathBuf {
93 self.as_ref().join(path).try_into().unwrap()
95 pub fn normalize(&self) -> AbsPathBuf {
96 AbsPathBuf(normalize_path(&self.0))
100 // https://github.com/rust-lang/cargo/blob/79c769c3d7b4c2cf6a93781575b7f592ef974255/src/cargo/util/paths.rs#L60-L85
101 fn normalize_path(path: &Path) -> PathBuf {
102 let mut components = path.components().peekable();
103 let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
105 PathBuf::from(c.as_os_str())
110 for component in components {
112 Component::Prefix(..) => unreachable!(),
113 Component::RootDir => {
114 ret.push(component.as_os_str());
116 Component::CurDir => {}
117 Component::ParentDir => {
120 Component::Normal(c) => {