From 9fb6730c80eb6ae0ad60708fb8a2ff9ef08d4e2b Mon Sep 17 00:00:00 2001 From: Ohad Ravid Date: Thu, 21 Apr 2022 23:02:38 +0300 Subject: [PATCH] Make `Cow` covariant w.r.t `Owned` --- library/alloc/src/borrow.rs | 4 ++-- library/alloc/tests/borrow.rs | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/borrow.rs b/library/alloc/src/borrow.rs index 8b13e36c4b3c7..a3e661172059a 100644 --- a/library/alloc/src/borrow.rs +++ b/library/alloc/src/borrow.rs @@ -178,7 +178,7 @@ where /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[cfg_attr(not(test), rustc_diagnostic_item = "Cow")] -pub enum Cow<'a, B: ?Sized + 'a> +pub enum Cow<'a, B: ?Sized + 'a, O = ::Owned> where B: ToOwned, { @@ -188,7 +188,7 @@ where /// Owned data. #[stable(feature = "rust1", since = "1.0.0")] - Owned(#[stable(feature = "rust1", since = "1.0.0")] ::Owned), + Owned(#[stable(feature = "rust1", since = "1.0.0")] O), } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/alloc/tests/borrow.rs b/library/alloc/tests/borrow.rs index 57976aa6cdfdf..75b5867496e15 100644 --- a/library/alloc/tests/borrow.rs +++ b/library/alloc/tests/borrow.rs @@ -58,3 +58,14 @@ fn cow_const() { const IS_OWNED: bool = COW.is_owned(); assert!(!IS_OWNED); } + +#[allow(dead_code)] +fn assert_covariance() { + fn cow<'new>(c: Cow<'static, str>) -> Cow<'new, str> { + c + } + + fn cow_cow<'new>(c: Cow<'static, Cow<'static, str>>) -> Cow<'new, Cow<'new, str>> { + c + } +}