pub type Utf8UnixPathBuf = Utf8PathBuf<Utf8UnixEncoding>;
Expand description
Represents a Unix-specific Utf8PathBuf
Aliased Type§
pub struct Utf8UnixPathBuf { /* private fields */ }
Implementations
Source§impl<T> Utf8PathBuf<T>where
T: Utf8Encoding,
impl<T> Utf8PathBuf<T>where
T: Utf8Encoding,
Sourcepub fn new() -> Self
pub fn new() -> Self
Allocates an empty Utf8PathBuf
.
§Examples
use typed_path::{Utf8PathBuf, Utf8UnixEncoding};
// NOTE: A pathbuf cannot be created on its own without a defined encoding
let path = Utf8PathBuf::<Utf8UnixEncoding>::new();
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new PathBuf
with a given capacity used to create the
internal String
. See with_capacity
defined on String
.
§Examples
use typed_path::{Utf8PathBuf, Utf8UnixEncoding};
// NOTE: A pathbuf cannot be created on its own without a defined encoding
let mut path = Utf8PathBuf::<Utf8UnixEncoding>::with_capacity(10);
let capacity = path.capacity();
// This push is done without reallocating
path.push(r"C:\");
assert_eq!(capacity, path.capacity());
Sourcepub fn push<P: AsRef<Utf8Path<T>>>(&mut self, path: P)
pub fn push<P: AsRef<Utf8Path<T>>>(&mut self, path: P)
Extends self
with path
.
If path
is absolute, it replaces the current path.
With Utf8WindowsPathBuf
:
- if
path
has a root but no prefix (e.g.,\windows
), it replaces everything except for the prefix (if any) ofself
. - if
path
has a prefix but no root, it replacesself
. - if
self
has a verbatim prefix (e.g.\\?\C:\windows
) andpath
is not empty, the new path is normalized: all references to.
and..
are removed.
§Examples
Pushing a relative path extends the existing path:
use typed_path::{Utf8PathBuf, Utf8UnixEncoding};
// NOTE: A pathbuf cannot be created on its own without a defined encoding
let mut path = Utf8PathBuf::<Utf8UnixEncoding>::from("/tmp");
path.push("file.bk");
assert_eq!(path, Utf8PathBuf::from("/tmp/file.bk"));
Pushing an absolute path replaces the existing path:
use typed_path::{Utf8PathBuf, Utf8UnixEncoding};
// NOTE: A pathbuf cannot be created on its own without a defined encoding
let mut path = Utf8PathBuf::<Utf8UnixEncoding>::from("/tmp");
path.push("/etc");
assert_eq!(path, Utf8PathBuf::from("/etc"));
Sourcepub fn push_checked<P: AsRef<Utf8Path<T>>>(
&mut self,
path: P,
) -> Result<(), CheckedPathError>
pub fn push_checked<P: AsRef<Utf8Path<T>>>( &mut self, path: P, ) -> Result<(), CheckedPathError>
Like Utf8PathBuf::push
, extends self
with path
, but also checks to ensure that
path
abides by a set of rules.
§Rules
path
cannot contain a prefix component.path
cannot contain a root component.path
cannot contain invalid filename bytes.path
cannot contain parent components such that the current path would be escaped.
§Examples
Pushing a relative path extends the existing path:
use typed_path::{Utf8PathBuf, Utf8UnixEncoding};
// NOTE: A pathbuf cannot be created on its own without a defined encoding
let mut path = Utf8PathBuf::<Utf8UnixEncoding>::from("/tmp");
// Pushing a relative path works like normal
assert!(path.push_checked("file.bk").is_ok());
assert_eq!(path, Utf8PathBuf::from("/tmp/file.bk"));
Pushing a relative path that contains unresolved parent directory references fails with an error:
use typed_path::{CheckedPathError, Utf8PathBuf, Utf8UnixEncoding};
// NOTE: A pathbuf cannot be created on its own without a defined encoding
let mut path = Utf8PathBuf::<Utf8UnixEncoding>::from("/tmp");
// Pushing a relative path that contains parent directory references that cannot be
// resolved within the path is considered an error as this is considered a path
// traversal attack!
assert_eq!(path.push_checked(".."), Err(CheckedPathError::PathTraversalAttack));
assert_eq!(path, Utf8PathBuf::from("/tmp"));
Pushing an absolute path fails with an error:
use typed_path::{CheckedPathError, Utf8PathBuf, Utf8UnixEncoding};
// NOTE: A pathbuf cannot be created on its own without a defined encoding
let mut path = Utf8PathBuf::<Utf8UnixEncoding>::from("/tmp");
// Pushing an absolute path will fail with an error
assert_eq!(path.push_checked("/etc"), Err(CheckedPathError::UnexpectedRoot));
assert_eq!(path, Utf8PathBuf::from("/tmp"));
Sourcepub fn pop(&mut self) -> bool
pub fn pop(&mut self) -> bool
Truncates self
to self.parent
.
Returns false
and does nothing if self.parent
is None
.
Otherwise, returns true
.
§Examples
use typed_path::{Utf8Path, Utf8PathBuf, Utf8UnixEncoding};
// NOTE: A pathbuf cannot be created on its own without a defined encoding
let mut p = Utf8PathBuf::<Utf8UnixEncoding>::from("/spirited/away.rs");
p.pop();
assert_eq!(Utf8Path::new("/spirited"), p);
p.pop();
assert_eq!(Utf8Path::new("/"), p);
Sourcepub fn set_file_name<S: AsRef<str>>(&mut self, file_name: S)
pub fn set_file_name<S: AsRef<str>>(&mut self, file_name: S)
Updates self.file_name
to file_name
.
If self.file_name
was None
, this is equivalent to pushing
file_name
.
Otherwise it is equivalent to calling pop
and then pushing
file_name
. The new path will be a sibling of the original path.
(That is, it will have the same parent.)
§Examples
use typed_path::{Utf8PathBuf, Utf8UnixEncoding};
// NOTE: A pathbuf cannot be created on its own without a defined encoding
let mut buf = Utf8PathBuf::<Utf8UnixEncoding>::from("/");
assert!(buf.file_name() == None);
buf.set_file_name("bar");
assert!(buf == Utf8PathBuf::from("/bar"));
assert!(buf.file_name().is_some());
buf.set_file_name("baz.txt");
assert!(buf == Utf8PathBuf::from("/baz.txt"));
Sourcepub fn set_extension<S: AsRef<str>>(&mut self, extension: S) -> bool
pub fn set_extension<S: AsRef<str>>(&mut self, extension: S) -> bool
Updates self.extension
to extension
.
Returns false
and does nothing if self.file_name
is None
,
returns true
and updates the extension otherwise.
If self.extension
is None
, the extension is added; otherwise
it is replaced.
§Examples
use typed_path::{Utf8Path, Utf8PathBuf, Utf8UnixEncoding};
let mut p = Utf8PathBuf::<Utf8UnixEncoding>::from("/feel/the");
p.set_extension("force");
assert_eq!(Utf8Path::new("/feel/the.force"), p.as_path());
p.set_extension("dark_side");
assert_eq!(Utf8Path::new("/feel/the.dark_side"), p.as_path());
Sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
Sourcepub fn into_boxed_path(self) -> Box<Utf8Path<T>>
pub fn into_boxed_path(self) -> Box<Utf8Path<T>>
Converts this Utf8PathBuf
into a boxed Utf8Path
.
Sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
Invokes try_reserve
on the underlying instance of String
.
Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Invokes reserve_exact
on the underlying instance of String
.
Sourcepub fn try_reserve_exact(
&mut self,
additional: usize,
) -> Result<(), TryReserveError>
pub fn try_reserve_exact( &mut self, additional: usize, ) -> Result<(), TryReserveError>
Invokes try_reserve_exact
on the underlying instance of String
.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Invokes shrink_to_fit
on the underlying instance of String
.
Sourcepub fn from_bytes_path_buf<U>(
path_buf: PathBuf<U>,
) -> Result<Self, FromUtf8Error>where
U: Encoding,
pub fn from_bytes_path_buf<U>(
path_buf: PathBuf<U>,
) -> Result<Self, FromUtf8Error>where
U: Encoding,
Consumes PathBuf
and returns a new Utf8PathBuf
by checking that the path contains
valid UTF-8.
§Errors
Returns Err
if the path is not UTF-8 with a description as to why the
provided component is not UTF-8.
§Examples
use typed_path::{PathBuf, Utf8PathBuf, UnixEncoding, Utf8UnixEncoding};
let path_buf = PathBuf::<UnixEncoding>::from(&[0xf0, 0x9f, 0x92, 0x96]);
let utf8_path_buf = Utf8PathBuf::<Utf8UnixEncoding>::from_bytes_path_buf(path_buf).unwrap();
assert_eq!(utf8_path_buf.as_str(), "💖");
Sourcepub unsafe fn from_bytes_path_buf_unchecked<U>(path_buf: PathBuf<U>) -> Selfwhere
U: Encoding,
pub unsafe fn from_bytes_path_buf_unchecked<U>(path_buf: PathBuf<U>) -> Selfwhere
U: Encoding,
Consumes PathBuf
and returns a new Utf8PathBuf
by checking that the path contains
valid UTF-8.
§Errors
Returns Err
if the path is not UTF-8 with a description as to why the
provided component is not UTF-8.
§Safety
The path passed in must be valid UTF-8.
§Examples
use typed_path::{PathBuf, Utf8PathBuf, UnixEncoding, Utf8UnixEncoding};
let path_buf = PathBuf::<UnixEncoding>::from(&[0xf0, 0x9f, 0x92, 0x96]);
let utf8_path_buf = unsafe {
Utf8PathBuf::<Utf8UnixEncoding>::from_bytes_path_buf_unchecked(path_buf)
};
assert_eq!(utf8_path_buf.as_str(), "💖");
Sourcepub fn into_bytes_path_buf<U>(self) -> PathBuf<U>where
U: Encoding,
pub fn into_bytes_path_buf<U>(self) -> PathBuf<U>where
U: Encoding,
Consumes Utf8PathBuf
and returns a new PathBuf
§Examples
use typed_path::{PathBuf, Utf8PathBuf, UnixEncoding, Utf8UnixEncoding};
let utf8_path_buf = Utf8PathBuf::<Utf8UnixEncoding>::from("💖");
let path_buf = utf8_path_buf.into_bytes_path_buf::<UnixEncoding>();
assert_eq!(path_buf.as_bytes(), &[0xf0, 0x9f, 0x92, 0x96]);
Trait Implementations§
Source§impl TryFrom<Utf8TypedPathBuf> for Utf8UnixPathBuf
impl TryFrom<Utf8TypedPathBuf> for Utf8UnixPathBuf
Source§type Error = Utf8TypedPathBuf
type Error = Utf8TypedPathBuf
Source§impl<T> AsRef<[u8]> for Utf8PathBuf<T>where
T: Utf8Encoding,
impl<T> AsRef<[u8]> for Utf8PathBuf<T>where
T: Utf8Encoding,
Source§impl<T> AsRef<OsStr> for Utf8PathBuf<T>where
T: Utf8Encoding,
impl<T> AsRef<OsStr> for Utf8PathBuf<T>where
T: Utf8Encoding,
Source§impl<T> AsRef<Utf8Path<T>> for Utf8PathBuf<T>where
T: Utf8Encoding,
impl<T> AsRef<Utf8Path<T>> for Utf8PathBuf<T>where
T: Utf8Encoding,
Source§impl<T> AsRef<str> for Utf8PathBuf<T>where
T: Utf8Encoding,
impl<T> AsRef<str> for Utf8PathBuf<T>where
T: Utf8Encoding,
Source§impl<T> Borrow<Utf8Path<T>> for Utf8PathBuf<T>where
T: Utf8Encoding,
impl<T> Borrow<Utf8Path<T>> for Utf8PathBuf<T>where
T: Utf8Encoding,
Source§impl<T> Clone for Utf8PathBuf<T>where
T: Utf8Encoding,
impl<T> Clone for Utf8PathBuf<T>where
T: Utf8Encoding,
Source§impl<T> Debug for Utf8PathBuf<T>where
T: Utf8Encoding,
impl<T> Debug for Utf8PathBuf<T>where
T: Utf8Encoding,
Source§impl<T> Default for Utf8PathBuf<T>where
T: Utf8Encoding,
impl<T> Default for Utf8PathBuf<T>where
T: Utf8Encoding,
Source§fn default() -> Utf8PathBuf<T>
fn default() -> Utf8PathBuf<T>
Source§impl<T> Deref for Utf8PathBuf<T>where
T: Utf8Encoding,
impl<T> Deref for Utf8PathBuf<T>where
T: Utf8Encoding,
Source§impl<T> Display for Utf8PathBuf<T>where
T: Utf8Encoding,
impl<T> Display for Utf8PathBuf<T>where
T: Utf8Encoding,
Source§impl<T, P> Extend<P> for Utf8PathBuf<T>
impl<T, P> Extend<P> for Utf8PathBuf<T>
Source§fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T, V> From<&V> for Utf8PathBuf<T>
impl<T, V> From<&V> for Utf8PathBuf<T>
Source§fn from(s: &V) -> Self
fn from(s: &V) -> Self
Converts a borrowed str
to a Utf8PathBuf
.
Allocates a Utf8PathBuf
and copies the data into it.