Need to convert a &str into a Cow<'a, Path>, but no blanket impl for T: AsRef<Path> exists. Alternatives?

I have the following function signature:

fn output_dir<'a, T: Into<Cow<'a, Path>>>(output_dir: T);

And I want to be able to pass in a &str. However, I am getting the error:

the trait `std::convert::From<&str>` is not implemented for `std::borrow::Cow<'_, std::path::Path>`

So this immediately leads to the question: if I can convert a &str into a Path, why can I not turn this easily into a Cow?

It looks like we don’t have a blanket impl for this kind of thing, e.g. something like:

impl<'a, T, U> From<T> for Cow<'a, U>
where T: AsRef<U>

Is there some alternative to this that I am not thinking of?

Would it be interesting to have an RFC to include this in stdlib?

That impl would conflict with From<T> for T, which is why it doesn’t exist. The best you can do is create the Cow directly or create the Path/PathBuf directly and pass that.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.