From 0942b17cdc04cc2c0705dfcc4559b3b865ec27c2 Mon Sep 17 00:00:00 2001 From: Zachary S Date: Sat, 15 Feb 2025 19:09:38 -0600 Subject: [PATCH] Restrict DerefPure for Cow impl to T = impl Clone, [impl Clone], str. --- alloc/src/borrow.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/alloc/src/borrow.rs b/alloc/src/borrow.rs index dbfd2e74abee6..17dad3277b95d 100644 --- a/alloc/src/borrow.rs +++ b/alloc/src/borrow.rs @@ -340,8 +340,18 @@ where } } +// `Cow<'_, T>` can only implement `DerefPure` if `>` (and `BorrowMut`) is trusted. +// For now, we restrict `DerefPure for Cow` to `T: Sized` (`T as Borrow` is trusted), +// `str` (`String as Borrow` is trusted) and `[T]` (`Vec as Borrow<[T]>` is trusted). +// In the future, a `BorrowPure` trait analogous to `DerefPure` might generalize this. #[unstable(feature = "deref_pure_trait", issue = "87121")] -unsafe impl DerefPure for Cow<'_, B> where B::Owned: Borrow {} +unsafe impl 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 DerefPure for Cow<'_, [T]> {} #[stable(feature = "rust1", since = "1.0.0")] impl Eq for Cow<'_, B> where B: Eq + ToOwned {}