Rollup merge of #138217 - theemathas:cow_is_owned_borrowed_associated, r=dtolnay

Turn `Cow::is_borrowed,is_owned` into associated functions.

This is done because `Cow` implements `Deref`. Therefore, to avoid conflicts with an inner type having a method of the same name, we use an associated method, like `Box::into_raw`.

Tracking issue: #65143
This commit is contained in:
Jacob Pratt
2025-10-30 02:43:41 -04:00
committed by GitHub
3 changed files with 19 additions and 11 deletions

View File

@@ -921,7 +921,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
}
}
if projection.is_owned() {
if Cow::is_owned(&projection) {
place.projection = self.tcx.mk_place_elems(&projection);
}

View File

@@ -215,6 +215,10 @@ impl<B: ?Sized + ToOwned> Clone for Cow<'_, B> {
impl<B: ?Sized + ToOwned> Cow<'_, B> {
/// Returns true if the data is borrowed, i.e. if `to_mut` would require additional work.
///
/// Note: this is an associated function, which means that you have to call
/// it as `Cow::is_borrowed(&c)` instead of `c.is_borrowed()`. This is so
/// that there is no conflict with a method on the inner type.
///
/// # Examples
///
/// ```
@@ -222,14 +226,14 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> {
/// use std::borrow::Cow;
///
/// let cow = Cow::Borrowed("moo");
/// assert!(cow.is_borrowed());
/// assert!(Cow::is_borrowed(&cow));
///
/// let bull: Cow<'_, str> = Cow::Owned("...moo?".to_string());
/// assert!(!bull.is_borrowed());
/// assert!(!Cow::is_borrowed(&bull));
/// ```
#[unstable(feature = "cow_is_borrowed", issue = "65143")]
pub const fn is_borrowed(&self) -> bool {
match *self {
pub const fn is_borrowed(c: &Self) -> bool {
match *c {
Borrowed(_) => true,
Owned(_) => false,
}
@@ -237,6 +241,10 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> {
/// Returns true if the data is owned, i.e. if `to_mut` would be a no-op.
///
/// Note: this is an associated function, which means that you have to call
/// it as `Cow::is_owned(&c)` instead of `c.is_owned()`. This is so that
/// there is no conflict with a method on the inner type.
///
/// # Examples
///
/// ```
@@ -244,14 +252,14 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> {
/// use std::borrow::Cow;
///
/// let cow: Cow<'_, str> = Cow::Owned("moo".to_string());
/// assert!(cow.is_owned());
/// assert!(Cow::is_owned(&cow));
///
/// let bull = Cow::Borrowed("...moo?");
/// assert!(!bull.is_owned());
/// assert!(!Cow::is_owned(&bull));
/// ```
#[unstable(feature = "cow_is_borrowed", issue = "65143")]
pub const fn is_owned(&self) -> bool {
!self.is_borrowed()
pub const fn is_owned(c: &Self) -> bool {
!Cow::is_borrowed(c)
}
/// Acquires a mutable reference to the owned form of the data.

View File

@@ -52,9 +52,9 @@ fn cow_const() {
const COW: Cow<'_, str> = Cow::Borrowed("moo");
const IS_BORROWED: bool = COW.is_borrowed();
const IS_BORROWED: bool = Cow::is_borrowed(&COW);
assert!(IS_BORROWED);
const IS_OWNED: bool = COW.is_owned();
const IS_OWNED: bool = Cow::is_owned(&COW);
assert!(!IS_OWNED);
}