camino-rs / camino

Like Rust's std::path::Path, but UTF-8.

Home Page:https://docs.rs/camino

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`AsRef<Path>` instead of `AsRef<Self>` in `Utf8Path::starts_with`?

smoelius opened this issue · comments

Mostly because I am curious, what is the rationale for requiring base to implement AsRef<Path> instead of AsRef<Self> in Utf8Path::starts_with?

pub fn starts_with(&self, base: impl AsRef<Path>) -> bool {

commented

Hi @smoelius--great question, thanks for asking!

Consider what happens if you have a Path -- it is reasonable to ask if a Utf8Path starts with a Path, and camino can answer that for you without having to convert the Path to a Utf8Path (you could always return false if the conversion fails, but it's an extra annoyance).

The same applies to other third party types that implement AsRef<Path>. consider what happens if there's another third party type MyPath which is unaware of camino but implements AsRef<Path>.

So impl AsRef<Path> is more general than AsRef<Self>.

Got it. Thank you for the explanation.