pub struct Path { /* private fields */ }Expand description
A path
Paths must be null terminated ASCII strings with at most PathBuf::MAX_SIZE bytes (not
including the trailing null).
Implementations§
Source§impl Path
impl Path
Sourcepub const fn const_eq(&self, path: &Path) -> bool
pub const 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);Source§impl Path
impl Path
Sourcepub const fn is_empty(&self) -> bool
pub const 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 const fn from_str_with_nul(s: &str) -> Result<&Self, PathError>
pub const fn from_str_with_nul(s: &str) -> Result<&Self, PathError>
Creates a path from a string.
The string must only consist of ASCII characters. The last character must be null. It
must contain at most PathBuf::MAX_SIZE bytes, not including the trailing null. If
these conditions are not met, this function returns an error.
Sourcepub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, PathError>
pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, PathError>
Creates a path from a byte buffer.
The byte buffer must only consist of ASCII characters. The last character must be null.
It must contain at most PathBuf::MAX_SIZE bytes, not including the trailing null. If
these conditions are not met, this function returns an error.
Sourcepub const fn from_cstr(cstr: &CStr) -> Result<&Self, PathError>
pub const fn from_cstr(cstr: &CStr) -> Result<&Self, PathError>
Creates a path from a C string.
The string must only consist of ASCII characters. It must contain at most
PathBuf::MAX_SIZE bytes, not including the trailing null. If these conditions are
not met, this function returns an error.
Sourcepub const unsafe fn from_cstr_unchecked(cstr: &CStr) -> &Self
pub const unsafe fn from_cstr_unchecked(cstr: &CStr) -> &Self
Creates a path from a C string without checking the invariants.
§Safety
The string must only consist of ASCII characters. It must contain at most
PathBuf::MAX_SIZE bytes, not including the trailing null.
Sourcepub fn join(&self, path: &Path) -> PathBuf
pub fn join(&self, path: &Path) -> PathBuf
Creates an owned PathBuf with path adjoined to self.