Trait std::old_path::GenericPathUnstable
[-] [+]
[src]
pub trait GenericPath: Clone + GenericPathUnsafe { fn as_vec<'a>(&'a self) -> &'a [u8]; fn into_vec(self) -> Vec<u8>; fn dirname<'a>(&'a self) -> &'a [u8]; fn filename<'a>(&'a self) -> Option<&'a [u8]>; fn root_path(&self) -> Option<Self>; fn pop(&mut self) -> bool; fn is_absolute(&self) -> bool; fn is_ancestor_of(&self, other: &Self) -> bool; fn path_relative_from(&self, base: &Self) -> Option<Self>; fn ends_with_path(&self, child: &Self) -> bool; fn new<T: BytesContainer>(path: T) -> Self { ... } fn new_opt<T: BytesContainer>(path: T) -> Option<Self> { ... } fn as_str<'a>(&'a self) -> Option<&'a str> { ... } fn display<'a>(&'a self) -> Display<'a, Self> { ... } fn filename_display<'a>(&'a self) -> Display<'a, Self> { ... } fn dirname_str<'a>(&'a self) -> Option<&'a str> { ... } fn filename_str<'a>(&'a self) -> Option<&'a str> { ... } fn filestem<'a>(&'a self) -> Option<&'a [u8]> { ... } fn filestem_str<'a>(&'a self) -> Option<&'a str> { ... } fn extension<'a>(&'a self) -> Option<&'a [u8]> { ... } fn extension_str<'a>(&'a self) -> Option<&'a str> { ... } fn set_filename<T: BytesContainer>(&mut self, filename: T) { ... } fn set_extension<T: BytesContainer>(&mut self, extension: T) { ... } fn with_filename<T: BytesContainer>(&self, filename: T) -> Self { ... } fn with_extension<T: BytesContainer>(&self, extension: T) -> Self { ... } fn dir_path(&self) -> Self { ... } fn push<T: BytesContainer>(&mut self, path: T) { ... } fn push_many<T: BytesContainer>(&mut self, paths: &[T]) { ... } fn join<T: BytesContainer>(&self, path: T) -> Self { ... } fn join_many<T: BytesContainer>(&self, paths: &[T]) -> Self { ... } fn is_relative(&self) -> bool { ... } }
A trait that represents the generic operations available on paths
Required Methods
fn as_vec<'a>(&'a self) -> &'a [u8]
Returns the path as a byte vector
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("abc/def"); assert_eq!(p.as_vec(), b"abc/def"); } }let p = Path::new("abc/def"); assert_eq!(p.as_vec(), b"abc/def");
fn into_vec(self) -> Vec<u8>
Converts the Path into an owned byte vector
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("abc/def"); assert_eq!(p.into_vec(), b"abc/def".to_vec()); // attempting to use p now results in "error: use of moved value" } }let p = Path::new("abc/def"); assert_eq!(p.into_vec(), b"abc/def".to_vec()); // attempting to use p now results in "error: use of moved value"
fn dirname<'a>(&'a self) -> &'a [u8]
Returns the directory component of self
, as a byte vector (with no trailing separator).
If self
has no directory component, returns ['.'].
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("abc/def/ghi"); assert_eq!(p.dirname(), b"abc/def"); } }let p = Path::new("abc/def/ghi"); assert_eq!(p.dirname(), b"abc/def");
fn filename<'a>(&'a self) -> Option<&'a [u8]>
Returns the file component of self
, as a byte vector.
If self
represents the root of the file hierarchy, returns None.
If self
is "." or "..", returns None.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("abc/def/ghi"); assert_eq!(p.filename(), Some(b"ghi")); } }let p = Path::new("abc/def/ghi"); assert_eq!(p.filename(), Some(b"ghi"));
fn root_path(&self) -> Option<Self>
Returns a Path that represents the filesystem root that self
is rooted in.
If self
is not absolute, or vol/cwd-relative in the case of Windows, this returns None.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { assert_eq!(Path::new("abc/def").root_path(), None); assert_eq!(Path::new("/abc/def").root_path(), Some(Path::new("/"))); } }assert_eq!(Path::new("abc/def").root_path(), None); assert_eq!(Path::new("/abc/def").root_path(), Some(Path::new("/")));
fn pop(&mut self) -> bool
Removes the last path component from the receiver.
Returns true
if the receiver was modified, or false
if it already
represented the root of the file hierarchy.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let mut p = Path::new("foo/bar/baz.txt"); p.pop(); assert_eq!(p, Path::new("foo/bar")); } }let mut p = Path::new("foo/bar/baz.txt"); p.pop(); assert_eq!(p, Path::new("foo/bar"));
fn is_absolute(&self) -> bool
Returns whether self
represents an absolute path.
An absolute path is defined as one that, when joined to another path, will
yield back the same absolute path.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("/abc/def"); assert!(p.is_absolute()); } }let p = Path::new("/abc/def"); assert!(p.is_absolute());
fn is_ancestor_of(&self, other: &Self) -> bool
Returns whether self
is equal to, or is an ancestor of, the given path.
If both paths are relative, they are compared as though they are relative
to the same parent path.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("foo/bar/baz/quux.txt"); let fb = Path::new("foo/bar"); let bq = Path::new("baz/quux.txt"); assert!(fb.is_ancestor_of(&p)); } }let p = Path::new("foo/bar/baz/quux.txt"); let fb = Path::new("foo/bar"); let bq = Path::new("baz/quux.txt"); assert!(fb.is_ancestor_of(&p));
fn path_relative_from(&self, base: &Self) -> Option<Self>
Returns the Path that, were it joined to base
, would yield self
.
If no such path exists, None is returned.
If self
is absolute and base
is relative, or on Windows if both
paths refer to separate drives, an absolute path is returned.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("foo/bar/baz/quux.txt"); let fb = Path::new("foo/bar"); let bq = Path::new("baz/quux.txt"); assert_eq!(p.path_relative_from(&fb), Some(bq)); } }let p = Path::new("foo/bar/baz/quux.txt"); let fb = Path::new("foo/bar"); let bq = Path::new("baz/quux.txt"); assert_eq!(p.path_relative_from(&fb), Some(bq));
fn ends_with_path(&self, child: &Self) -> bool
Returns whether the relative path child
is a suffix of self
.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("foo/bar/baz/quux.txt"); let bq = Path::new("baz/quux.txt"); assert!(p.ends_with_path(&bq)); } }let p = Path::new("foo/bar/baz/quux.txt"); let bq = Path::new("baz/quux.txt"); assert!(p.ends_with_path(&bq));
Provided Methods
fn new<T: BytesContainer>(path: T) -> Self
Creates a new Path from a byte vector or string. The resulting Path will always be normalized.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let path = Path::new("foo/bar"); } }let path = Path::new("foo/bar");
Panics
Panics the task if the path contains a NUL.
See individual Path impls for additional restrictions.
fn new_opt<T: BytesContainer>(path: T) -> Option<Self>
Creates a new Path from a byte vector or string, if possible. The resulting Path will always be normalized.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let x: &[u8] = b"foo\0"; assert!(Path::new_opt(x).is_none()); } }let x: &[u8] = b"foo\0"; assert!(Path::new_opt(x).is_none());
fn as_str<'a>(&'a self) -> Option<&'a str>
Returns the path as a string, if possible. If the path is not representable in utf-8, this returns None.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("/abc/def"); assert_eq!(p.as_str(), Some("/abc/def")); } }let p = Path::new("/abc/def"); assert_eq!(p.as_str(), Some("/abc/def"));
fn display<'a>(&'a self) -> Display<'a, Self>
Returns an object that implements Display
for printing paths
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("abc/def"); println!("{}", p.display()); // prints "abc/def" } }let p = Path::new("abc/def"); println!("{}", p.display()); // prints "abc/def"
fn filename_display<'a>(&'a self) -> Display<'a, Self>
Returns an object that implements Display
for printing filenames
If there is no filename, nothing will be printed.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("abc/def"); println!("{}", p.filename_display()); // prints "def" } }let p = Path::new("abc/def"); println!("{}", p.filename_display()); // prints "def"
fn dirname_str<'a>(&'a self) -> Option<&'a str>
Returns the directory component of self
, as a string, if possible.
See dirname
for details.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("abc/def/ghi"); assert_eq!(p.dirname_str(), Some("abc/def")); } }let p = Path::new("abc/def/ghi"); assert_eq!(p.dirname_str(), Some("abc/def"));
fn filename_str<'a>(&'a self) -> Option<&'a str>
Returns the file component of self
, as a string, if possible.
See filename
for details.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("abc/def/ghi"); assert_eq!(p.filename_str(), Some("ghi")); } }let p = Path::new("abc/def/ghi"); assert_eq!(p.filename_str(), Some("ghi"));
fn filestem<'a>(&'a self) -> Option<&'a [u8]>
Returns the stem of the filename of self
, as a byte vector.
The stem is the portion of the filename just before the last '.'.
If there is no '.', the entire filename is returned.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("/abc/def.txt"); assert_eq!(p.filestem(), Some(b"def")); } }let p = Path::new("/abc/def.txt"); assert_eq!(p.filestem(), Some(b"def"));
fn filestem_str<'a>(&'a self) -> Option<&'a str>
Returns the stem of the filename of self
, as a string, if possible.
See filestem
for details.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("/abc/def.txt"); assert_eq!(p.filestem_str(), Some("def")); } }let p = Path::new("/abc/def.txt"); assert_eq!(p.filestem_str(), Some("def"));
fn extension<'a>(&'a self) -> Option<&'a [u8]>
Returns the extension of the filename of self
, as an optional byte vector.
The extension is the portion of the filename just after the last '.'.
If there is no extension, None is returned.
If the filename ends in '.', the empty vector is returned.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("abc/def.txt"); assert_eq!(p.extension(), Some(b"txt")); } }let p = Path::new("abc/def.txt"); assert_eq!(p.extension(), Some(b"txt"));
fn extension_str<'a>(&'a self) -> Option<&'a str>
Returns the extension of the filename of self
, as a string, if possible.
See extension
for details.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("abc/def.txt"); assert_eq!(p.extension_str(), Some("txt")); } }let p = Path::new("abc/def.txt"); assert_eq!(p.extension_str(), Some("txt"));
fn set_filename<T: BytesContainer>(&mut self, filename: T)
Replaces the filename portion of the path with the given byte vector or string. If the replacement name is [], this is equivalent to popping the path.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let mut p = Path::new("abc/def.txt"); p.set_filename("foo.dat"); assert!(p == Path::new("abc/foo.dat")); } }let mut p = Path::new("abc/def.txt"); p.set_filename("foo.dat"); assert!(p == Path::new("abc/foo.dat"));
Panics
Panics the task if the filename contains a NUL.
fn set_extension<T: BytesContainer>(&mut self, extension: T)
Replaces the extension with the given byte vector or string.
If there is no extension in self
, this adds one.
If the argument is [] or "", this removes the extension.
If self
has no filename, this is a no-op.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let mut p = Path::new("abc/def.txt"); p.set_extension("csv"); assert_eq!(p, Path::new("abc/def.csv")); } }let mut p = Path::new("abc/def.txt"); p.set_extension("csv"); assert_eq!(p, Path::new("abc/def.csv"));
Panics
Panics the task if the extension contains a NUL.
fn with_filename<T: BytesContainer>(&self, filename: T) -> Self
Returns a new Path constructed by replacing the filename with the given
byte vector or string.
See set_filename
for details.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let mut p = Path::new("abc/def.txt"); assert_eq!(p.with_filename("foo.dat"), Path::new("abc/foo.dat")); } }let mut p = Path::new("abc/def.txt"); assert_eq!(p.with_filename("foo.dat"), Path::new("abc/foo.dat"));
Panics
Panics the task if the filename contains a NUL.
fn with_extension<T: BytesContainer>(&self, extension: T) -> Self
Returns a new Path constructed by setting the extension to the given
byte vector or string.
See set_extension
for details.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let mut p = Path::new("abc/def.txt"); assert_eq!(p.with_extension("csv"), Path::new("abc/def.csv")); } }let mut p = Path::new("abc/def.txt"); assert_eq!(p.with_extension("csv"), Path::new("abc/def.csv"));
Panics
Panics the task if the extension contains a NUL.
fn dir_path(&self) -> Self
Returns the directory component of self
, as a Path.
If self
represents the root of the filesystem hierarchy, returns self
.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("abc/def/ghi"); assert_eq!(p.dir_path(), Path::new("abc/def")); } }let p = Path::new("abc/def/ghi"); assert_eq!(p.dir_path(), Path::new("abc/def"));
fn push<T: BytesContainer>(&mut self, path: T)
Pushes a path (as a byte vector or string) onto self
.
If the argument represents an absolute path, it replaces self
.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let mut p = Path::new("foo/bar"); p.push("baz.txt"); assert_eq!(p, Path::new("foo/bar/baz.txt")); } }let mut p = Path::new("foo/bar"); p.push("baz.txt"); assert_eq!(p, Path::new("foo/bar/baz.txt"));
Panics
Panics the task if the path contains a NUL.
fn push_many<T: BytesContainer>(&mut self, paths: &[T])
Pushes multiple paths (as byte vectors or strings) onto self
.
See push
for details.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let mut p = Path::new("foo"); p.push_many(&["bar", "baz.txt"]); assert_eq!(p, Path::new("foo/bar/baz.txt")); } }let mut p = Path::new("foo"); p.push_many(&["bar", "baz.txt"]); assert_eq!(p, Path::new("foo/bar/baz.txt"));
fn join<T: BytesContainer>(&self, path: T) -> Self
Returns a new Path constructed by joining self
with the given path
(as a byte vector or string).
If the given path is absolute, the new Path will represent just that.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("/foo"); assert_eq!(p.join("bar.txt"), Path::new("/foo/bar.txt")); } }let p = Path::new("/foo"); assert_eq!(p.join("bar.txt"), Path::new("/foo/bar.txt"));
Panics
Panics the task if the path contains a NUL.
fn join_many<T: BytesContainer>(&self, paths: &[T]) -> Self
Returns a new Path constructed by joining self
with the given paths
(as byte vectors or strings).
See join
for details.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("foo"); let fbbq = Path::new("foo/bar/baz/quux.txt"); assert_eq!(p.join_many(&["bar", "baz", "quux.txt"]), fbbq); } }let p = Path::new("foo"); let fbbq = Path::new("foo/bar/baz/quux.txt"); assert_eq!(p.join_many(&["bar", "baz", "quux.txt"]), fbbq);
fn is_relative(&self) -> bool
Returns whether self
represents a relative path.
Typically this is the inverse of is_absolute
.
But for Windows paths, it also means the path is not volume-relative or
relative to the current working directory.
Example
fn main() { foo(); #[cfg(windows)] fn foo() {} #[cfg(unix)] fn foo() { let p = Path::new("abc/def"); assert!(p.is_relative()); } }let p = Path::new("abc/def"); assert!(p.is_relative());
Implementors
impl GenericPath for Path
impl GenericPath for Path