canonical_path

Struct CanonicalPath

Source
pub struct CanonicalPath(/* private fields */);
Expand description

A reference type for a canonical filesystem path

More specifically: it is at least guaranteed to be canonical at the time it is created. There are potential TOCTTOU problems if the underlying filesystem structure changes after path canonicalization.

Implementations§

Source§

impl CanonicalPath

Source

pub fn new<P>(path: &P) -> Result<&Self>
where P: AsRef<Path> + ?Sized,

Create a canonical path, returning error if the supplied path is not canonical

Source

pub unsafe fn from_path_unchecked<P>(path: &P) -> &Self
where P: AsRef<Path> + ?Sized,

Create a canonical path from a path, skipping the canonicalization check

This uses the same unsafe reference conversion tricks as std to convert from AsRef<Path> to AsRef<CanonicalPath>, i.e. &CanonicalPath is a newtype for &Path in the same way &Path is a newtype for &OsStr.

Source

pub fn to_canonical_path_buf(&self) -> CanonicalPathBuf

Convert a canonical path reference into an owned CanonicalPathBuf

Source

pub fn as_path(&self) -> &Path

Return a Path reference.

Source

pub fn as_os_str(&self) -> &OsStr

Return an OsStr reference.

Source

pub fn to_str(&self) -> Option<&str>

Yields a &str slice if the path is valid unicode.

Source

pub fn parent(&self) -> Result<CanonicalPathBuf>

Return a canonical parent path of this path, or io::Error if the path is the root directory or another canonicalization error occurs.

Source

pub fn file_name(&self) -> Option<&OsStr>

Returns the final component of the path, if there is one.

Source

pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool

Determines whether base is a prefix of self.

Source

pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool

Determines whether child is a suffix of self.

Source

pub fn file_stem(&self) -> Option<&OsStr>

Extracts the stem (non-extension) portion of self.file_name.

Source

pub fn extension(&self) -> Option<&OsStr>

Extracts the extension of self.file_name, if possible.

Source

pub fn with_file_name<S: AsRef<OsStr>>( &self, file_name: S, ) -> Result<CanonicalPathBuf>

Creates an owned CanonicalPathBuf like self but with the given file name.

Source

pub fn with_extension<S: AsRef<OsStr>>( &self, extension: S, ) -> Result<CanonicalPathBuf>

Creates an owned CanonicalPathBuf like self but with the given extension.

Source

pub fn components(&self) -> Components<'_>

Produces an iterator over the Components of a path

Source

pub fn iter(&self) -> Iter<'_>

Produces an iterator over the path’s components viewed as OsStr slices.

Source

pub fn display(&self) -> Display<'_>

Returns an object that implements Display for safely printing paths that may contain non-Unicode data.

Source

pub fn metadata(&self) -> Result<Metadata>

Queries the file system to get information about a file, directory, etc.

Unlike the std version of this method, it will not follow symlinks, since as a canonical path we should be symlink-free.

Source

pub fn join<P: AsRef<Path>>(&self, path: P) -> Result<CanonicalPathBuf>

Join a path onto a canonical path, returning a CanonicalPathBuf.

Source

pub fn read_dir(&self) -> Result<ReadDir>

Returns an iterator over the entries within a directory.

The iterator will yield instances of io::Result. New errors may be encountered after an iterator is initially constructed.

This is an alias to fs::read_dir.

Source

pub fn exists(&self) -> bool

Does this path exist?

Source

pub fn is_file(&self) -> bool

Is this path a file?

Source

pub fn is_dir(&self) -> bool

Is this path a directory?

Trait Implementations§

Source§

impl AsRef<CanonicalPath> for CanonicalPathBuf

Source§

fn as_ref(&self) -> &CanonicalPath

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Path> for CanonicalPath

Source§

fn as_ref(&self) -> &Path

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<CanonicalPath> for CanonicalPathBuf

Source§

fn borrow(&self) -> &CanonicalPath

Immutably borrows from an owned value. Read more
Source§

impl Debug for CanonicalPath

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for CanonicalPath

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
Source§

impl Ord for CanonicalPath

Source§

fn cmp(&self, other: &CanonicalPath) -> Ordering

This method returns an Ordering between self and other. Read more
Source§

impl PartialEq for CanonicalPath

Source§

fn eq(&self, other: &CanonicalPath) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for CanonicalPath

Source§

fn partial_cmp(&self, other: &CanonicalPath) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl ToOwned for CanonicalPath

Source§

type Owned = CanonicalPathBuf

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> CanonicalPathBuf

Creates owned data from borrowed data, usually by cloning. Read more
1.63.0 · Source§

fn clone_into(&self, target: &mut Self::Owned)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl Eq for CanonicalPath

Source§

impl StructuralPartialEq for CanonicalPath

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more