diff options
| author | Tyler Mandry <tmandry@gmail.com> | 2020-08-31 19:18:21 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-31 19:18:21 -0700 |
| commit | c307e90daaaba8b66e3335a82d343f7a62361c02 (patch) | |
| tree | 1f6e7c947c63cfe2f04214e39c6c9d7519289572 | |
| parent | 4f276202f52ca6cfd122cc88ea304b197ab06a02 (diff) | |
| parent | d591829ed0c9c589c0b05e9cabf1b90d30ed19a9 (diff) | |
| download | rust-c307e90daaaba8b66e3335a82d343f7a62361c02.tar.gz rust-c307e90daaaba8b66e3335a82d343f7a62361c02.zip | |
Rollup merge of #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 #49146 (Allow if and match in constants). Tracking issue: #65143
| -rw-r--r-- | library/alloc/src/borrow.rs | 4 | ||||
| -rw-r--r-- | src/test/ui/consts/cow-is-borrowed.rs | 15 |
2 files changed, 17 insertions, 2 deletions
diff --git a/library/alloc/src/borrow.rs b/library/alloc/src/borrow.rs index 51c233a21f1..e7260f3956c 100644 --- a/library/alloc/src/borrow.rs +++ b/library/alloc/src/borrow.rs @@ -217,7 +217,7 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> { /// assert!(!bull.is_borrowed()); /// ``` #[unstable(feature = "cow_is_borrowed", issue = "65143")] - pub fn is_borrowed(&self) -> bool { + pub const fn is_borrowed(&self) -> bool { match *self { Borrowed(_) => true, Owned(_) => false, @@ -239,7 +239,7 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> { /// assert!(!bull.is_owned()); /// ``` #[unstable(feature = "cow_is_borrowed", issue = "65143")] - pub fn is_owned(&self) -> bool { + pub const fn is_owned(&self) -> bool { !self.is_borrowed() } diff --git a/src/test/ui/consts/cow-is-borrowed.rs b/src/test/ui/consts/cow-is-borrowed.rs new file mode 100644 index 00000000000..adebe20f5a2 --- /dev/null +++ b/src/test/ui/consts/cow-is-borrowed.rs @@ -0,0 +1,15 @@ +// run-pass + +#![feature(cow_is_borrowed)] + +use std::borrow::Cow; + +fn main() { + const COW: Cow<str> = Cow::Borrowed("moo"); + + const IS_BORROWED: bool = COW.is_borrowed(); + assert!(IS_BORROWED); + + const IS_OWNED: bool = COW.is_owned(); + assert!(!IS_OWNED); +} |
