Currently, Cow
is usable only with predefined ToOwned
relationship, so you can’t put custom types or even boxed slices like Box<[T]>
instead of Vec
or String
for the owned side of &[T]
.
This is possible to workaround with custom newtype wrappers for the borrowed variant or custom enum instead of Cow
, but it doesn’t seem that this limitation is necessary in the first place?
So what I’m thinking is changing Cow
from current
pub enum Cow<'a, B>
where
B: 'a + ToOwned + ?Sized,
{
Borrowed(&'a B),
Owned(<B as ToOwned>::Owned),
}
to something like
pub enum Cow<'a, B, O = <B as ToOwned>::Owned>
where
B: 'a + ?Sized,
O: Borrow<B>,
{
Borrowed(&'a B),
Owned(O),
}
This would still preserve backwards compatibility with existing Cow
definition by defaulting to ToOwned
version of the owned type if it’s not explicitly provided, but allow to provide your own as long as it can be borrowed as the B
type.
This wouldn’t allow doing Cow::into_owned
/ ::into_mut
with custom type - you’ll need to convert types yourself if you do this (or maybe we can rely on From
conversions?), but in my experience there is a lot of cases where Cow
is still useful as purely an optimised storage and conversions to owned don’t matter that much to be a problem then.
Does this look useful? Are there any other problems that would make this infeasible?