From 19645ac05a3e5cd4e508e9819f4939fc2f959b5b Mon Sep 17 00:00:00 2001 From: ltdk Date: Tue, 28 Dec 2021 20:59:50 -0500 Subject: [PATCH] Add Result::{ok, err, and, or, unwrap_or} as const --- library/core/src/result.rs | 55 +++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index fbd6d419236..65812b2e0c3 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -632,10 +632,16 @@ pub fn is_err_with(&self, f: impl FnOnce(&E) -> bool) -> bool { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub fn ok(self) -> Option { + #[rustc_const_unstable(feature = "const_result_drop", issue = "92384")] + pub const fn ok(self) -> Option + where + E: ~const Drop, + { match self { Ok(x) => Some(x), - Err(_) => None, + // FIXME: ~const Drop doesn't quite work right yet + #[allow(unused_variables)] + Err(x) => None, } } @@ -657,9 +663,15 @@ pub fn ok(self) -> Option { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub fn err(self) -> Option { + #[rustc_const_unstable(feature = "const_result_drop", issue = "92384")] + pub const fn err(self) -> Option + where + T: ~const Drop, + { match self { - Ok(_) => None, + // FIXME: ~const Drop doesn't quite work right yet + #[allow(unused_variables)] + Ok(x) => None, Err(x) => Some(x), } } @@ -1266,10 +1278,18 @@ pub fn into_err(self) -> E /// assert_eq!(x.and(y), Ok("different result type")); /// ``` #[inline] + #[rustc_const_unstable(feature = "const_result_drop", issue = "92384")] #[stable(feature = "rust1", since = "1.0.0")] - pub fn and(self, res: Result) -> Result { + pub const fn and(self, res: Result) -> Result + where + T: ~const Drop, + U: ~const Drop, + E: ~const Drop, + { match self { - Ok(_) => res, + // FIXME: ~const Drop doesn't quite work right yet + #[allow(unused_variables)] + Ok(x) => res, Err(e) => Err(e), } } @@ -1331,11 +1351,19 @@ pub fn and_then Result>(self, op: F) -> Result { /// assert_eq!(x.or(y), Ok(2)); /// ``` #[inline] + #[rustc_const_unstable(feature = "const_result_drop", issue = "92384")] #[stable(feature = "rust1", since = "1.0.0")] - pub fn or(self, res: Result) -> Result { + pub const fn or(self, res: Result) -> Result + where + T: ~const Drop, + E: ~const Drop, + F: ~const Drop, + { match self { Ok(v) => Ok(v), - Err(_) => res, + // FIXME: ~const Drop doesn't quite work right yet + #[allow(unused_variables)] + Err(e) => res, } } @@ -1387,11 +1415,18 @@ pub fn or_else Result>(self, op: O) -> Result { /// assert_eq!(x.unwrap_or(default), default); /// ``` #[inline] + #[rustc_const_unstable(feature = "const_result_drop", issue = "92384")] #[stable(feature = "rust1", since = "1.0.0")] - pub fn unwrap_or(self, default: T) -> T { + pub const fn unwrap_or(self, default: T) -> T + where + T: ~const Drop, + E: ~const Drop, + { match self { Ok(t) => t, - Err(_) => default, + // FIXME: ~const Drop doesn't quite work right yet + #[allow(unused_variables)] + Err(e) => default, } }