pub struct Path { /* private fields */ }Expand description
Memory-efficient file path representation.
When storing multiple paths with common parent, the standard PathBuf
would keep the parent path text duplicated in memory, wasting a lot of memory.
This structure here shares the common parent between many paths by reference-counted
references.
Implementations§
Source§impl Path
impl Path
pub fn canonicalize(&self) -> Path
pub fn is_absolute(&self) -> bool
pub fn is_relative(&self) -> bool
Sourcepub fn root(&self) -> Option<&Path>
pub fn root(&self) -> Option<&Path>
Returns the absolute root of the path if the path is absolute. In Unix, returns “/”. In Windows this can return a root with prefix e.g. “C:". If path is relative, returns None.
Copies this path from under an Arc.
Generally cheap, because only the last component is copied.
Sourcepub fn join<P: AsRef<Path>>(self: &Arc<Path>, path: P) -> Path
pub fn join<P: AsRef<Path>>(self: &Arc<Path>, path: P) -> Path
Creates an owned Path with path adjoined to self.
The path must be relative.
Sourcepub fn resolve<P: AsRef<Path>>(self: &Arc<Path>, path: P) -> Path
pub fn resolve<P: AsRef<Path>>(self: &Arc<Path>, path: P) -> Path
If path is relative, works the same as join.
If path is absolute, ignores self and returns path.
Sourcepub fn file_name(&self) -> Option<OsString>
pub fn file_name(&self) -> Option<OsString>
Returns the name of the last component of this path or None if the path is directory (e.g. root dir or parent dir).
Sourcepub fn file_name_cstr(&self) -> Option<&CStr>
pub fn file_name_cstr(&self) -> Option<&CStr>
Returns the name of the last component of this path or None if the path is directory (e.g. root dir or parent dir). Doesn’t allocate anything on the heap.
Sourcepub fn parent(&self) -> Option<&Arc<Path>>
pub fn parent(&self) -> Option<&Arc<Path>>
Returns the parent directory of this path. Doesn’t allocate anything on the heap.
Sourcepub fn strip_prefix(&self, base: &Path) -> Option<Path>
pub fn strip_prefix(&self, base: &Path) -> Option<Path>
Returns a path that joined to base would give this path.
If base is the same as this path, returns current directory.
If this path doesn’t have a base prefix, returns None.
Sourcepub fn strip_root(&self) -> Path
pub fn strip_root(&self) -> Path
If this path is absolute, strips the root component and returns a relative path.
Otherwise returns a clone of this path.
E.g. /foo/bar becomes foo/bar
Sourcepub fn is_prefix_of(&self, other: &Path) -> bool
pub fn is_prefix_of(&self, other: &Path) -> bool
Returns true if self is a prefix of another path
Sourcepub fn to_path_buf(&self) -> PathBuf
pub fn to_path_buf(&self) -> PathBuf
Converts this path to a standard library path buffer. We need this to be able to use this path with other standard library I/O functions.
Sourcepub fn to_string_lossy(&self) -> String
pub fn to_string_lossy(&self) -> String
Converts this path to an UTF encoded string.
Any non-Unicode sequences are replaced with
[U+FFFD REPLACEMENT CHARACTER][U+FFFD].
Sourcepub fn to_escaped_string(&self) -> String
pub fn to_escaped_string(&self) -> String
Returns a lossless string representation in STFU8 format.
Sourcepub fn from_escaped_string(encoded: &str) -> Result<Path, DecodeError>
pub fn from_escaped_string(encoded: &str) -> Result<Path, DecodeError>
Decodes the path from the string encoded with to_escaped_string.
Sourcepub fn quote(&self) -> String
pub fn quote(&self) -> String
Formats the path in a way that Posix-shell can decode it.
If the path doesn’t contain any special characters, returns it as-is.
If the path contains special shell characters like ‘\’ or ‘*’, it is single-quoted.
This function also takes care of the characters that cannot be represented in UTF-8
by escaping them with $'\xXX' or $'\uXXXX' syntax.
Sourcepub fn display(&self) -> String
pub fn display(&self) -> String
Returns a representation suitable for display in the console. Control characters like newline or linefeed are escaped.
Sourcepub fn hash128(&self) -> u128
pub fn hash128(&self) -> u128
Returns a hash of the full path. Useful for deduplicating paths without making path clones. We need 128-bits so that collisions are not a problem. Thanks to using a long hash we can be sure collisions won’t be a problem.
Sourcepub fn component_count(&self) -> usize
pub fn component_count(&self) -> usize
Returns the number of components in this path
Trait Implementations§
Source§impl AsRef<Path> for PathAndMetadata
impl AsRef<Path> for PathAndMetadata
Source§impl<'de> Deserialize<'de> for Path
impl<'de> Deserialize<'de> for Path
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl From<PathAndMetadata> for Path
impl From<PathAndMetadata> for Path
Source§fn from(value: PathAndMetadata) -> Self
fn from(value: PathAndMetadata) -> Self
Source§impl Ord for Path
impl Ord for Path
Source§impl PartialOrd for Path
impl PartialOrd for Path
Source§impl Serialize for Path
impl Serialize for Path
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Source§impl ValueParserFactory for Path
Allows to read command line arguments as crate::path::Path.
impl ValueParserFactory for Path
Allows to read command line arguments as crate::path::Path.
Source§type Parser = PathParser
type Parser = PathParser
ValueParser. Read moreSource§fn value_parser() -> Self::Parser
fn value_parser() -> Self::Parser
Self::Parserimpl Eq for Path
impl StructuralPartialEq for Path
Auto Trait Implementations§
impl Freeze for Path
impl RefUnwindSafe for Path
impl Send for Path
impl Sync for Path
impl Unpin for Path
impl UnwindSafe for Path
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more