Skip to content
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

[�CP] Option: add by-{ref,mut} variants to the most pervasive adapters #98536

Closed
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
92 changes: 92 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
@@ -739,6 +739,26 @@ impl<T> Option<T> {
}
}

/// Like `.expect()` but with a `&self` receiver.
///
/// Convenience shorthand for `.as_ref().expect()`.
#[inline]
#[doc(alias = "ref_expect")]
#[unstable(feature = "option_borrowing_adapters", issue = "none")]
pub fn expect_ref(&self, msg: &str) -> &T {
self.as_ref().expect(msg)
}

/// Like `.expect()` but with a `&mut self` receiver.
///
/// Convenience shorthand for `.as_mut().expect()`.
#[inline]
#[doc(alias = "mut_expect")]
#[unstable(feature = "option_borrowing_adapters", issue = "none")]
pub fn expect_mut(&mut self, msg: &str) -> &mut T {
self.as_mut().expect(msg)
}

/// Returns the contained [`Some`] value, consuming the `self` value.
///
/// Because this function may panic, its use is generally discouraged.
@@ -776,6 +796,26 @@ impl<T> Option<T> {
}
}

/// Like `.unwrap()` but with a `&self` receiver.
///
/// Convenience shorthand for `.as_ref().unwrap()`.
#[inline]
#[doc(alias = "ref_unwrap")]
#[unstable(feature = "option_borrowing_adapters", issue = "none")]
pub fn unwrap_ref(&self) -> &T {
self.as_ref().unwrap()
}

/// Like `.unwrap()` but with a `&mut self` receiver.
///
/// Convenience shorthand for `.as_mut().unwrap()`.
#[inline]
#[doc(alias = "mut_unwrap")]
#[unstable(feature = "option_borrowing_adapters", issue = "none")]
pub fn unwrap_mut(&mut self) -> &mut T {
self.as_mut().unwrap()
}

/// Returns the contained [`Some`] value or a provided default.
///
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
@@ -931,6 +971,32 @@ impl<T> Option<T> {
}
}

/// Like `.map()` but with a `&self` receiver.
///
/// Convenience shorthand for `.as_ref().map()`.
#[inline]
#[doc(alias = "ref_map")]
#[unstable(feature = "option_borrowing_adapters", issue = "none")]
pub fn map_ref<'r, U, F>(&'r self, f: F) -> Option<U>
where
F: FnOnce(&'r T) -> U,
{
self.as_ref().map(f)
}

/// Like `.map()` but with a `&mut self` receiver.
///
/// Convenience shorthand for `.as_mut().map()`.
#[inline]
#[doc(alias = "mut_map")]
#[unstable(feature = "option_borrowing_adapters", issue = "none")]
pub fn map_mut<'r, U, F>(&'r mut self, f: F) -> Option<U>
where
F: FnOnce(&'r mut T) -> U,
{
self.as_mut().map(f)
}

/// Calls the provided closure with a reference to the contained value (if [`Some`]).
///
/// # Examples
@@ -1264,6 +1330,32 @@ impl<T> Option<T> {
}
}

/// Like `.and_then()`, but with a `&self` receiver.
///
/// Convenience shorthand for `.as_ref().and_then()`.
#[inline]
#[doc(alias = "ref_and_then")]
#[unstable(feature = "option_borrowing_adapters", issue = "none")]
pub fn and_then_ref<'r, U, F>(&'r self, f: F) -> Option<U>
where
F: FnOnce(&'r T) -> Option<U>,
{
self.as_ref().and_then(f)
}

/// Like `.and_then()`, but with a `&mut self` receiver.
///
/// Convenience shorthand for `.as_mut().and_then()`.
#[inline]
#[doc(alias = "mut_and_then")]
#[unstable(feature = "option_borrowing_adapters", issue = "none")]
pub fn and_then_mut<'r, U, F>(&'r mut self, f: F) -> Option<U>
where
F: FnOnce(&'r mut T) -> Option<U>,
{
self.as_mut().and_then(f)
}

/// Returns [`None`] if the option is [`None`], otherwise calls `predicate`
/// with the wrapped value and returns:
///