camino-rs / camino

Like Rust's std::path::Path, but UTF-8.
https://docs.rs/camino
Apache License 2.0
435 stars 16 forks source link

Feature request: `Cow<'a, str>` conversions #39

Open indygreg opened 1 year ago

indygreg commented 1 year ago

Thanks for this terrific crate!

I found myself needing to convert between Cow<'a, str> and Cow<'a, Utf8Path>. This is a bit awkward with the current crate APIs because you need to explicitly construct a Cow or Utf8Path/Utf8PathBuf at the call site. I'd prefer to e.g. just .into() on a Cow<'a, str> to get a Cow<'a, Utf8Path>.

What do you think about adding explicit Cow<'a, str> type conversions to Utf8Path and Utf8PathBuf?

sunshowers commented 1 year ago

Thanks for the kind words!

Sadly that isn't possible via From directly:

error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
    --> src/lib.rs:2253:1
     |
2253 | impl<'a> From<Cow<'a, str>> for Cow<'a, Utf8Path> {
     | ^^^^^^^^^------------------^^^^^-----------------
     | |        |                      |
     | |        |                      `Cow` is not defined in the current crate
     | |        `Cow` is not defined in the current crate
     | impl doesn't use only types from inside the current crate
     |
     = note: define and implement a trait or new type instead

For more information about this error, try `rustc --explain E0117`.
error: could not compile `camino` due to previous error

I guess we could add explicit methods to do this. Not sure what to call them.