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
impl PathBuf
pub const MAX_SIZE: usize = 255usize
pub const MAX_SIZE_PLUS_ONE: usize = 256usize
pub const fn new() -> Self
Sourcepub const fn from_path(path: &Path) -> Self
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"));pub const fn as_path(&self) -> &Path
pub const fn as_str(&self) -> &str
pub fn clear(&mut self)
Sourcepub const unsafe fn from_buffer_unchecked(buf: [c_char; 256]) -> Self
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.
Methods from Deref<Target = Path>§
Sourcepub fn const_eq(&self, path: &Path) -> bool
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")));Sourcepub fn cmp_str(&self, other: &Path) -> Ordering
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.
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);Sourcepub fn cmp_lfs(&self, other: &Path) -> Ordering
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);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Return true if the path is empty
assert!(path!("").is_empty());
assert!(!path!("something").is_empty());Sourcepub fn file_name(&self) -> Option<&Path>
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);Sourcepub fn ancestors(&self) -> Ancestors<'_> ⓘ
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());Sourcepub fn iter(&self) -> Iter<'_> ⓘ
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());Sourcepub fn join(&self, path: &Path) -> PathBuf
pub fn join(&self, path: &Path) -> PathBuf
Creates an owned PathBuf with path adjoined to self.