The idea is I have an Attribute trait which I implement for numeric primitives and &str.
trait Attrib {
type Type;
fn get() -> Self::Type;
fn set(val: impl Into<Self::Type>);
}
But I would want get() to return String but set() take &str. The playground code with Into works, but it's an unnecessary allocation.
An easy solution would be to define the trait like this:
trait Attrib {
type RType;
type SType;
fn get() -> Self::RType;
fn set(val: Self::SType);
}
impl Attrib for &str {
type SType = &str;
type RType = String;
fn get() -> Self::RType { ... }
fn set(val: Self::SType) { ... }
}
I was wondering if it's possible to keep a single associated Type but make get and set generic with some mixture of ToOwned and Borrow bounds. I couldn't make anything working.