PathBuf

Struct PathBuf 

Source
pub struct PathBuf { /* private fields */ }
Expand description

An owned, mutable path

Paths must be null terminated ASCII strings with at most PathBuf::MAX_SIZE bytes (not including the trailing null).

Implementations§

Source§

impl PathBuf

Source

pub const MAX_SIZE: usize = 255usize

Source

pub const MAX_SIZE_PLUS_ONE: usize = 256usize

Source

pub const fn new() -> Self

Source

pub const fn from_path(path: &Path) -> Self

Creates a PathBuf from a Path.

This method is a const-friendly version of the From<&Path> implementation. If you don’t need a const method, prefer From<&Path> as it is more idiomatic and more efficient.

The path_buf macro can be used instead to construct a PathBuf from a string literal.

§Example
const PATH: PathBuf = PathBuf::from_path(path!("test"));
assert_eq!(PATH, path_buf!("test"));
Source

pub const fn as_path(&self) -> &Path

Source

pub const fn as_str(&self) -> &str

Source

pub fn clear(&mut self)

Source

pub const unsafe fn from_buffer_unchecked(buf: [c_char; 256]) -> Self

Creates a from a raw buffer containing a null-terminated ASCII string.

§Safety

The buffer must contain only ASCII characters and at least one null byte.

Source

pub fn push(&mut self, path: &Path)

Extends self with path

Methods from Deref<Target = Path>§

Source

pub fn const_eq(&self, path: &Path) -> bool

Checks two paths for equality.

This provides an easy way to check paths in a const context.

§Example
const fn check(path: &Path) -> bool {
    !path.const_eq(path!("forbidden-path"))
}

assert!(check(path!("allowed-path")));
assert!(!check(path!("forbidden-path")));
Source

pub fn cmp_str(&self, other: &Path) -> Ordering

Compare the path using their string representation This comarison function as would be expected for a String type.

This ordering does not match the ordering obsvered when iterating over a directory.

See cmp_lfs and littlefs#923.

assert_eq!(path!("some_path_a").cmp_str(path!("some_path_b")), Ordering::Less);
assert_eq!(path!("some_path_b").cmp_str(path!("some_path_a")), Ordering::Greater);
assert_eq!(path!("some_path").cmp_str(path!("some_path_a")), Ordering::Less);
assert_eq!(path!("some_path").cmp_str(path!("some_path_b")), Ordering::Less);
assert_eq!(path!("some_path").cmp_str(path!("some_path")), Ordering::Equal);
Source

pub fn cmp_lfs(&self, other: &Path) -> Ordering

Compare the path using their string representation

This comparison function matches the iteration order of littlefs when iterating over directory. For more information, see littlefs#923

assert_eq!(path!("some_path_a").cmp_lfs(path!("some_path_b")), Ordering::Less);
assert_eq!(path!("some_path_b").cmp_lfs(path!("some_path_a")), Ordering::Greater);
assert_eq!(path!("some_path").cmp_lfs(path!("some_path_a")), Ordering::Greater);
assert_eq!(path!("some_path").cmp_lfs(path!("some_path_b")), Ordering::Greater);
assert_eq!(path!("some_path_a").cmp_lfs(path!("some_path")), Ordering::Less);
assert_eq!(path!("some_path_b").cmp_lfs(path!("some_path")), Ordering::Less);
assert_eq!(path!("some_path").cmp_lfs(path!("some_path")), Ordering::Equal);
Source

pub fn is_empty(&self) -> bool

Return true if the path is empty


assert!(path!("").is_empty());
assert!(!path!("something").is_empty());
Source

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

Get the name of the file this path points to if it points to one

let path = path!("/some/path/file.extension");
assert_eq!(path.file_name(), Some(path!("file.extension")));

let path = path!("/");
assert_eq!(path.file_name(), None);

let path = path!("");
assert_eq!(path.file_name(), None);

let path = path!("/some/path/file.extension/");
assert_eq!(path.file_name(), None);
Source

pub fn ancestors(&self) -> Ancestors<'_>

Iterate over the ancestors of the path

let path = path!("/some/path/file.extension");
let mut ancestors = path.ancestors();
assert_eq!(&*ancestors.next().unwrap(), path!("/some/path/file.extension"));
assert_eq!(&*ancestors.next().unwrap(), path!("/some/path"));
assert_eq!(&*ancestors.next().unwrap(), path!("/some"));
assert_eq!(&*ancestors.next().unwrap(), path!("/"));
assert!(ancestors.next().is_none());
Source

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

Iterate over the components of the path

let path = path!("/some/path/file.extension");
let mut iter = path.iter();
assert_eq!(&*iter.next().unwrap(), path!("/"));
assert_eq!(&*iter.next().unwrap(), path!("some"));
assert_eq!(&*iter.next().unwrap(), path!("path"));
assert_eq!(&*iter.next().unwrap(), path!("file.extension"));
assert!(iter.next().is_none());
Source

pub fn as_ptr(&self) -> *const c_char

Returns the inner pointer to this C string.

Source

pub fn join(&self, path: &Path) -> PathBuf

Creates an owned PathBuf with path adjoined to self.

Source

pub fn as_str_ref_with_trailing_nul(&self) -> &str

Source

pub fn as_str(&self) -> &str

Source

pub fn parent(&self) -> Option<PathBuf>

Trait Implementations§

Source§

impl Clone for PathBuf

Source§

fn clone(&self) -> PathBuf

Returns a duplicate 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 PathBuf

Source§

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

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

impl Default for PathBuf

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Deref for PathBuf

Source§

type Target = Path

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Path

Dereferences the value.
Source§

impl Display for PathBuf

Source§

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

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

impl From<&Path> for PathBuf

Source§

fn from(path: &Path) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for PathBuf

Source§

fn eq(&self, other: &Self) -> 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 TryFrom<&[u8]> for PathBuf

Accepts byte strings, with or without trailing nul.

Source§

type Error = PathError

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

fn try_from(bytes: &[u8]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 1]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 1]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 10]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 10]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 11]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 11]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 12]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 12]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 13]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 13]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 14]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 14]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 15]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 15]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 16]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 16]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 17]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 17]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 18]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 18]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 19]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 19]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 2]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 2]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 20]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 20]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 21]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 21]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 22]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 22]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 23]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 23]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 24]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 24]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 25]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 25]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 26]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 26]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 27]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 27]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 28]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 28]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 29]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 29]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 3]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 3]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 30]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 30]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 31]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 31]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 32]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 32]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 4]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 4]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 5]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 5]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 6]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 6]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 7]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 7]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 8]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 8]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&[u8; 9]> for PathBuf

Source§

type Error = PathError

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

fn try_from(bytes: &[u8; 9]) -> Result<Self, PathError>

Performs the conversion.
Source§

impl TryFrom<&str> for PathBuf

Accepts strings, with or without trailing nul.

Source§

type Error = PathError

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

fn try_from(s: &str) -> Result<Self, PathError>

Performs the conversion.
Source§

impl Eq for PathBuf

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.