Skip to content

Commit a706598

Browse files
committed
Constify ControlFlow methods
1 parent c6d42d7 commit a706598

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

library/core/src/ops/control_flow.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::marker::Destruct;
12
use crate::{convert, ops};
23

34
/// Used to tell an operation whether it should exit early or go on as usual.
@@ -150,7 +151,8 @@ impl<B, C> ControlFlow<B, C> {
150151
/// ```
151152
#[inline]
152153
#[stable(feature = "control_flow_enum_is", since = "1.59.0")]
153-
pub fn is_break(&self) -> bool {
154+
#[rustc_const_unstable(feature = "const_control_flow", issue = "none")]
155+
pub const fn is_break(&self) -> bool {
154156
matches!(*self, ControlFlow::Break(_))
155157
}
156158

@@ -166,7 +168,8 @@ impl<B, C> ControlFlow<B, C> {
166168
/// ```
167169
#[inline]
168170
#[stable(feature = "control_flow_enum_is", since = "1.59.0")]
169-
pub fn is_continue(&self) -> bool {
171+
#[rustc_const_unstable(feature = "const_control_flow", issue = "none")]
172+
pub const fn is_continue(&self) -> bool {
170173
matches!(*self, ControlFlow::Continue(_))
171174
}
172175

@@ -183,7 +186,11 @@ impl<B, C> ControlFlow<B, C> {
183186
/// ```
184187
#[inline]
185188
#[stable(feature = "control_flow_enum", since = "1.83.0")]
186-
pub fn break_value(self) -> Option<B> {
189+
#[rustc_const_unstable(feature = "const_control_flow", issue = "none")]
190+
pub const fn break_value(self) -> Option<B>
191+
where
192+
Self: [const] Destruct,
193+
{
187194
match self {
188195
ControlFlow::Continue(..) => None,
189196
ControlFlow::Break(x) => Some(x),
@@ -257,7 +264,8 @@ impl<B, C> ControlFlow<B, C> {
257264
/// ```
258265
#[inline]
259266
#[unstable(feature = "control_flow_ok", issue = "140266")]
260-
pub fn break_ok(self) -> Result<B, C> {
267+
#[rustc_const_unstable(feature = "const_control_flow", issue = "none")]
268+
pub const fn break_ok(self) -> Result<B, C> {
261269
match self {
262270
ControlFlow::Continue(c) => Err(c),
263271
ControlFlow::Break(b) => Ok(b),
@@ -268,7 +276,11 @@ impl<B, C> ControlFlow<B, C> {
268276
/// to the break value in case it exists.
269277
#[inline]
270278
#[stable(feature = "control_flow_enum", since = "1.83.0")]
271-
pub fn map_break<T>(self, f: impl FnOnce(B) -> T) -> ControlFlow<T, C> {
279+
#[rustc_const_unstable(feature = "const_control_flow", issue = "none")]
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+
{
272284
match self {
273285
ControlFlow::Continue(x) => ControlFlow::Continue(x),
274286
ControlFlow::Break(x) => ControlFlow::Break(f(x)),
@@ -288,7 +300,11 @@ impl<B, C> ControlFlow<B, C> {
288300
/// ```
289301
#[inline]
290302
#[stable(feature = "control_flow_enum", since = "1.83.0")]
291-
pub fn continue_value(self) -> Option<C> {
303+
#[rustc_const_unstable(feature = "const_control_flow", issue = "none")]
304+
pub const fn continue_value(self) -> Option<C>
305+
where
306+
Self: [const] Destruct,
307+
{
292308
match self {
293309
ControlFlow::Continue(x) => Some(x),
294310
ControlFlow::Break(..) => None,
@@ -361,7 +377,8 @@ impl<B, C> ControlFlow<B, C> {
361377
/// ```
362378
#[inline]
363379
#[unstable(feature = "control_flow_ok", issue = "140266")]
364-
pub fn continue_ok(self) -> Result<C, B> {
380+
#[rustc_const_unstable(feature = "const_control_flow", issue = "none")]
381+
pub const fn continue_ok(self) -> Result<C, B> {
365382
match self {
366383
ControlFlow::Continue(c) => Ok(c),
367384
ControlFlow::Break(b) => Err(b),
@@ -372,7 +389,11 @@ impl<B, C> ControlFlow<B, C> {
372389
/// to the continue value in case it exists.
373390
#[inline]
374391
#[stable(feature = "control_flow_enum", since = "1.83.0")]
375-
pub fn map_continue<T>(self, f: impl FnOnce(C) -> T) -> ControlFlow<B, T> {
392+
#[rustc_const_unstable(feature = "const_control_flow", issue = "none")]
393+
pub const fn map_continue<T, F>(self, f: F) -> ControlFlow<B, T>
394+
where
395+
F: [const] FnOnce(C) -> T + [const] Destruct,
396+
{
376397
match self {
377398
ControlFlow::Continue(x) => ControlFlow::Continue(f(x)),
378399
ControlFlow::Break(x) => ControlFlow::Break(x),

0 commit comments

Comments
 (0)