From f49b6c6cd58aa61c804dcb22cc013c728f8924af Mon Sep 17 00:00:00 2001 From: Madhav Madhusoodanan Date: Mon, 24 Feb 2025 00:36:25 +0530 Subject: [PATCH] Added into_value const function to ControlFlow Fixed issue with usage of generics and moved feature gate to crate root Removed const tag Fixed alphabetical ordering of feature gate, added same to doctest Removed crate-level declaration of feature gate control_flow_into_value Used const_precise_live_drops to constify into_value without issue of a drop --- library/core/src/ops/control_flow.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/library/core/src/ops/control_flow.rs b/library/core/src/ops/control_flow.rs index c8fcee5c140f5..8993e14fcd379 100644 --- a/library/core/src/ops/control_flow.rs +++ b/library/core/src/ops/control_flow.rs @@ -229,6 +229,27 @@ impl ControlFlow { } } +impl ControlFlow { + /// Extracts the value `T` that is wrapped by `ControlFlow`. + /// + /// # Examples + /// + /// ``` + /// #![feature(control_flow_into_value)] + /// use std::ops::ControlFlow; + /// + /// assert_eq!(ControlFlow::::Break(1024).into_value(), 1024); + /// assert_eq!(ControlFlow::::Continue(512).into_value(), 512); + /// ``` + #[unstable(feature = "control_flow_into_value", issue = "137461")] + #[rustc_allow_const_fn_unstable(const_precise_live_drops)] + pub const fn into_value(self) -> T { + match self { + ControlFlow::Continue(x) | ControlFlow::Break(x) => x, + } + } +} + /// These are used only as part of implementing the iterator adapters. /// They have mediocre names and non-obvious semantics, so aren't /// currently on a path to potential stabilization.