Skip to content

Commit f1a0dfd

Browse files
authored
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
2 parents 292be5c + b335056 commit f1a0dfd

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
921921
}
922922
}
923923

924-
if projection.is_owned() {
924+
if Cow::is_owned(&projection) {
925925
place.projection = self.tcx.mk_place_elems(&projection);
926926
}
927927

library/alloc/src/borrow.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,43 +215,51 @@ impl<B: ?Sized + ToOwned> Clone for Cow<'_, B> {
215215
impl<B: ?Sized + ToOwned> Cow<'_, B> {
216216
/// Returns true if the data is borrowed, i.e. if `to_mut` would require additional work.
217217
///
218+
/// Note: this is an associated function, which means that you have to call
219+
/// it as `Cow::is_borrowed(&c)` instead of `c.is_borrowed()`. This is so
220+
/// that there is no conflict with a method on the inner type.
221+
///
218222
/// # Examples
219223
///
220224
/// ```
221225
/// #![feature(cow_is_borrowed)]
222226
/// use std::borrow::Cow;
223227
///
224228
/// let cow = Cow::Borrowed("moo");
225-
/// assert!(cow.is_borrowed());
229+
/// assert!(Cow::is_borrowed(&cow));
226230
///
227231
/// let bull: Cow<'_, str> = Cow::Owned("...moo?".to_string());
228-
/// assert!(!bull.is_borrowed());
232+
/// assert!(!Cow::is_borrowed(&bull));
229233
/// ```
230234
#[unstable(feature = "cow_is_borrowed", issue = "65143")]
231-
pub const fn is_borrowed(&self) -> bool {
232-
match *self {
235+
pub const fn is_borrowed(c: &Self) -> bool {
236+
match *c {
233237
Borrowed(_) => true,
234238
Owned(_) => false,
235239
}
236240
}
237241

238242
/// Returns true if the data is owned, i.e. if `to_mut` would be a no-op.
239243
///
244+
/// Note: this is an associated function, which means that you have to call
245+
/// it as `Cow::is_owned(&c)` instead of `c.is_owned()`. This is so that
246+
/// there is no conflict with a method on the inner type.
247+
///
240248
/// # Examples
241249
///
242250
/// ```
243251
/// #![feature(cow_is_borrowed)]
244252
/// use std::borrow::Cow;
245253
///
246254
/// let cow: Cow<'_, str> = Cow::Owned("moo".to_string());
247-
/// assert!(cow.is_owned());
255+
/// assert!(Cow::is_owned(&cow));
248256
///
249257
/// let bull = Cow::Borrowed("...moo?");
250-
/// assert!(!bull.is_owned());
258+
/// assert!(!Cow::is_owned(&bull));
251259
/// ```
252260
#[unstable(feature = "cow_is_borrowed", issue = "65143")]
253-
pub const fn is_owned(&self) -> bool {
254-
!self.is_borrowed()
261+
pub const fn is_owned(c: &Self) -> bool {
262+
!Cow::is_borrowed(c)
255263
}
256264

257265
/// Acquires a mutable reference to the owned form of the data.

library/alloctests/tests/borrow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ fn cow_const() {
5252

5353
const COW: Cow<'_, str> = Cow::Borrowed("moo");
5454

55-
const IS_BORROWED: bool = COW.is_borrowed();
55+
const IS_BORROWED: bool = Cow::is_borrowed(&COW);
5656
assert!(IS_BORROWED);
5757

58-
const IS_OWNED: bool = COW.is_owned();
58+
const IS_OWNED: bool = Cow::is_owned(&COW);
5959
assert!(!IS_OWNED);
6060
}

0 commit comments

Comments
 (0)