relative-path icon indicating copy to clipboard operation
relative-path copied to clipboard

Add a RelativePathBuf constructor which can construct a relative path from two std paths

Open Gordon01 opened this issue 4 years ago • 2 comments

While i tried to work around the bug, I found out that I can't use .relative() for absolute paths (which is expected):

called Result::unwrap() on an Err value: FromPathError { kind: NonRelative }

But I belive it would be very useful if we can supply two absolute paths and get the RelativePath from it.

Gordon01 avatar Dec 22 '21 16:12 Gordon01

I could see us having another constructor which supports this:

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

let path = RelativePathBuf::relative_from(from, to)?;

This is a bit tricky to implement, and there are some choices that need to be made (what does it mean to build relative from two relative paths). But it's not fundamentally out of the question.

udoprog avatar Mar 05 '23 15:03 udoprog

I second this. I just hit this problem in my code. Given a shared root, I want to calculate all RelativePaths to this root. Currently there is no other choice than to use std::PathBuf::strip_prefix, of which I'm not sure it works like I think it does.

samvv avatar Feb 01 '24 12:02 samvv