Skip to content

Commit c307e90

Browse files
authored
Rollup merge of rust-lang#76139 - CDirkx:cow-is-borrowed, r=ecstatic-morse
Make `cow_is_borrowed` methods const Constify the following methods of `alloc::borrow::Cow`: - `is_borrowed` - `is_owned` Analogous to the const methods `is_some` and `is_none` for Option, and `is_ok` and `is_err` for Result. These methods are still unstable under `cow_is_borrowed`. Possible because of rust-lang#49146 (Allow if and match in constants). Tracking issue: rust-lang#65143
2 parents 4f27620 + d591829 commit c307e90

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Diff for: library/alloc/src/borrow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> {
217217
/// assert!(!bull.is_borrowed());
218218
/// ```
219219
#[unstable(feature = "cow_is_borrowed", issue = "65143")]
220-
pub fn is_borrowed(&self) -> bool {
220+
pub const fn is_borrowed(&self) -> bool {
221221
match *self {
222222
Borrowed(_) => true,
223223
Owned(_) => false,
@@ -239,7 +239,7 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> {
239239
/// assert!(!bull.is_owned());
240240
/// ```
241241
#[unstable(feature = "cow_is_borrowed", issue = "65143")]
242-
pub fn is_owned(&self) -> bool {
242+
pub const fn is_owned(&self) -> bool {
243243
!self.is_borrowed()
244244
}
245245

Diff for: src/test/ui/consts/cow-is-borrowed.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-pass
2+
3+
#![feature(cow_is_borrowed)]
4+
5+
use std::borrow::Cow;
6+
7+
fn main() {
8+
const COW: Cow<str> = Cow::Borrowed("moo");
9+
10+
const IS_BORROWED: bool = COW.is_borrowed();
11+
assert!(IS_BORROWED);
12+
13+
const IS_OWNED: bool = COW.is_owned();
14+
assert!(!IS_OWNED);
15+
}

0 commit comments

Comments
 (0)