canonical_path

Struct CanonicalPathBuf

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

An owned path on the filesystem which is guaranteed to be canonical.

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 CanonicalPathBuf

Source

pub fn canonicalize<P>(path: P) -> Result<Self>
where P: AsRef<Path>,

Create a canonical path by first canonicalizing the given path.

Source

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

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

Source

pub fn as_canonical_path(&self) -> &CanonicalPath

Return a CanonicalPath reference.

Source

pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S)

Updates self’s filename ala the same method on PathBuf

Source

pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool

Updates self.extension to extension.

Returns false and does nothing if self.file_name is None, returns true and updates the extension otherwise. If self.extension is None, the extension is added; otherwise it is replaced.

Source

pub fn into_path_buf(self) -> PathBuf

Consumes the CanonicalPathBuf, yielding its internal PathBuf storage.

Source

pub fn into_os_string(self) -> OsString

Consumes the CanonicalPathBuf, yielding its internal OsString storage.

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<OsStr> for CanonicalPathBuf

Source§

fn as_ref(&self) -> &OsStr

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

impl AsRef<Path> for CanonicalPathBuf

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 Clone for CanonicalPathBuf

Source§

fn clone(&self) -> CanonicalPathBuf

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CanonicalPathBuf

Source§

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

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

impl Hash for CanonicalPathBuf

Source§

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

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

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for CanonicalPathBuf

Source§

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

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

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for CanonicalPathBuf

Source§

fn eq(&self, other: &CanonicalPathBuf) -> 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 CanonicalPathBuf

Source§

fn partial_cmp(&self, other: &CanonicalPathBuf) -> 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 Eq for CanonicalPathBuf

Source§

impl StructuralPartialEq for CanonicalPathBuf

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
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.