about summary refs log tree commit diff
path: root/library/alloc/src
diff options
context:
space:
mode:
authorZachary S <zasample18+github@gmail.com>2025-02-15 19:09:38 -0600
committerZachary S <zasample18+github@gmail.com>2025-02-15 21:02:00 -0600
commit0f220efb1a9c1bab199baadcb01ce2129bce0037 (patch)
treeba84a3a486fdc9f9a7a073b5d9cfcfaf1bb5e000 /library/alloc/src
parent9cd60bd2ccc41bc898d2ad86728f14035d2df72d (diff)
downloadrust-0f220efb1a9c1bab199baadcb01ce2129bce0037.tar.gz
rust-0f220efb1a9c1bab199baadcb01ce2129bce0037.zip
Restrict DerefPure for Cow<T> impl to T = impl Clone, [impl Clone], str.
Diffstat (limited to 'library/alloc/src')
-rw-r--r--library/alloc/src/borrow.rs12
1 files changed, 11 insertions, 1 deletions
diff --git a/library/alloc/src/borrow.rs b/library/alloc/src/borrow.rs
index dbfd2e74abe..17dad3277b9 100644
--- a/library/alloc/src/borrow.rs
+++ b/library/alloc/src/borrow.rs
@@ -340,8 +340,18 @@ where
     }
 }
 
+// `Cow<'_, T>` can only implement `DerefPure` if `<T::Owned as Borrow<T>>` (and `BorrowMut<T>`) is trusted.
+// For now, we restrict `DerefPure for Cow<T>` to `T: Sized` (`T as Borrow<T>` is trusted),
+// `str` (`String as Borrow<str>` is trusted) and `[T]` (`Vec<T> as Borrow<[T]>` is trusted).
+// In the future, a `BorrowPure<T>` trait analogous to `DerefPure` might generalize this.
 #[unstable(feature = "deref_pure_trait", issue = "87121")]
-unsafe impl<B: ?Sized + ToOwned> DerefPure for Cow<'_, B> where B::Owned: Borrow<B> {}
+unsafe impl<T: Clone> DerefPure for Cow<'_, T> {}
+#[cfg(not(no_global_oom_handling))]
+#[unstable(feature = "deref_pure_trait", issue = "87121")]
+unsafe impl DerefPure for Cow<'_, str> {}
+#[cfg(not(no_global_oom_handling))]
+#[unstable(feature = "deref_pure_trait", issue = "87121")]
+unsafe impl<T: Clone> DerefPure for Cow<'_, [T]> {}
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<B: ?Sized> Eq for Cow<'_, B> where B: Eq + ToOwned {}