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 PartialEq for Path
impl PartialEq for Path
source§impl PartialOrd for Path
impl PartialOrd for Path
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§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
impl ValueParserFactory for Path
Allows to read command line arguments as crate::path::Path.
§type Parser = PathParser
type Parser = PathParser
ValueParser
]. Read moresource§fn value_parser() -> Self::Parser
fn value_parser() -> Self::Parser
Self::Parser
]impl Eq for Path
impl StructuralEq for Path
impl StructuralPartialEq for Path
Auto Trait Implementations§
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
§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.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.