1use crate::marker::Destruct;
2use crate::{convert, ops};
3
4#[stable(feature = "control_flow_enum_type", since = "1.55.0")]
83#[rustc_diagnostic_item = "ControlFlow"]
84#[must_use]
85#[derive(#[automatically_derived]
#[stable(feature = "control_flow_enum_type", since = "1.55.0")]
impl<B: crate::marker::Copy, C: crate::marker::Copy> crate::marker::Copy for
ControlFlow<B, C> {
}Copy, #[automatically_derived]
#[stable(feature = "control_flow_enum_type", since = "1.55.0")]
impl<B: crate::fmt::Debug, C: crate::fmt::Debug> crate::fmt::Debug for
ControlFlow<B, C> {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
match self {
ControlFlow::Continue(__self_0) =>
crate::fmt::Formatter::debug_tuple_field1_finish(f,
"Continue", &__self_0),
ControlFlow::Break(__self_0) =>
crate::fmt::Formatter::debug_tuple_field1_finish(f, "Break",
&__self_0),
}
}
}Debug, #[automatically_derived]
#[stable(feature = "control_flow_enum_type", since = "1.55.0")]
impl<B: crate::hash::Hash, C: crate::hash::Hash> crate::hash::Hash for
ControlFlow<B, C> {
#[inline]
fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = crate::intrinsics::discriminant_value(self);
crate::hash::Hash::hash(&__self_discr, state);
match self {
ControlFlow::Continue(__self_0) =>
crate::hash::Hash::hash(__self_0, state),
ControlFlow::Break(__self_0) =>
crate::hash::Hash::hash(__self_0, state),
}
}
}Hash)]
88#[derive_const(#[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "control_flow_enum_type", since = "1.55.0")]
impl<B: [const] crate::clone::Clone, C: [const] crate::clone::Clone>
const crate::clone::Clone for ControlFlow<B, C> {
#[inline]
fn clone(&self) -> ControlFlow<B, C> {
match self {
ControlFlow::Continue(__self_0) =>
ControlFlow::Continue(crate::clone::Clone::clone(__self_0)),
ControlFlow::Break(__self_0) =>
ControlFlow::Break(crate::clone::Clone::clone(__self_0)),
}
}
}Clone, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "control_flow_enum_type", since = "1.55.0")]
impl<B: [const] crate::cmp::PartialEq, C: [const] crate::cmp::PartialEq>
const crate::cmp::PartialEq for ControlFlow<B, C> {
#[inline]
fn eq(&self, other: &ControlFlow<B, C>) -> bool {
let __self_discr = crate::intrinsics::discriminant_value(self);
let __arg1_discr = crate::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(ControlFlow::Continue(__self_0),
ControlFlow::Continue(__arg1_0)) => __self_0 == __arg1_0,
(ControlFlow::Break(__self_0), ControlFlow::Break(__arg1_0))
=> __self_0 == __arg1_0,
_ => unsafe { crate::intrinsics::unreachable() }
}
}
}PartialEq, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "control_flow_enum_type", since = "1.55.0")]
impl<B: [const] crate::cmp::Eq, C: [const] crate::cmp::Eq>
const crate::cmp::Eq for ControlFlow<B, C> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: crate::cmp::AssertParamIsEq<C>;
let _: crate::cmp::AssertParamIsEq<B>;
}
}Eq)]
89pub enum ControlFlow<B, C = ()> {
90 #[stable(feature = "control_flow_enum_type", since = "1.55.0")]
92 #[lang = "Continue"]
93 Continue(C),
94 #[stable(feature = "control_flow_enum_type", since = "1.55.0")]
96 #[lang = "Break"]
97 Break(B),
98 }
102
103#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
104#[rustc_const_unstable(feature = "const_try", issue = "74935")]
105impl<B, C> const ops::Try for ControlFlow<B, C> {
106 type Output = C;
107 type Residual = ControlFlow<B, convert::Infallible>;
108
109 #[inline]
110 fn from_output(output: Self::Output) -> Self {
111 ControlFlow::Continue(output)
112 }
113
114 #[inline]
115 fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
116 match self {
117 ControlFlow::Continue(c) => ControlFlow::Continue(c),
118 ControlFlow::Break(b) => ControlFlow::Break(ControlFlow::Break(b)),
119 }
120 }
121}
122
123#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
124#[rustc_const_unstable(feature = "const_try", issue = "74935")]
125impl<B, C> const ops::FromResidual<ControlFlow<B, convert::Infallible>> for ControlFlow<B, C> {
128 #[inline]
129 fn from_residual(residual: ControlFlow<B, convert::Infallible>) -> Self {
130 match residual {
131 ControlFlow::Break(b) => ControlFlow::Break(b),
132 }
133 }
134}
135
136#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
137#[rustc_const_unstable(feature = "const_try_residual", issue = "91285")]
138impl<B, C> const ops::Residual<C> for ControlFlow<B, convert::Infallible> {
139 type TryType = ControlFlow<B, C>;
140}
141
142impl<B, C> ControlFlow<B, C> {
143 #[inline]
154 #[stable(feature = "control_flow_enum_is", since = "1.59.0")]
155 #[rustc_const_stable(feature = "min_const_control_flow", since = "1.95.0")]
156 pub const fn is_break(&self) -> bool {
157 #[allow(non_exhaustive_omitted_patterns)] match *self {
ControlFlow::Break(_) => true,
_ => false,
}matches!(*self, ControlFlow::Break(_))
158 }
159
160 #[inline]
171 #[stable(feature = "control_flow_enum_is", since = "1.59.0")]
172 #[rustc_const_stable(feature = "min_const_control_flow", since = "1.95.0")]
173 pub const fn is_continue(&self) -> bool {
174 #[allow(non_exhaustive_omitted_patterns)] match *self {
ControlFlow::Continue(_) => true,
_ => false,
}matches!(*self, ControlFlow::Continue(_))
175 }
176
177 #[inline]
189 #[stable(feature = "control_flow_enum", since = "1.83.0")]
190 #[rustc_const_unstable(feature = "const_control_flow", issue = "148739")]
191 pub const fn break_value(self) -> Option<B>
192 where
193 Self: [const] Destruct,
194 {
195 match self {
196 ControlFlow::Continue(..) => None,
197 ControlFlow::Break(x) => Some(x),
198 }
199 }
200
201 #[inline]
265 #[stable(feature = "control_flow_ok", since = "CURRENT_RUSTC_VERSION")]
266 #[rustc_const_stable(feature = "control_flow_ok", since = "CURRENT_RUSTC_VERSION")]
267 #[rustc_allow_const_fn_unstable(const_precise_live_drops)]
268 pub const fn break_ok(self) -> Result<B, C> {
269 match self {
270 ControlFlow::Continue(c) => Err(c),
271 ControlFlow::Break(b) => Ok(b),
272 }
273 }
274
275 #[inline]
278 #[stable(feature = "control_flow_enum", since = "1.83.0")]
279 #[rustc_const_unstable(feature = "const_control_flow", issue = "148739")]
280 pub const fn map_break<T, F>(self, f: F) -> ControlFlow<T, C>
281 where
282 F: [const] FnOnce(B) -> T + [const] Destruct,
283 {
284 match self {
285 ControlFlow::Continue(x) => ControlFlow::Continue(x),
286 ControlFlow::Break(x) => ControlFlow::Break(f(x)),
287 }
288 }
289
290 #[inline]
302 #[stable(feature = "control_flow_enum", since = "1.83.0")]
303 #[rustc_const_unstable(feature = "const_control_flow", issue = "148739")]
304 pub const fn continue_value(self) -> Option<C>
305 where
306 Self: [const] Destruct,
307 {
308 match self {
309 ControlFlow::Continue(x) => Some(x),
310 ControlFlow::Break(..) => None,
311 }
312 }
313
314 #[inline]
377 #[stable(feature = "control_flow_ok", since = "CURRENT_RUSTC_VERSION")]
378 #[rustc_const_stable(feature = "control_flow_ok", since = "CURRENT_RUSTC_VERSION")]
379 #[rustc_allow_const_fn_unstable(const_precise_live_drops)]
380 pub const fn continue_ok(self) -> Result<C, B> {
381 match self {
382 ControlFlow::Continue(c) => Ok(c),
383 ControlFlow::Break(b) => Err(b),
384 }
385 }
386
387 #[inline]
390 #[stable(feature = "control_flow_enum", since = "1.83.0")]
391 #[rustc_const_unstable(feature = "const_control_flow", issue = "148739")]
392 pub const fn map_continue<T, F>(self, f: F) -> ControlFlow<B, T>
393 where
394 F: [const] FnOnce(C) -> T + [const] Destruct,
395 {
396 match self {
397 ControlFlow::Continue(x) => ControlFlow::Continue(f(x)),
398 ControlFlow::Break(x) => ControlFlow::Break(x),
399 }
400 }
401}
402
403impl<T> ControlFlow<T, T> {
404 #[unstable(feature = "control_flow_into_value", issue = "137461")]
416 #[rustc_allow_const_fn_unstable(const_precise_live_drops)]
417 pub const fn into_value(self) -> T {
418 match self {
419 ControlFlow::Continue(x) | ControlFlow::Break(x) => x,
420 }
421 }
422}
423
424impl<R: ops::Try> ControlFlow<R, R::Output> {
428 #[inline]
430 pub(crate) fn from_try(r: R) -> Self {
431 match R::branch(r) {
432 ControlFlow::Continue(v) => ControlFlow::Continue(v),
433 ControlFlow::Break(v) => ControlFlow::Break(R::from_residual(v)),
434 }
435 }
436
437 #[inline]
439 pub(crate) fn into_try(self) -> R {
440 match self {
441 ControlFlow::Continue(v) => R::from_output(v),
442 ControlFlow::Break(v) => v,
443 }
444 }
445}