File tree 3 files changed +26
-2
lines changed
3 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -14,8 +14,12 @@ impl bool {
14
14
/// assert_eq!(true.then_some(0), Some(0));
15
15
/// ```
16
16
#[ unstable( feature = "bool_to_option" , issue = "80967" ) ]
17
+ #[ rustc_const_unstable( feature = "const_bool_to_option" , issue = "91917" ) ]
17
18
#[ inline]
18
- pub fn then_some < T > ( self , t : T ) -> Option < T > {
19
+ pub const fn then_some < T > ( self , t : T ) -> Option < T >
20
+ where
21
+ T : ~const Drop ,
22
+ {
19
23
if self { Some ( t) } else { None }
20
24
}
21
25
@@ -29,8 +33,13 @@ impl bool {
29
33
/// assert_eq!(true.then(|| 0), Some(0));
30
34
/// ```
31
35
#[ stable( feature = "lazy_bool_to_option" , since = "1.50.0" ) ]
36
+ #[ rustc_const_unstable( feature = "const_bool_to_option" , issue = "91917" ) ]
32
37
#[ inline]
33
- pub fn then < T , F : FnOnce ( ) -> T > ( self , f : F ) -> Option < T > {
38
+ pub const fn then < T , F > ( self , f : F ) -> Option < T >
39
+ where
40
+ F : ~const FnOnce ( ) -> T ,
41
+ F : ~const Drop ,
42
+ {
34
43
if self { Some ( f ( ) ) } else { None }
35
44
}
36
45
}
Original file line number Diff line number Diff line change @@ -88,4 +88,18 @@ fn test_bool_to_option() {
88
88
assert_eq ! ( true . then_some( 0 ) , Some ( 0 ) ) ;
89
89
assert_eq ! ( false . then( || 0 ) , None ) ;
90
90
assert_eq ! ( true . then( || 0 ) , Some ( 0 ) ) ;
91
+
92
+ const fn zero ( ) -> i32 {
93
+ 0
94
+ }
95
+
96
+ const A : Option < i32 > = false . then_some ( 0 ) ;
97
+ const B : Option < i32 > = true . then_some ( 0 ) ;
98
+ const C : Option < i32 > = false . then ( zero) ;
99
+ const D : Option < i32 > = true . then ( zero) ;
100
+
101
+ assert_eq ! ( A , None ) ;
102
+ assert_eq ! ( B , Some ( 0 ) ) ;
103
+ assert_eq ! ( C , None ) ;
104
+ assert_eq ! ( D , Some ( 0 ) ) ;
91
105
}
Original file line number Diff line number Diff line change 8
8
#![ feature( cfg_panic) ]
9
9
#![ feature( cfg_target_has_atomic) ]
10
10
#![ feature( const_assume) ]
11
+ #![ feature( const_bool_to_option) ]
11
12
#![ feature( const_cell_into_inner) ]
12
13
#![ feature( const_convert) ]
13
14
#![ feature( const_maybe_uninit_as_mut_ptr) ]
You can’t perform that action at this time.
0 commit comments