Skip to content
Closed
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
26 changes: 22 additions & 4 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@

#![stable(feature = "rust1", since = "1.0.0")]

use crate::fmt;
use crate::iter::{self, FusedIterator, TrustedLen};
use crate::ops::{self, ControlFlow, Deref, DerefMut};
use crate::panicking::{panic, panic_display};
Expand Down Expand Up @@ -955,7 +956,7 @@ impl<T> Option<T> {
pub const fn expect(self, msg: &str) -> T {
match self {
Some(val) => val,
None => expect_failed(msg),
None => expect_failed("expected: ", msg),
}
}

Expand Down Expand Up @@ -1783,7 +1784,11 @@ impl<T> Option<T> {
where
P: FnOnce(&mut T) -> bool,
{
if self.as_mut().map_or(false, predicate) { self.take() } else { None }
if self.as_mut().map_or(false, predicate) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. My automatic formatting seems to have different opinions. TODO: clean up

self.take()
} else {
None
}
}

/// Replaces the actual value in the option by the value given in parameter,
Expand Down Expand Up @@ -2045,8 +2050,21 @@ const fn unwrap_failed() -> ! {
#[cfg_attr(feature = "panic_immediate_abort", inline)]
#[cold]
#[track_caller]
const fn expect_failed(msg: &str) -> ! {
panic_display(&msg)
const fn expect_failed(prefix: &str, msg: &str) -> ! {
// TODO: FIXME: there seem to be no way currently to print anything other than a single `&str`
// in `panic` in `const` context.
struct MsgWithPrefix<'a> {
prefix: &'a str,
msg: &'a str,
}

impl<'a> fmt::Display for MsgWithPrefix<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.prefix)?;
f.write_str(self.msg)
}
}
panic_display(&MsgWithPrefix { prefix, msg })
}

/////////////////////////////////////////////////////////////////////////////
Expand Down
14 changes: 7 additions & 7 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ impl<T, E> Result<T, E> {
{
match self {
Ok(t) => t,
Err(e) => unwrap_failed(msg, &e),
Err(e) => unwrap_failed("expected: ", msg, &e),
}
}

Expand Down Expand Up @@ -1134,7 +1134,7 @@ impl<T, E> Result<T, E> {
{
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),
}
}

Expand Down Expand Up @@ -1197,7 +1197,7 @@ impl<T, E> Result<T, E> {
T: fmt::Debug,
{
match self {
Ok(t) => unwrap_failed(msg, &t),
Ok(t) => unwrap_failed("expected error: ", msg, &t),
Err(e) => e,
}
}
Expand Down Expand Up @@ -1228,7 +1228,7 @@ impl<T, E> Result<T, E> {
T: fmt::Debug,
{
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,
}
}
Expand Down Expand Up @@ -1728,8 +1728,8 @@ impl<T, E> Result<Result<T, E>, E> {
#[inline(never)]
#[cold]
#[track_caller]
fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! {
panic!("{msg}: {error:?}")
fn unwrap_failed(prefix: &str, msg: &str, error: &dyn fmt::Debug) -> ! {
panic!("{prefix}{msg}: {error:?}")
}

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

Expand Down
Loading