Skip to content

Commit b335056

Browse files
committed
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`.
1 parent ca98d4d commit b335056

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
@@ -746,7 +746,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
746746
}
747747
}
748748

749-
if projection.is_owned() {
749+
if Cow::is_owned(&projection) {
750750
place.projection = self.tcx.mk_place_elems(&projection);
751751
}
752752

library/alloc/src/borrow.rs

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

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

254262
/// 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)