diff options
| author | bors <bors@rust-lang.org> | 2019-07-17 08:29:02 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-07-17 08:29:02 +0000 |
| commit | d56128d2919132aceaf74cc3c68a4554f5445fce (patch) | |
| tree | db1797e894005e2c7c911c0c00151de2b9678a14 | |
| parent | bf16480f9cf124630f4a4ffc6d8a57b72fbd5ce9 (diff) | |
| parent | 74c8d984dbd73c070f9c9a5ac991cc271ff05164 (diff) | |
| download | rust-d56128d2919132aceaf74cc3c68a4554f5445fce.tar.gz rust-d56128d2919132aceaf74cc3c68a4554f5445fce.zip | |
Auto merge of #62596 - cuviper:expect_none, r=rkruppe
Add Option::expect_none(msg) and unwrap_none() These are `Option` analogues to `Result::expect_err` and `unwrap_err`.
| -rw-r--r-- | src/libcore/option.rs | 95 | ||||
| -rw-r--r-- | src/libcore/result.rs | 10 |
2 files changed, 99 insertions, 6 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 0b3ef41891b..33d6afdc975 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -136,7 +136,7 @@ #![stable(feature = "rust1", since = "1.0.0")] use crate::iter::{FromIterator, FusedIterator, TrustedLen}; -use crate::{convert, hint, mem, ops::{self, Deref}}; +use crate::{convert, fmt, hint, mem, ops::{self, Deref}}; use crate::pin::Pin; // Note that this is not a lang item per se, but it has a hidden dependency on @@ -977,6 +977,92 @@ impl<T: Clone> Option<&mut T> { } } +impl<T: fmt::Debug> Option<T> { + /// Unwraps an option, expecting [`None`] and returning nothing. + /// + /// # Panics + /// + /// Panics if the value is a [`Some`], with a panic message including the + /// passed message, and the content of the [`Some`]. + /// + /// [`Some`]: #variant.Some + /// [`None`]: #variant.None + /// + /// # Examples + /// + /// ``` + /// #![feature(option_expect_none)] + /// + /// use std::collections::HashMap; + /// let mut squares = HashMap::new(); + /// for i in -10..=10 { + /// // This will not panic, since all keys are unique. + /// squares.insert(i, i * i).expect_none("duplicate key"); + /// } + /// ``` + /// + /// ```{.should_panic} + /// #![feature(option_expect_none)] + /// + /// use std::collections::HashMap; + /// let mut sqrts = HashMap::new(); + /// for i in -10..=10 { + /// // This will panic, since both negative and positive `i` will + /// // insert the same `i * i` key, returning the old `Some(i)`. + /// sqrts.insert(i * i, i).expect_none("duplicate key"); + /// } + /// ``` + #[inline] + #[unstable(feature = "option_expect_none", reason = "newly added", issue = "62633")] + pub fn expect_none(self, msg: &str) { + if let Some(val) = self { + expect_none_failed(msg, &val); + } + } + + /// Unwraps an option, expecting [`None`] and returning nothing. + /// + /// # Panics + /// + /// Panics if the value is a [`Some`], with a custom panic message provided + /// by the [`Some`]'s value. + /// + /// [`Some(v)`]: #variant.Some + /// [`None`]: #variant.None + /// + /// # Examples + /// + /// ``` + /// #![feature(option_unwrap_none)] + /// + /// use std::collections::HashMap; + /// let mut squares = HashMap::new(); + /// for i in -10..=10 { + /// // This will not panic, since all keys are unique. + /// squares.insert(i, i * i).unwrap_none(); + /// } + /// ``` + /// + /// ```{.should_panic} + /// #![feature(option_unwrap_none)] + /// + /// use std::collections::HashMap; + /// let mut sqrts = HashMap::new(); + /// for i in -10..=10 { + /// // This will panic, since both negative and positive `i` will + /// // insert the same `i * i` key, returning the old `Some(i)`. + /// sqrts.insert(i * i, i).unwrap_none(); + /// } + /// ``` + #[inline] + #[unstable(feature = "option_unwrap_none", reason = "newly added", issue = "62633")] + pub fn unwrap_none(self) { + if let Some(val) = self { + expect_none_failed("called `Option::unwrap_none()` on a `Some` value", &val); + } + } +} + impl<T: Default> Option<T> { /// Returns the contained value or a default /// @@ -1069,6 +1155,13 @@ fn expect_failed(msg: &str) -> ! { panic!("{}", msg) } +// This is a separate function to reduce the code size of .expect_none() itself. +#[inline(never)] +#[cold] +fn expect_none_failed(msg: &str, value: &dyn fmt::Debug) -> ! { + panic!("{}: {:?}", msg, value) +} + ///////////////////////////////////////////////////////////////////////////// // Trait implementations ///////////////////////////////////////////////////////////////////////////// diff --git a/src/libcore/result.rs b/src/libcore/result.rs index ba72e1e75f8..3a38b66ad01 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -849,7 +849,7 @@ impl<T, E: fmt::Debug> Result<T, E> { pub fn unwrap(self) -> T { match self { Ok(t) => t, - Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", e), + Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", &e), } } @@ -876,7 +876,7 @@ impl<T, E: fmt::Debug> Result<T, E> { pub fn expect(self, msg: &str) -> T { match self { Ok(t) => t, - Err(e) => unwrap_failed(msg, e), + Err(e) => unwrap_failed(msg, &e), } } } @@ -908,7 +908,7 @@ impl<T: fmt::Debug, E> Result<T, E> { #[stable(feature = "rust1", since = "1.0.0")] pub fn unwrap_err(self) -> E { match self { - Ok(t) => unwrap_failed("called `Result::unwrap_err()` on an `Ok` value", t), + Ok(t) => unwrap_failed("called `Result::unwrap_err()` on an `Ok` value", &t), Err(e) => e, } } @@ -935,7 +935,7 @@ impl<T: fmt::Debug, E> Result<T, E> { #[stable(feature = "result_expect_err", since = "1.17.0")] pub fn expect_err(self, msg: &str) -> E { match self { - Ok(t) => unwrap_failed(msg, t), + Ok(t) => unwrap_failed(msg, &t), Err(e) => e, } } @@ -1047,7 +1047,7 @@ impl<T, E> Result<Option<T>, E> { // This is a separate function to reduce the code size of the methods #[inline(never)] #[cold] -fn unwrap_failed<E: fmt::Debug>(msg: &str, error: E) -> ! { +fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! { panic!("{}: {:?}", msg, error) } |
