#![cfg(any(doc, all(unix, feature = "std")))]
use std::env;
use std::io;
#[cfg(not(doc))]
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};
pub trait PosixPathExt: Sealed {
fn posix_absolute(&self) -> io::Result<PathBuf>;
}
impl PosixPathExt for Path {
fn posix_absolute(&self) -> io::Result<PathBuf> {
let mut components = self.strip_prefix(".").unwrap_or(self).components();
let path_os = self.as_os_str().as_bytes();
let mut normalized = if self.is_absolute() {
if path_os.starts_with(b"//") && !path_os.starts_with(b"///") {
components.next();
PathBuf::from("//")
} else {
PathBuf::new()
}
} else {
env::current_dir()?
};
normalized.extend(components);
if path_os.ends_with(b"/") {
normalized.push("");
}
Ok(normalized)
}
}
mod private {
pub trait Sealed {}
impl Sealed for std::path::Path {}
}
use private::Sealed;