[][src]Struct ironpath::Relative

pub struct 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 another Relative path without having to revalidatet the invariants.
  • Joining a Relative to an Absolute will always produce a path that refers to a child of the Absolute, unless the directory named by the Absolute 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]

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_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl Ord for Relative[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]

🔬 This is a nightly-only experimental API. (clamp)

Restrict a value to a certain interval. Read more

impl Debug for Relative[src]

impl Hash for Relative[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

Auto Trait Implementations

impl Send for Relative

impl Sync for Relative

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.