[−][src]Struct ironpath::Relative
A relative path that may be joined to an absolute path.
This path obeys the following invariants:
- It is relative, containing no prefix or root directory components.
- It contains only named path components, no
/./
or/../
ones. - It uses the platform-native path-component separator (
/
on POSIX,\
on Windows).
Therefore:
- It's always reasonably straight-forward for humans to understand.
- It can safely be appended to an
Absolute
path or anotherRelative
path without having to revalidatet the invariants. - Joining a
Relative
to anAbsolute
will always produce a path that refers to a child of theAbsolute
, unless the directory named by theAbsolute
contains a symilnk to a directory outside it.
Since this type implements AsRef<Path>
, it can be used with almost any
standard library function that expects a path, but you probably only want
to join it to an Absolute
path.
Methods
impl Relative
[src]
pub fn new<P: AsRef<Path>>(path: P) -> Result<Relative, Error>
[src]
Convert an arbitrary path to follow the rules for a Relative
path.
- Any
/./
components in the path are removed. - If a named component is followed by a
/../
component, they cancel each other out and both are removed. We cannot resolve symlinks here since we do not know what absolute path this path is relative to.
Performance
This validation is performed entirely in memory, with no reference to the filesystem.
Errors
Returns Error::PathIsAbsolute
if the given path contains prefix or
root directory components, like /usr/share
or C:file.txt
.
Returns Error::RelativePathEscapesPrefix
if any /../
components
cannot be normalized away, like a/b/../../../c
.
pub fn join<P: AsRef<Path>>(&self, path: P) -> Result<Relative, Error>
[src]
Clone this path, attempting to add an arbitrary relative path on the end.
Performance
An expression like:
other_relative_path.join(path)?
...is the same as doing:
other_relative_path.join_relative(&Relative::new(path)?)
...and therefore involves the same allocation and other costs as
calling Relative::new()
yourself.
If you plan on joining the same relative path to many other Relative
paths, it's more efficient to call Relative::new()
once yourself and
call .join_relative()
each time.
Errors
Returns the same errors as Relative::new()
.
Examples
let config_base = Relative::new("SuperSoftwareCo/MyCoolApp")?; let video_config = config_base.join("video.cfg")?; let audio_config = config_base.join("audio.cfg")?;
pub fn join_relative(&self, tail: &Relative) -> Relative
[src]
Clone this path, adding the given Relative
path on the end.
Performance
Since Relative
paths all have the same invariants, no additional
checks or processing need to be done, just straight concatenation.
pub fn as_path(&self) -> &Path
[src]
Coerces to a Path
slice.
Since Relative
implements AsRef<Path>
, this method is not needed
very often—you can often just pass it directly to the thing that needs
a Path
.
Examples
If you really, really need to convert a Relative
to a PathBuf
:
let owned_path: std::path::PathBuf = relative.as_path().into();
pub fn as_os_str(&self) -> &OsStr
[src]
Coerces to an OsStr
slice.
Since Relative
implements AsRef<OsStr>
, this method is not needed
very often—you can often just pass it directly to the thing that needs
an OsStr
.
Examples
If you really, really need to convert a Relative
to a OsString
:
let owned_string: std::ffi::OsString = relative.as_os_str().into();
Trait Implementations
impl Eq for Relative
[src]
impl PartialOrd<Relative> for Relative
[src]
fn partial_cmp(&self, other: &Relative) -> Option<Ordering>
[src]
fn lt(&self, other: &Relative) -> bool
[src]
fn le(&self, other: &Relative) -> bool
[src]
fn gt(&self, other: &Relative) -> bool
[src]
fn ge(&self, other: &Relative) -> bool
[src]
impl PartialEq<Relative> for Relative
[src]
impl AsRef<Path> for Relative
[src]
impl AsRef<OsStr> for Relative
[src]
impl Clone for Relative
[src]
fn clone(&self) -> Relative
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl Ord for Relative
[src]
fn cmp(&self, other: &Relative) -> Ordering
[src]
fn max(self, other: Self) -> Self
1.21.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.21.0[src]
Compares and returns the minimum of two values. Read more
fn clamp(self, min: Self, max: Self) -> Self
[src]
clamp
)Restrict a value to a certain interval. Read more
impl Debug for Relative
[src]
impl Hash for Relative
[src]
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,