Skip to content

Commit 4888488

Browse files
authored
Rollup merge of rust-lang#137105 - zachs18:cow-derefpure-restrict, r=Nadrieril
Restrict DerefPure for Cow<T> impl to T = impl Clone, [impl Clone], str. Fixes rust-lang#136046 `feature(deref_patterns)` tracking issue: rust-lang#87121 `Cow<'_, T>` should only implement `DerefPure` if its `Deref` impl is pure, which requires `<T::Owned as Borrow<T>>::borrow` to be pure. This PR restricts `impl DerefPure for Cow<'_, T>` to `T: Sized + Clone`, `T = [U: Clone]`, and `T = str` (for all of whom `<T::Owned as Borrow<T>>::borrow` is implemented in the stdlib and is pure). cc ``@Nadrieril`` ------ An alternate approach would be to introduce a new `unsafe trait BorrowPure<T>` analogous to `DerefPure` that could be implemented for `T: Sized`, `&T`, `&mut T`, `String`, `Vec`, `Box`, `PathBuf`, `OsString`, etc. rust-lang/rust@master...zachs18:borrow-pure-trait
2 parents 78f5f68 + 263e4b2 commit 4888488

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

alloc/src/borrow.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,18 @@ where
340340
}
341341
}
342342

343+
// `Cow<'_, T>` can only implement `DerefPure` if `<T::Owned as Borrow<T>>` (and `BorrowMut<T>`) is trusted.
344+
// For now, we restrict `DerefPure for Cow<T>` to `T: Sized` (`T as Borrow<T>` is trusted),
345+
// `str` (`String as Borrow<str>` is trusted) and `[T]` (`Vec<T> as Borrow<[T]>` is trusted).
346+
// In the future, a `BorrowPure<T>` trait analogous to `DerefPure` might generalize this.
343347
#[unstable(feature = "deref_pure_trait", issue = "87121")]
344-
unsafe impl<B: ?Sized + ToOwned> DerefPure for Cow<'_, B> where B::Owned: Borrow<B> {}
348+
unsafe impl<T: Clone> DerefPure for Cow<'_, T> {}
349+
#[cfg(not(no_global_oom_handling))]
350+
#[unstable(feature = "deref_pure_trait", issue = "87121")]
351+
unsafe impl DerefPure for Cow<'_, str> {}
352+
#[cfg(not(no_global_oom_handling))]
353+
#[unstable(feature = "deref_pure_trait", issue = "87121")]
354+
unsafe impl<T: Clone> DerefPure for Cow<'_, [T]> {}
345355

346356
#[stable(feature = "rust1", since = "1.0.0")]
347357
impl<B: ?Sized> Eq for Cow<'_, B> where B: Eq + ToOwned {}

0 commit comments

Comments
 (0)