diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2025-02-17 06:38:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-17 06:38:15 +0100 |
| commit | fae72a07dd43775e39d94698c165f9a007115172 (patch) | |
| tree | 1a51b320265741ca5fff76b05f7666d1d11ea641 | |
| parent | 2a25a16c570e0f7d72b272e63347c28620abb761 (diff) | |
| parent | 0f220efb1a9c1bab199baadcb01ce2129bce0037 (diff) | |
| download | rust-fae72a07dd43775e39d94698c165f9a007115172.tar.gz rust-fae72a07dd43775e39d94698c165f9a007115172.zip | |
Rollup merge of #137105 - zachs18:cow-derefpure-restrict, r=Nadrieril
Restrict DerefPure for Cow<T> impl to T = impl Clone, [impl Clone], str. Fixes #136046 `feature(deref_patterns)` tracking issue: https://github.com/rust-lang/rust/issues/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. https://github.com/rust-lang/rust/compare/master...zachs18:borrow-pure-trait
| -rw-r--r-- | library/alloc/src/borrow.rs | 12 |
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 {} |
