Skip to content

Commit 474c9fe

Browse files
Rollup merge of #151489 - bend-n:constify-all-boolean-methods-under-feature-gate-const-bool, r=jhpratt
constify boolean methods ```rs // core::bool impl bool { pub const fn then_some<T: [const] Destruct>(self, t: T) -> Option<T>; pub const fn then<T, F: [const] FnOnce() -> T + [const] Destruct>(self, f: F) -> Option<T>; pub const fn ok_or<E: [const] Destruct>(self, err: E) -> Result<(), E>; pub const fn ok_or_else<E, F: [const] FnOnce() -> E + [const] Destruct>; } ``` will make tracking issue if pr liked
2 parents ab06899 + 207dcbb commit 474c9fe

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

library/core/src/bool.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! impl bool {}
22
3+
use crate::marker::Destruct;
4+
35
impl bool {
46
/// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html),
57
/// or `None` otherwise.
@@ -29,8 +31,9 @@ impl bool {
2931
/// assert_eq!(a, 2);
3032
/// ```
3133
#[stable(feature = "bool_to_option", since = "1.62.0")]
34+
#[rustc_const_unstable(feature = "const_bool", issue = "151531")]
3235
#[inline]
33-
pub fn then_some<T>(self, t: T) -> Option<T> {
36+
pub const fn then_some<T: [const] Destruct>(self, t: T) -> Option<T> {
3437
if self { Some(t) } else { None }
3538
}
3639

@@ -57,8 +60,9 @@ impl bool {
5760
#[doc(alias = "then_with")]
5861
#[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
5962
#[rustc_diagnostic_item = "bool_then"]
63+
#[rustc_const_unstable(feature = "const_bool", issue = "151531")]
6064
#[inline]
61-
pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
65+
pub const fn then<T, F: [const] FnOnce() -> T + [const] Destruct>(self, f: F) -> Option<T> {
6266
if self { Some(f()) } else { None }
6367
}
6468

@@ -94,8 +98,9 @@ impl bool {
9498
/// assert_eq!(a, 2);
9599
/// ```
96100
#[unstable(feature = "bool_to_result", issue = "142748")]
101+
#[rustc_const_unstable(feature = "const_bool", issue = "151531")]
97102
#[inline]
98-
pub fn ok_or<E>(self, err: E) -> Result<(), E> {
103+
pub const fn ok_or<E: [const] Destruct>(self, err: E) -> Result<(), E> {
99104
if self { Ok(()) } else { Err(err) }
100105
}
101106

@@ -124,8 +129,12 @@ impl bool {
124129
/// assert_eq!(a, 1);
125130
/// ```
126131
#[unstable(feature = "bool_to_result", issue = "142748")]
132+
#[rustc_const_unstable(feature = "const_bool", issue = "151531")]
127133
#[inline]
128-
pub fn ok_or_else<E, F: FnOnce() -> E>(self, f: F) -> Result<(), E> {
134+
pub const fn ok_or_else<E, F: [const] FnOnce() -> E + [const] Destruct>(
135+
self,
136+
f: F,
137+
) -> Result<(), E> {
129138
if self { Ok(()) } else { Err(f()) }
130139
}
131140
}

library/coretests/tests/bool.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,17 @@ pub fn test_bool_not() {
8282
}
8383
}
8484

85+
const fn zero() -> i32 {
86+
0
87+
}
88+
8589
#[test]
8690
fn test_bool_to_option() {
8791
assert_eq!(false.then_some(0), None);
8892
assert_eq!(true.then_some(0), Some(0));
8993
assert_eq!(false.then(|| 0), None);
9094
assert_eq!(true.then(|| 0), Some(0));
9195

92-
/* FIXME(#110395)
93-
const fn zero() -> i32 {
94-
0
95-
}
96-
9796
const A: Option<i32> = false.then_some(0);
9897
const B: Option<i32> = true.then_some(0);
9998
const C: Option<i32> = false.then(zero);
@@ -103,7 +102,6 @@ fn test_bool_to_option() {
103102
assert_eq!(B, Some(0));
104103
assert_eq!(C, None);
105104
assert_eq!(D, Some(0));
106-
*/
107105
}
108106

109107
#[test]
@@ -112,4 +110,14 @@ fn test_bool_to_result() {
112110
assert_eq!(true.ok_or(0), Ok(()));
113111
assert_eq!(false.ok_or_else(|| 0), Err(0));
114112
assert_eq!(true.ok_or_else(|| 0), Ok(()));
113+
114+
const A: Result<(), i32> = false.ok_or(0);
115+
const B: Result<(), i32> = true.ok_or(0);
116+
const C: Result<(), i32> = false.ok_or_else(zero);
117+
const D: Result<(), i32> = true.ok_or_else(zero);
118+
119+
assert_eq!(A, Err(0));
120+
assert_eq!(B, Ok(()));
121+
assert_eq!(C, Err(0));
122+
assert_eq!(D, Ok(()));
115123
}

library/coretests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#![feature(clamp_magnitude)]
1818
#![feature(clone_to_uninit)]
1919
#![feature(const_array)]
20+
#![feature(const_bool)]
2021
#![feature(const_cell_traits)]
2122
#![feature(const_clone)]
2223
#![feature(const_cmp)]

0 commit comments

Comments
 (0)