I have some Trait Named
trait Named {
fn name(&self) -> &str;
}
and a struct implementing it
struct Struct {
name: String
}
impl Named for Struct {
fn name(&self) -> &str {
&self.name
}
}
Now I found out that I need to support different versions of Owned/Borrowed data (like PathBuf
and Path
)
How can I express this with a generic Trait such that I can have struct
struct StructContainsPathBuf {
name: PathBuf
}
impl Named<Path> for StructContainsPathBuf {
fn name(&self) -> &Path {
&self.name
}
}
I basically need a way express the following
trait<OwnedType> Named {
fn named(&self) -> ThisGuyEvaluatesToTheBorrowedVersionOf<OwnedType>; // String->&str, PathBuf->&Path
}
Introducing just the borrowed version &str
/Path
as type parameter does not work because then you get the compiler trying to copy it which is unintended.
I think it should somehow be possible with Borrowed
/ToOwned
but I failed trying myself.