relative_path

Trait PathExt

Source
pub trait PathExt: Sealed {
    // Required method
    fn relative_to<P>(
        &self,
        root: P,
    ) -> Result<RelativePathBuf, RelativeToError>
       where P: AsRef<Path>;
}
Expand description

Extension methods for Path and PathBuf to for building and interacting with RelativePath.

Required Methods§

Source

fn relative_to<P>(&self, root: P) -> Result<RelativePathBuf, RelativeToError>
where P: AsRef<Path>,

Build a relative path from the provided directory to self.

Producing a relative path like this is a logical operation and does not guarantee that the constructed path corresponds to what the filesystem would do. On Linux for example symbolic links could mean that the logical path doesn’t correspond to the filesystem path.

§Examples
use std::path::Path;
use relative_path::{RelativePath, PathExt};

let baz = Path::new("/foo/bar/baz");
let bar = Path::new("/foo/bar");
let qux = Path::new("/foo/bar/qux");

assert_eq!(bar.relative_to(baz)?, RelativePath::new("../"));
assert_eq!(baz.relative_to(bar)?, RelativePath::new("baz"));
assert_eq!(qux.relative_to(baz)?, RelativePath::new("../qux"));
assert_eq!(baz.relative_to(qux)?, RelativePath::new("../baz"));
assert_eq!(bar.relative_to(qux)?, RelativePath::new("../"));
§Errors

Errors in case the provided path contains components which cannot be converted into a relative path as needed, such as non-utf8 data.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl PathExt for Path

Source§

impl PathExt for PathBuf

Implementors§