#[repr(transparent)]pub struct BasePath(/* private fields */);
Expand description
A borrowed path that has a prefix on Windows.
Note that comparison traits such as PartialEq
will compare paths
literally instead of comparing components. The former is more efficient and
easier to use correctly.
Safety
This type should not be used for memory safety, but implementations can
panic if this path is missing a prefix on Windows. A safe new_unchecked
method might be added later that can safely create invalid base paths.
Although this type is annotated with #[repr(transparent)]
, the inner
representation is not stable. Transmuting between this type and any other
causes immediate undefined behavior.
Implementations§
source§impl BasePath
impl BasePath
sourcepub fn new<'a, P>(path: P) -> Result<Cow<'a, Self>>where
P: Into<Cow<'a, Path>>,
pub fn new<'a, P>(path: P) -> Result<Cow<'a, Self>>where P: Into<Cow<'a, Path>>,
Creates a new base path.
On Windows, if path
is missing a prefix, it will be joined to the
current directory.
Errors
Returns an error if reading the current directory fails.
Examples
use std::path::Path;
use normpath::BasePath;
if cfg!(windows) {
let path = Path::new(r"X:\foo\bar");
assert_eq!(path, *BasePath::new(path)?);
assert!(BasePath::new(Path::new(r"foo\bar")).is_ok());
}
sourcepub fn try_new<P>(path: &P) -> Result<&Self, MissingPrefixError>where
P: AsRef<Path> + ?Sized,
pub fn try_new<P>(path: &P) -> Result<&Self, MissingPrefixError>where P: AsRef<Path> + ?Sized,
sourcepub fn as_os_str(&self) -> &OsStr
pub fn as_os_str(&self) -> &OsStr
Returns a reference to the wrapped path as a platform string.
sourcepub fn canonicalize(&self) -> Result<BasePathBuf>
pub fn canonicalize(&self) -> Result<BasePathBuf>
Equivalent to Path::canonicalize
.
sourcepub fn components(&self) -> Components<'_>
pub fn components(&self) -> Components<'_>
Equivalent to Path::components
.
sourcepub fn ends_with<P>(&self, child: P) -> boolwhere
P: AsRef<Path>,
pub fn ends_with<P>(&self, child: P) -> boolwhere P: AsRef<Path>,
Equivalent to Path::ends_with
.
sourcepub fn exists(&self) -> bool
pub fn exists(&self) -> bool
Equivalent to Path::exists
.
sourcepub fn extension(&self) -> Option<&OsStr>
pub fn extension(&self) -> Option<&OsStr>
Equivalent to Path::extension
.
sourcepub fn file_name(&self) -> Option<&OsStr>
pub fn file_name(&self) -> Option<&OsStr>
Equivalent to Path::file_name
.
sourcepub fn file_stem(&self) -> Option<&OsStr>
pub fn file_stem(&self) -> Option<&OsStr>
Equivalent to Path::file_stem
.
sourcepub fn has_root(&self) -> bool
pub fn has_root(&self) -> bool
Equivalent to Path::has_root
.
sourcepub fn is_absolute(&self) -> bool
pub fn is_absolute(&self) -> bool
Equivalent to Path::is_absolute
.
sourcepub fn is_dir(&self) -> bool
pub fn is_dir(&self) -> bool
Equivalent to Path::is_dir
.
sourcepub fn is_file(&self) -> bool
pub fn is_file(&self) -> bool
Equivalent to Path::is_file
.
sourcepub fn is_relative(&self) -> bool
pub fn is_relative(&self) -> bool
Equivalent to Path::is_relative
.
sourcepub fn join<P>(&self, path: P) -> BasePathBufwhere
P: AsRef<Path>,
pub fn join<P>(&self, path: P) -> BasePathBufwhere P: AsRef<Path>,
An improved version of Path::join
that handles more edge cases.
For example, on Windows, leading .
and ..
components of path
will
be normalized if possible. If self
is a verbatim path, it would be
invalid to normalize them later.
You should still call normalize
before parent
to normalize some
additional components.
Examples
use std::path::Path;
use normpath::BasePath;
if cfg!(windows) {
assert_eq!(
Path::new(r"\\?\foo\baz\test.rs"),
BasePath::try_new(r"\\?\foo\bar")
.unwrap()
.join("../baz/test.rs"),
);
}
sourcepub fn metadata(&self) -> Result<Metadata>
pub fn metadata(&self) -> Result<Metadata>
Equivalent to Path::metadata
.
sourcepub fn normalize(&self) -> Result<BasePathBuf>
pub fn normalize(&self) -> Result<BasePathBuf>
Equivalent to PathExt::normalize
.
sourcepub fn normalize_virtually(&self) -> Result<BasePathBuf>
pub fn normalize_virtually(&self) -> Result<BasePathBuf>
Equivalent to PathExt::normalize_virtually
.
sourcepub fn parent(&self) -> Result<Option<&Self>, ParentError>
pub fn parent(&self) -> Result<Option<&Self>, ParentError>
Returns this path without its last component.
Returns Ok(None)
if the last component is Component::RootDir
.
You should usually only call this method on normalized paths. They
will prevent an unexpected path from being returned due to symlinks,
and some .
and ..
components will be normalized.
Errors
Returns an error if the last component is not Component::Normal
or
Component::RootDir
. To ignore this error, use parent_unchecked
.
Examples
use std::path::Path;
use normpath::BasePath;
if cfg!(windows) {
assert_eq!(
Path::new(r"X:\foo"),
BasePath::try_new(r"X:\foo\bar").unwrap().parent()?.unwrap(),
);
}
sourcepub fn parent_unchecked(&self) -> Option<&Self>
pub fn parent_unchecked(&self) -> Option<&Self>
Equivalent to Path::parent
.
It is usually better to use parent
.
Examples
use std::path::Path;
use normpath::BasePath;
if cfg!(windows) {
assert_eq!(
Path::new(r"X:\foo"),
BasePath::try_new(r"X:\foo\..")
.unwrap()
.parent_unchecked()
.unwrap(),
);
}
sourcepub fn read_dir(&self) -> Result<ReadDir>
pub fn read_dir(&self) -> Result<ReadDir>
Equivalent to Path::read_dir
.
sourcepub fn read_link(&self) -> Result<PathBuf>
pub fn read_link(&self) -> Result<PathBuf>
Equivalent to Path::read_link
.
sourcepub fn starts_with<P>(&self, base: P) -> boolwhere
P: AsRef<Path>,
pub fn starts_with<P>(&self, base: P) -> boolwhere P: AsRef<Path>,
Equivalent to Path::starts_with
.
sourcepub fn symlink_metadata(&self) -> Result<Metadata>
pub fn symlink_metadata(&self) -> Result<Metadata>
Equivalent to Path::symlink_metadata
.
Trait Implementations§
source§impl AsRef<BasePath> for BasePathBuf
impl AsRef<BasePath> for BasePathBuf
source§impl Borrow<BasePath> for BasePathBuf
impl Borrow<BasePath> for BasePathBuf
source§impl PartialEq<&BasePath> for BasePathBuf
impl PartialEq<&BasePath> for BasePathBuf
source§impl PartialEq<&BasePath> for Cow<'_, BasePath>
impl PartialEq<&BasePath> for Cow<'_, BasePath>
source§impl PartialEq<&BasePath> for Cow<'_, Path>
impl PartialEq<&BasePath> for Cow<'_, Path>
source§impl PartialEq<&BasePath> for Path
impl PartialEq<&BasePath> for Path
source§impl PartialEq<&BasePath> for PathBuf
impl PartialEq<&BasePath> for PathBuf
source§impl PartialEq<&Path> for BasePath
impl PartialEq<&Path> for BasePath
source§impl PartialEq<BasePath> for &Path
impl PartialEq<BasePath> for &Path
source§impl PartialEq<BasePath> for BasePath
impl PartialEq<BasePath> for BasePath
source§impl PartialEq<BasePath> for BasePathBuf
impl PartialEq<BasePath> for BasePathBuf
source§impl PartialEq<BasePath> for Cow<'_, BasePath>
impl PartialEq<BasePath> for Cow<'_, BasePath>
source§impl PartialEq<BasePath> for Cow<'_, Path>
impl PartialEq<BasePath> for Cow<'_, Path>
source§impl PartialEq<BasePath> for Path
impl PartialEq<BasePath> for Path
source§impl PartialEq<BasePath> for PathBuf
impl PartialEq<BasePath> for PathBuf
source§impl PartialEq<BasePathBuf> for &BasePath
impl PartialEq<BasePathBuf> for &BasePath
source§fn eq(&self, other: &BasePathBuf) -> bool
fn eq(&self, other: &BasePathBuf) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<BasePathBuf> for BasePath
impl PartialEq<BasePathBuf> for BasePath
source§fn eq(&self, other: &BasePathBuf) -> bool
fn eq(&self, other: &BasePathBuf) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<Cow<'_, BasePath>> for &BasePath
impl PartialEq<Cow<'_, BasePath>> for &BasePath
source§impl PartialEq<Cow<'_, BasePath>> for BasePath
impl PartialEq<Cow<'_, BasePath>> for BasePath
source§impl PartialEq<Cow<'_, Path>> for &BasePath
impl PartialEq<Cow<'_, Path>> for &BasePath
source§impl PartialEq<Cow<'_, Path>> for BasePath
impl PartialEq<Cow<'_, Path>> for BasePath
source§impl PartialEq<Path> for &BasePath
impl PartialEq<Path> for &BasePath
source§impl PartialEq<Path> for BasePath
impl PartialEq<Path> for BasePath
source§impl PartialEq<PathBuf> for &BasePath
impl PartialEq<PathBuf> for &BasePath
source§impl PartialEq<PathBuf> for BasePath
impl PartialEq<PathBuf> for BasePath
source§impl PartialOrd<&BasePath> for BasePathBuf
impl PartialOrd<&BasePath> for BasePathBuf
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<&BasePath> for Cow<'_, BasePath>
impl PartialOrd<&BasePath> for Cow<'_, BasePath>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<&BasePath> for Cow<'_, Path>
impl PartialOrd<&BasePath> for Cow<'_, Path>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<&BasePath> for Path
impl PartialOrd<&BasePath> for Path
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<&BasePath> for PathBuf
impl PartialOrd<&BasePath> for PathBuf
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<&Path> for BasePath
impl PartialOrd<&Path> for BasePath
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<BasePath> for &Path
impl PartialOrd<BasePath> for &Path
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<BasePath> for BasePath
impl PartialOrd<BasePath> for BasePath
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<BasePath> for BasePathBuf
impl PartialOrd<BasePath> for BasePathBuf
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<BasePath> for Cow<'_, BasePath>
impl PartialOrd<BasePath> for Cow<'_, BasePath>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<BasePath> for Cow<'_, Path>
impl PartialOrd<BasePath> for Cow<'_, Path>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<BasePath> for Path
impl PartialOrd<BasePath> for Path
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<BasePath> for PathBuf
impl PartialOrd<BasePath> for PathBuf
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<BasePathBuf> for &BasePath
impl PartialOrd<BasePathBuf> for &BasePath
source§fn partial_cmp(&self, other: &BasePathBuf) -> Option<Ordering>
fn partial_cmp(&self, other: &BasePathBuf) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<BasePathBuf> for BasePath
impl PartialOrd<BasePathBuf> for BasePath
source§fn partial_cmp(&self, other: &BasePathBuf) -> Option<Ordering>
fn partial_cmp(&self, other: &BasePathBuf) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<Cow<'_, BasePath>> for &BasePath
impl PartialOrd<Cow<'_, BasePath>> for &BasePath
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<Cow<'_, BasePath>> for BasePath
impl PartialOrd<Cow<'_, BasePath>> for BasePath
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<Cow<'_, Path>> for &BasePath
impl PartialOrd<Cow<'_, Path>> for &BasePath
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<Cow<'_, Path>> for BasePath
impl PartialOrd<Cow<'_, Path>> for BasePath
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<Path> for &BasePath
impl PartialOrd<Path> for &BasePath
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<Path> for BasePath
impl PartialOrd<Path> for BasePath
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<PathBuf> for &BasePath
impl PartialOrd<PathBuf> for &BasePath
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<PathBuf> for BasePath
impl PartialOrd<PathBuf> for BasePath
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more