impl Borrow and AsRef for CowArc (#11616)

# Objective

- Allow `HashMap<Cow<'_, T>, _>` to use `&T` as key for `HashMap::get`
- Makes `CowArc` more like `Cow`

## Solution

Implements `Borrow<T>` and `AsRef<T>` for `CowArc<T>`.
This commit is contained in:
Tristan Guichaoua 2024-01-30 15:27:53 +01:00 committed by GitHub
parent 6b40b6749e
commit 4b7ef44bb4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,4 +1,5 @@
use std::{
borrow::Borrow,
fmt::{Debug, Display},
hash::Hash,
ops::Deref,
@ -37,6 +38,20 @@ impl<'a, T: ?Sized> Deref for CowArc<'a, T> {
}
}
impl<'a, T: ?Sized> Borrow<T> for CowArc<'a, T> {
#[inline]
fn borrow(&self) -> &T {
self
}
}
impl<'a, T: ?Sized> AsRef<T> for CowArc<'a, T> {
#[inline]
fn as_ref(&self) -> &T {
self
}
}
impl<'a, T: ?Sized> CowArc<'a, T>
where
&'a T: Into<Arc<T>>,