Skip to content

Commit 96b3b83

Browse files
Rollup merge of #143771 - Randl:const-result, r=tgross35
Constify some more `Result` functions
2 parents 4eddebd + 9377e0a commit 96b3b83

File tree

3 files changed

+197
-25
lines changed

3 files changed

+197
-25
lines changed

library/core/src/result.rs

Lines changed: 112 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@
534534
#![stable(feature = "rust1", since = "1.0.0")]
535535

536536
use crate::iter::{self, FusedIterator, TrustedLen};
537+
use crate::marker::Destruct;
537538
use crate::ops::{self, ControlFlow, Deref, DerefMut};
538539
use crate::{convert, fmt, hint};
539540

@@ -606,7 +607,13 @@ impl<T, E> Result<T, E> {
606607
#[must_use]
607608
#[inline]
608609
#[stable(feature = "is_some_and", since = "1.70.0")]
609-
pub fn is_ok_and(self, f: impl FnOnce(T) -> bool) -> bool {
610+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
611+
pub const fn is_ok_and<F>(self, f: F) -> bool
612+
where
613+
F: ~const FnOnce(T) -> bool + ~const Destruct,
614+
T: ~const Destruct,
615+
E: ~const Destruct,
616+
{
610617
match self {
611618
Err(_) => false,
612619
Ok(x) => f(x),
@@ -655,7 +662,13 @@ impl<T, E> Result<T, E> {
655662
#[must_use]
656663
#[inline]
657664
#[stable(feature = "is_some_and", since = "1.70.0")]
658-
pub fn is_err_and(self, f: impl FnOnce(E) -> bool) -> bool {
665+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
666+
pub const fn is_err_and<F>(self, f: F) -> bool
667+
where
668+
F: ~const FnOnce(E) -> bool + ~const Destruct,
669+
E: ~const Destruct,
670+
T: ~const Destruct,
671+
{
659672
match self {
660673
Ok(_) => false,
661674
Err(e) => f(e),
@@ -682,8 +695,13 @@ impl<T, E> Result<T, E> {
682695
/// ```
683696
#[inline]
684697
#[stable(feature = "rust1", since = "1.0.0")]
698+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
685699
#[rustc_diagnostic_item = "result_ok_method"]
686-
pub fn ok(self) -> Option<T> {
700+
pub const fn ok(self) -> Option<T>
701+
where
702+
T: ~const Destruct,
703+
E: ~const Destruct,
704+
{
687705
match self {
688706
Ok(x) => Some(x),
689707
Err(_) => None,
@@ -706,7 +724,12 @@ impl<T, E> Result<T, E> {
706724
/// ```
707725
#[inline]
708726
#[stable(feature = "rust1", since = "1.0.0")]
709-
pub fn err(self) -> Option<E> {
727+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
728+
pub const fn err(self) -> Option<E>
729+
where
730+
T: ~const Destruct,
731+
E: ~const Destruct,
732+
{
710733
match self {
711734
Ok(_) => None,
712735
Err(x) => Some(x),
@@ -796,7 +819,11 @@ impl<T, E> Result<T, E> {
796819
/// ```
797820
#[inline]
798821
#[stable(feature = "rust1", since = "1.0.0")]
799-
pub fn map<U, F: FnOnce(T) -> U>(self, op: F) -> Result<U, E> {
822+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
823+
pub const fn map<U, F>(self, op: F) -> Result<U, E>
824+
where
825+
F: ~const FnOnce(T) -> U + ~const Destruct,
826+
{
800827
match self {
801828
Ok(t) => Ok(op(t)),
802829
Err(e) => Err(e),
@@ -823,8 +850,15 @@ impl<T, E> Result<T, E> {
823850
/// ```
824851
#[inline]
825852
#[stable(feature = "result_map_or", since = "1.41.0")]
853+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
826854
#[must_use = "if you don't need the returned value, use `if let` instead"]
827-
pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U {
855+
pub const fn map_or<U, F>(self, default: U, f: F) -> U
856+
where
857+
F: ~const FnOnce(T) -> U + ~const Destruct,
858+
T: ~const Destruct,
859+
E: ~const Destruct,
860+
U: ~const Destruct,
861+
{
828862
match self {
829863
Ok(t) => f(t),
830864
Err(_) => default,
@@ -851,7 +885,12 @@ impl<T, E> Result<T, E> {
851885
/// ```
852886
#[inline]
853887
#[stable(feature = "result_map_or_else", since = "1.41.0")]
854-
pub fn map_or_else<U, D: FnOnce(E) -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U {
888+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
889+
pub const fn map_or_else<U, D, F>(self, default: D, f: F) -> U
890+
where
891+
D: ~const FnOnce(E) -> U + ~const Destruct,
892+
F: ~const FnOnce(T) -> U + ~const Destruct,
893+
{
855894
match self {
856895
Ok(t) => f(t),
857896
Err(e) => default(e),
@@ -877,10 +916,13 @@ impl<T, E> Result<T, E> {
877916
/// [default value]: Default::default
878917
#[inline]
879918
#[unstable(feature = "result_option_map_or_default", issue = "138099")]
880-
pub fn map_or_default<U, F>(self, f: F) -> U
919+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
920+
pub const fn map_or_default<U, F>(self, f: F) -> U
881921
where
882-
U: Default,
883-
F: FnOnce(T) -> U,
922+
F: ~const FnOnce(T) -> U + ~const Destruct,
923+
U: ~const Default,
924+
T: ~const Destruct,
925+
E: ~const Destruct,
884926
{
885927
match self {
886928
Ok(t) => f(t),
@@ -908,7 +950,11 @@ impl<T, E> Result<T, E> {
908950
/// ```
909951
#[inline]
910952
#[stable(feature = "rust1", since = "1.0.0")]
911-
pub fn map_err<F, O: FnOnce(E) -> F>(self, op: O) -> Result<T, F> {
953+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
954+
pub const fn map_err<F, O>(self, op: O) -> Result<T, F>
955+
where
956+
O: ~const FnOnce(E) -> F + ~const Destruct,
957+
{
912958
match self {
913959
Ok(t) => Ok(t),
914960
Err(e) => Err(op(e)),
@@ -930,7 +976,11 @@ impl<T, E> Result<T, E> {
930976
/// ```
931977
#[inline]
932978
#[stable(feature = "result_option_inspect", since = "1.76.0")]
933-
pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self {
979+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
980+
pub const fn inspect<F>(self, f: F) -> Self
981+
where
982+
F: ~const FnOnce(&T) + ~const Destruct,
983+
{
934984
if let Ok(ref t) = self {
935985
f(t);
936986
}
@@ -954,7 +1004,11 @@ impl<T, E> Result<T, E> {
9541004
/// ```
9551005
#[inline]
9561006
#[stable(feature = "result_option_inspect", since = "1.76.0")]
957-
pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self {
1007+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
1008+
pub const fn inspect_err<F>(self, f: F) -> Self
1009+
where
1010+
F: ~const FnOnce(&E) + ~const Destruct,
1011+
{
9581012
if let Err(ref e) = self {
9591013
f(e);
9601014
}
@@ -1033,7 +1087,8 @@ impl<T, E> Result<T, E> {
10331087
/// ```
10341088
#[inline]
10351089
#[stable(feature = "rust1", since = "1.0.0")]
1036-
pub fn iter(&self) -> Iter<'_, T> {
1090+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
1091+
pub const fn iter(&self) -> Iter<'_, T> {
10371092
Iter { inner: self.as_ref().ok() }
10381093
}
10391094

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

@@ -1195,9 +1251,11 @@ impl<T, E> Result<T, E> {
11951251
/// [`FromStr`]: crate::str::FromStr
11961252
#[inline]
11971253
#[stable(feature = "result_unwrap_or_default", since = "1.16.0")]
1198-
pub fn unwrap_or_default(self) -> T
1254+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
1255+
pub const fn unwrap_or_default(self) -> T
11991256
where
1200-
T: Default,
1257+
T: ~const Default + ~const Destruct,
1258+
E: ~const Destruct,
12011259
{
12021260
match self {
12031261
Ok(x) => x,
@@ -1370,7 +1428,13 @@ impl<T, E> Result<T, E> {
13701428
/// ```
13711429
#[inline]
13721430
#[stable(feature = "rust1", since = "1.0.0")]
1373-
pub fn and<U>(self, res: Result<U, E>) -> Result<U, E> {
1431+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
1432+
pub const fn and<U>(self, res: Result<U, E>) -> Result<U, E>
1433+
where
1434+
T: ~const Destruct,
1435+
E: ~const Destruct,
1436+
U: ~const Destruct,
1437+
{
13741438
match self {
13751439
Ok(_) => res,
13761440
Err(e) => Err(e),
@@ -1409,8 +1473,12 @@ impl<T, E> Result<T, E> {
14091473
/// ```
14101474
#[inline]
14111475
#[stable(feature = "rust1", since = "1.0.0")]
1476+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
14121477
#[rustc_confusables("flat_map", "flatmap")]
1413-
pub fn and_then<U, F: FnOnce(T) -> Result<U, E>>(self, op: F) -> Result<U, E> {
1478+
pub const fn and_then<U, F>(self, op: F) -> Result<U, E>
1479+
where
1480+
F: ~const FnOnce(T) -> Result<U, E> + ~const Destruct,
1481+
{
14141482
match self {
14151483
Ok(t) => op(t),
14161484
Err(e) => Err(e),
@@ -1446,7 +1514,13 @@ impl<T, E> Result<T, E> {
14461514
/// ```
14471515
#[inline]
14481516
#[stable(feature = "rust1", since = "1.0.0")]
1449-
pub fn or<F>(self, res: Result<T, F>) -> Result<T, F> {
1517+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
1518+
pub const fn or<F>(self, res: Result<T, F>) -> Result<T, F>
1519+
where
1520+
T: ~const Destruct,
1521+
E: ~const Destruct,
1522+
F: ~const Destruct,
1523+
{
14501524
match self {
14511525
Ok(v) => Ok(v),
14521526
Err(_) => res,
@@ -1471,7 +1545,11 @@ impl<T, E> Result<T, E> {
14711545
/// ```
14721546
#[inline]
14731547
#[stable(feature = "rust1", since = "1.0.0")]
1474-
pub fn or_else<F, O: FnOnce(E) -> Result<T, F>>(self, op: O) -> Result<T, F> {
1548+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
1549+
pub const fn or_else<F, O>(self, op: O) -> Result<T, F>
1550+
where
1551+
O: ~const FnOnce(E) -> Result<T, F> + ~const Destruct,
1552+
{
14751553
match self {
14761554
Ok(t) => Ok(t),
14771555
Err(e) => op(e),
@@ -1498,7 +1576,12 @@ impl<T, E> Result<T, E> {
14981576
/// ```
14991577
#[inline]
15001578
#[stable(feature = "rust1", since = "1.0.0")]
1501-
pub fn unwrap_or(self, default: T) -> T {
1579+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
1580+
pub const fn unwrap_or(self, default: T) -> T
1581+
where
1582+
T: ~const Destruct,
1583+
E: ~const Destruct,
1584+
{
15021585
match self {
15031586
Ok(t) => t,
15041587
Err(_) => default,
@@ -1519,7 +1602,11 @@ impl<T, E> Result<T, E> {
15191602
#[inline]
15201603
#[track_caller]
15211604
#[stable(feature = "rust1", since = "1.0.0")]
1522-
pub fn unwrap_or_else<F: FnOnce(E) -> T>(self, op: F) -> T {
1605+
#[rustc_const_unstable(feature = "const_result_trait_fn", issue = "144211")]
1606+
pub const fn unwrap_or_else<F>(self, op: F) -> T
1607+
where
1608+
F: ~const FnOnce(E) -> T + ~const Destruct,
1609+
{
15231610
match self {
15241611
Ok(t) => t,
15251612
Err(e) => op(e),
@@ -1762,7 +1849,7 @@ impl<T, E> Result<Result<T, E>, E> {
17621849
#[cold]
17631850
#[track_caller]
17641851
fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! {
1765-
panic!("{msg}: {error:?}")
1852+
panic!("{msg}: {error:?}");
17661853
}
17671854

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

library/coretests/tests/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#![feature(const_eval_select)]
2121
#![feature(const_ops)]
2222
#![feature(const_ref_cell)]
23+
#![feature(const_result_trait_fn)]
2324
#![feature(const_trait_impl)]
2425
#![feature(core_float_math)]
2526
#![feature(core_intrinsics)]
@@ -82,6 +83,7 @@
8283
#![feature(pointer_is_aligned_to)]
8384
#![feature(portable_simd)]
8485
#![feature(ptr_metadata)]
86+
#![feature(result_option_map_or_default)]
8587
#![feature(slice_from_ptr_range)]
8688
#![feature(slice_internals)]
8789
#![feature(slice_partition_dedup)]

0 commit comments

Comments
 (0)