Skip to content

Constify some more Result functions #143771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 112 additions & 25 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@
#![stable(feature = "rust1", since = "1.0.0")]

use crate::iter::{self, FusedIterator, TrustedLen};
use crate::marker::Destruct;
use crate::ops::{self, ControlFlow, Deref, DerefMut};
use crate::{convert, fmt, hint};

Expand Down Expand Up @@ -606,7 +607,13 @@ impl<T, E> Result<T, E> {
#[must_use]
#[inline]
#[stable(feature = "is_some_and", since = "1.70.0")]
pub fn is_ok_and(self, f: impl FnOnce(T) -> bool) -> bool {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn is_ok_and<F>(self, f: F) -> bool
where
F: ~const FnOnce(T) -> bool + ~const Destruct,
T: ~const Destruct,
E: ~const Destruct,
{
match self {
Err(_) => false,
Ok(x) => f(x),
Expand Down Expand Up @@ -655,7 +662,13 @@ impl<T, E> Result<T, E> {
#[must_use]
#[inline]
#[stable(feature = "is_some_and", since = "1.70.0")]
pub fn is_err_and(self, f: impl FnOnce(E) -> bool) -> bool {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn is_err_and<F>(self, f: F) -> bool
where
F: ~const FnOnce(E) -> bool + ~const Destruct,
E: ~const Destruct,
T: ~const Destruct,
{
match self {
Ok(_) => false,
Err(e) => f(e),
Expand All @@ -682,8 +695,13 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
#[rustc_diagnostic_item = "result_ok_method"]
pub fn ok(self) -> Option<T> {
pub const fn ok(self) -> Option<T>
where
T: ~const Destruct,
E: ~const Destruct,
{
match self {
Ok(x) => Some(x),
Err(_) => None,
Expand All @@ -706,7 +724,12 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn err(self) -> Option<E> {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn err(self) -> Option<E>
where
T: ~const Destruct,
E: ~const Destruct,
{
match self {
Ok(_) => None,
Err(x) => Some(x),
Expand Down Expand Up @@ -796,7 +819,11 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn map<U, F: FnOnce(T) -> U>(self, op: F) -> Result<U, E> {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn map<U, F>(self, op: F) -> Result<U, E>
where
F: ~const FnOnce(T) -> U + ~const Destruct,
{
match self {
Ok(t) => Ok(op(t)),
Err(e) => Err(e),
Expand All @@ -823,8 +850,15 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "result_map_or", since = "1.41.0")]
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
#[must_use = "if you don't need the returned value, use `if let` instead"]
pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U {
pub const fn map_or<U, F>(self, default: U, f: F) -> U
where
F: ~const FnOnce(T) -> U + ~const Destruct,
T: ~const Destruct,
E: ~const Destruct,
U: ~const Destruct,
{
match self {
Ok(t) => f(t),
Err(_) => default,
Expand All @@ -851,7 +885,12 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "result_map_or_else", since = "1.41.0")]
pub fn map_or_else<U, D: FnOnce(E) -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn map_or_else<U, D, F>(self, default: D, f: F) -> U
where
D: ~const FnOnce(E) -> U + ~const Destruct,
F: ~const FnOnce(T) -> U + ~const Destruct,
{
match self {
Ok(t) => f(t),
Err(e) => default(e),
Expand All @@ -877,10 +916,13 @@ impl<T, E> Result<T, E> {
/// [default value]: Default::default
#[inline]
#[unstable(feature = "result_option_map_or_default", issue = "138099")]
pub fn map_or_default<U, F>(self, f: F) -> U
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn map_or_default<U, F>(self, f: F) -> U
where
U: Default,
F: FnOnce(T) -> U,
F: ~const FnOnce(T) -> U + ~const Destruct,
U: ~const Default,
T: ~const Destruct,
E: ~const Destruct,
{
match self {
Ok(t) => f(t),
Expand Down Expand Up @@ -908,7 +950,11 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn map_err<F, O: FnOnce(E) -> F>(self, op: O) -> Result<T, F> {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn map_err<F, O>(self, op: O) -> Result<T, F>
where
O: ~const FnOnce(E) -> F + ~const Destruct,
{
match self {
Ok(t) => Ok(t),
Err(e) => Err(op(e)),
Expand All @@ -930,7 +976,11 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "result_option_inspect", since = "1.76.0")]
pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn inspect<F>(self, f: F) -> Self
where
F: ~const FnOnce(&T) + ~const Destruct,
{
if let Ok(ref t) = self {
f(t);
}
Expand All @@ -954,7 +1004,11 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "result_option_inspect", since = "1.76.0")]
pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn inspect_err<F>(self, f: F) -> Self
where
F: ~const FnOnce(&E) + ~const Destruct,
{
if let Err(ref e) = self {
f(e);
}
Expand Down Expand Up @@ -1033,7 +1087,8 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn iter(&self) -> Iter<'_, T> {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn iter(&self) -> Iter<'_, T> {
Iter { inner: self.as_ref().ok() }
}

Expand All @@ -1056,7 +1111,8 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn iter_mut(&mut self) -> IterMut<'_, T> {
IterMut { inner: self.as_mut().ok() }
}

Expand Down Expand Up @@ -1195,9 +1251,11 @@ impl<T, E> Result<T, E> {
/// [`FromStr`]: crate::str::FromStr
#[inline]
#[stable(feature = "result_unwrap_or_default", since = "1.16.0")]
pub fn unwrap_or_default(self) -> T
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn unwrap_or_default(self) -> T
where
T: Default,
T: ~const Default + ~const Destruct,
E: ~const Destruct,
{
match self {
Ok(x) => x,
Expand Down Expand Up @@ -1370,7 +1428,13 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn and<U>(self, res: Result<U, E>) -> Result<U, E> {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn and<U>(self, res: Result<U, E>) -> Result<U, E>
where
T: ~const Destruct,
E: ~const Destruct,
U: ~const Destruct,
{
match self {
Ok(_) => res,
Err(e) => Err(e),
Expand Down Expand Up @@ -1409,8 +1473,12 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
#[rustc_confusables("flat_map", "flatmap")]
pub fn and_then<U, F: FnOnce(T) -> Result<U, E>>(self, op: F) -> Result<U, E> {
pub const fn and_then<U, F>(self, op: F) -> Result<U, E>
where
F: ~const FnOnce(T) -> Result<U, E> + ~const Destruct,
{
match self {
Ok(t) => op(t),
Err(e) => Err(e),
Expand Down Expand Up @@ -1446,7 +1514,13 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn or<F>(self, res: Result<T, F>) -> Result<T, F> {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn or<F>(self, res: Result<T, F>) -> Result<T, F>
where
T: ~const Destruct,
E: ~const Destruct,
F: ~const Destruct,
{
match self {
Ok(v) => Ok(v),
Err(_) => res,
Expand All @@ -1471,7 +1545,11 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn or_else<F, O: FnOnce(E) -> Result<T, F>>(self, op: O) -> Result<T, F> {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn or_else<F, O>(self, op: O) -> Result<T, F>
where
O: ~const FnOnce(E) -> Result<T, F> + ~const Destruct,
{
match self {
Ok(t) => Ok(t),
Err(e) => op(e),
Expand All @@ -1498,7 +1576,12 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn unwrap_or(self, default: T) -> T {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn unwrap_or(self, default: T) -> T
where
T: ~const Destruct,
E: ~const Destruct,
{
match self {
Ok(t) => t,
Err(_) => default,
Expand All @@ -1519,7 +1602,11 @@ impl<T, E> Result<T, E> {
#[inline]
#[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn unwrap_or_else<F: FnOnce(E) -> T>(self, op: F) -> T {
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
pub const fn unwrap_or_else<F>(self, op: F) -> T
where
F: ~const FnOnce(E) -> T + ~const Destruct,
{
match self {
Ok(t) => t,
Err(e) => op(e),
Expand Down Expand Up @@ -1762,7 +1849,7 @@ impl<T, E> Result<Result<T, E>, E> {
#[cold]
#[track_caller]
fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! {
panic!("{msg}: {error:?}")
panic!("{msg}: {error:?}");
}

// This is a separate function to avoid constructing a `dyn Debug`
Expand All @@ -1773,7 +1860,7 @@ fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! {
#[inline]
#[cold]
#[track_caller]
fn unwrap_failed<T>(_msg: &str, _error: &T) -> ! {
const fn unwrap_failed<T>(_msg: &str, _error: &T) -> ! {
panic!()
}

Expand Down
2 changes: 2 additions & 0 deletions library/coretests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#![feature(const_eval_select)]
#![feature(const_ops)]
#![feature(const_ref_cell)]
#![feature(const_result_trait_fn)]
#![feature(const_trait_impl)]
#![feature(core_float_math)]
#![feature(core_intrinsics)]
Expand Down Expand Up @@ -82,6 +83,7 @@
#![feature(pointer_is_aligned_to)]
#![feature(portable_simd)]
#![feature(ptr_metadata)]
#![feature(result_option_map_or_default)]
#![feature(slice_from_ptr_range)]
#![feature(slice_internals)]
#![feature(slice_partition_dedup)]
Expand Down
Loading
Loading