It's often not possible to implement ToOwned for user composite types
&str and &[u8] seems to work by internal magic but it is hard to implement ToOwned for user composite types. Consider:
struct Foo<'a> {
foo: &'a str,
bar: &'a str,
foobar: &'a str
}
struct FooBuf {
foo: String,
bar: String,
foobar: String
}
It is not possible to mark that FooBuf is an owned version of Foo as it would require implementing Borrow for FooBuf. However it is not possible to implement fn borrow(&FooBuf) -> &Foo as we don't have anywhere we can store Foo. Built-in types workaround it by handling &str and similar types separately from &T and packing the actual structure there.
(I know too little about type system to create any RFR which would fix it)
The problem with
Borrowenforcing the return type to be a&Tis similar to the situation withIndexwhere it is impossible to return any sort of actual value which isn't a&T.Peter Atashian at 2017-09-30 21:17:55
There's sort of a design for an extension of the "Borrow" mechanism for hash tables here: https://github.com/bluss/ordermap/pull/10 (not in any packaged version yet). It's backwards compatible modulo type inference regressions.. :-/
The new trait allows this kind of key equivalence of Foo and FooBuf.
bluss at 2017-10-02 10:07:11