Skip to content

Commit cd3cb28

Browse files
authored
Rollup merge of #64255 - varkor:bool-to-option, r=Centril
Add methods for converting `bool` to `Option<T>` This provides a reference implementation for rust-lang/rfcs#2757.
2 parents cab2d84 + 7b3f729 commit cd3cb28

File tree

10 files changed

+71
-1
lines changed

10 files changed

+71
-1
lines changed

src/libcore/bool.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! impl bool {}
2+
3+
#[cfg(not(boostrap_stdarch_ignore_this))]
4+
#[lang = "bool"]
5+
impl bool {
6+
/// Returns `Some(t)` if the `bool` is `true`, or `None` otherwise.
7+
///
8+
/// # Examples
9+
///
10+
/// ```
11+
/// #![feature(bool_to_option)]
12+
///
13+
/// assert_eq!(false.then(0), None);
14+
/// assert_eq!(true.then(0), Some(0));
15+
/// ```
16+
#[unstable(feature = "bool_to_option", issue = "64260")]
17+
#[inline]
18+
pub fn then<T>(self, t: T) -> Option<T> {
19+
if self {
20+
Some(t)
21+
} else {
22+
None
23+
}
24+
}
25+
26+
/// Returns `Some(f())` if the `bool` is `true`, or `None` otherwise.
27+
///
28+
/// # Examples
29+
///
30+
/// ```
31+
/// #![feature(bool_to_option)]
32+
///
33+
/// assert_eq!(false.then_with(|| 0), None);
34+
/// assert_eq!(true.then_with(|| 0), Some(0));
35+
/// ```
36+
#[unstable(feature = "bool_to_option", issue = "64260")]
37+
#[inline]
38+
pub fn then_with<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
39+
if self {
40+
Some(f())
41+
} else {
42+
None
43+
}
44+
}
45+
}

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ pub mod task;
227227
pub mod alloc;
228228

229229
// note: does not need to be public
230+
mod bool;
230231
mod tuple;
231232
mod unit;
232233

src/libcore/tests/bool.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
}

src/libcore/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(bool_to_option)]
12
#![feature(bound_cloned)]
23
#![feature(box_syntax)]
34
#![feature(cell_update)]
@@ -40,6 +41,7 @@ mod any;
4041
mod array;
4142
mod ascii;
4243
mod atomic;
44+
mod bool;
4345
mod cell;
4446
mod char;
4547
mod clone;

src/librustc/middle/lang_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ pub fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> LanguageItems {
244244

245245
language_item_table! {
246246
// Variant name, Name, Method name, Target;
247+
BoolImplItem, "bool", bool_impl, Target::Impl;
247248
CharImplItem, "char", char_impl, Target::Impl;
248249
StrImplItem, "str", str_impl, Target::Impl;
249250
SliceImplItem, "slice", slice_impl, Target::Impl;

src/librustc_typeck/check/method/probe.rs

+4
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
578578
ty::Param(p) => {
579579
self.assemble_inherent_candidates_from_param(p);
580580
}
581+
ty::Bool => {
582+
let lang_def_id = lang_items.bool_impl();
583+
self.assemble_inherent_impl_for_primitive(lang_def_id);
584+
}
581585
ty::Char => {
582586
let lang_def_id = lang_items.char_impl();
583587
self.assemble_inherent_impl_for_primitive(lang_def_id);

src/librustc_typeck/coherence/inherent_impls.rs

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
6767
ty::Dynamic(ref data, ..) if data.principal_def_id().is_some() => {
6868
self.check_def_id(item, data.principal_def_id().unwrap());
6969
}
70+
ty::Bool => {
71+
self.check_primitive_impl(def_id,
72+
lang_items.bool_impl(),
73+
None,
74+
"bool",
75+
"bool",
76+
item.span);
77+
}
7078
ty::Char => {
7179
self.check_primitive_impl(def_id,
7280
lang_items.char_impl(),

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3977,7 +3977,7 @@ fn build_deref_target_impls(cx: &DocContext<'_>,
39773977
F32 => tcx.lang_items().f32_impl(),
39783978
F64 => tcx.lang_items().f64_impl(),
39793979
Char => tcx.lang_items().char_impl(),
3980-
Bool => None,
3980+
Bool => tcx.lang_items().bool_impl(),
39813981
Str => tcx.lang_items().str_impl(),
39823982
Slice => tcx.lang_items().slice_impl(),
39833983
Array => tcx.lang_items().slice_impl(),

src/librustdoc/passes/collect_intra_doc_links.rs

+1
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ fn primitive_impl(cx: &DocContext<'_>, path_str: &str) -> Option<DefId> {
679679
"f32" => tcx.lang_items().f32_impl(),
680680
"f64" => tcx.lang_items().f64_impl(),
681681
"str" => tcx.lang_items().str_impl(),
682+
"bool" => tcx.lang_items().bool_impl(),
682683
"char" => tcx.lang_items().char_impl(),
683684
_ => None,
684685
}

src/librustdoc/passes/collect_trait_impls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub fn collect_trait_impls(krate: Crate, cx: &DocContext<'_>) -> Crate {
5353
lang_items.f64_impl(),
5454
lang_items.f32_runtime_impl(),
5555
lang_items.f64_runtime_impl(),
56+
lang_items.bool_impl(),
5657
lang_items.char_impl(),
5758
lang_items.str_impl(),
5859
lang_items.slice_impl(),

0 commit comments

Comments
 (0)