Is there an easier way to implement From/TryFrom for String, &String, &str, etc. without writing so many impl blocks?

59 points Tuckertcs 28 comments

For example, I have this code:

impl From<&str> for Foo {
    fn from(value: &str) -> Self {
        todo!()
    }
}

impl From<&String> for Foo {
    fn from(value: &String) -> Self {
        Self::from(value.as_str())
    }
}

impl From<String> for Foo {
    fn from(value: String) -> Self {
        Self::from(value.as_str())
    }
}

The three impl blocks seem a bit redundant, but they are technically different.

For some cases, you may want to treat them differently (to avoid cloning, for example), but if they all use the same underlying code, is there a way to use just one impl block?

For example, something like this (which of course doesn't compile):

impl From<Into<&str>> for Foo {
    fn from(value: impl Into<&str>) -> Self {
        todo!()
    }
}