File tree Expand file tree Collapse file tree 3 files changed +50
-1
lines changed Expand file tree Collapse file tree 3 files changed +50
-1
lines changed Original file line number Diff line number Diff line change 44
55#[ cfg( not( boostrap_stdarch_ignore_this) ) ]
66#[ lang = "bool" ]
7- impl bool { }
7+ impl bool {
8+ /// Returns `Some(t)` if the `bool` is `true`, or `None` otherwise.
9+ ///
10+ /// # Examples
11+ ///
12+ /// ```
13+ /// #![feature(bool_to_option)]
14+ ///
15+ /// assert_eq!(false.then(0), None);
16+ /// assert_eq!(true.then(0), Some(0));
17+ /// ```
18+ #[ unstable( feature = "bool_to_option" , issue = "0" ) ]
19+ #[ inline]
20+ pub fn then < T > ( self , t : T ) -> Option < T > {
21+ if self {
22+ Some ( t)
23+ } else {
24+ None
25+ }
26+ }
27+
28+ /// Returns `Some(f())` if the `bool` is `true`, or `None` otherwise.
29+ ///
30+ /// # Examples
31+ ///
32+ /// ```
33+ /// #![feature(bool_to_option)]
34+ ///
35+ /// assert_eq!(false.then_with(|| 0), None);
36+ /// assert_eq!(true.then_with(|| 0), Some(0));
37+ /// ```
38+ #[ unstable( feature = "bool_to_option" , issue = "0" ) ]
39+ #[ inline]
40+ pub fn then_with < T , F : FnOnce ( ) -> T > ( self , f : F ) -> Option < T > {
41+ if self {
42+ Some ( f ( ) )
43+ } else {
44+ None
45+ }
46+ }
47+ }
Original file line number Diff line number Diff line change 1+ #[ test]
2+ fn test_bool_to_option ( ) {
3+ assert_eq ! ( false . then( 0 ) , None ) ;
4+ assert_eq ! ( true . then( 0 ) , Some ( 0 ) ) ;
5+ assert_eq ! ( false . then_with( || 0 ) , None ) ;
6+ assert_eq ! ( true . then_with( || 0 ) , Some ( 0 ) ) ;
7+ }
Original file line number Diff line number Diff line change 1+ #![ feature( bool_to_option) ]
12#![ feature( bound_cloned) ]
23#![ feature( box_syntax) ]
34#![ feature( cell_update) ]
@@ -40,6 +41,7 @@ mod any;
4041mod array;
4142mod ascii;
4243mod atomic;
44+ mod bool;
4345mod cell;
4446mod char;
4547mod clone;
You can’t perform that action at this time.
0 commit comments