Trait relative_path::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.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl PathExt for Path

source§

impl PathExt for PathBuf

Implementors§