From Cow's definition:
pub enum Cow<'a, B>
where
B: 'a + ToOwned + ?Sized,
{
Borrowed(&'a B),
Owned(<B as ToOwned>::Owned),
}
So I have the following bounds: B: 'a + ToOwned + ?Sized
.
B: ToOwned
is necessary, e.g. to call Cow::to_mut
.
B: ?Sized
is a smart pointer convenience.
I don't quite see the necessity to require B: 'a
explicitly. A reference is only valid as long as its referred-to value is valid, so &'a B
"implies" B: 'a
, does it not? The same should apply "transitively" even if B
is a reference type, i.e. &'a &'b C
implies C: 'b
, 'b: 'a
and C: 'a
.
Somewhat connected to the above, the second example from the same documentation page also defines surprising bounds:
struct Items<'a, X> where [X]: ToOwned<Owned = Vec<X>> {
values: Cow<'a, [X]>,
}
impl<'a, X: Clone + 'a> Items<'a, X> where [X]: ToOwned<Owned = Vec<X>> {
fn new(v: Cow<'a, [X]>) -> Self {
Items { values: v }
}
}
Here X: Clone + 'a
.
[X]: ToOwned<Owned = Vec<X>>
already implies X: Clone
, because that's the bound on impl<T> ToOwned for [T]
.
If X
is an owned type, X: 'a
is trivially implied.
If X
was a reference, X: [X]
is built into the language, and [X]: 'a
is also necessary, as in the definition of Cow
, to be able to store &'a B
, where B
is [X]
.
Is there then a reason to define both these bounds?