Skip to content

Commit

Permalink
Auto merge of #116866 - slanterns:inspect-stabilize, r=BurntSushi
Browse files Browse the repository at this point in the history
Stabilize `result_option_inspect`

This PR stabilizes `result_option_inspect`:

```rust
// core::option

impl Option<T> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
}

// core::result

impl Result<T, E> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
    pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self;
}
```

<br>

Tracking issue: #91345.
Implementation PR: #91346.

Closes #91345.
  • Loading branch information
bors committed Nov 13, 2023
2 parents 531cb83 + ed512e9 commit 85b8450
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 16 deletions.
1 change: 0 additions & 1 deletion compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#![feature(if_let_guard)]
#![feature(let_chains)]
#![feature(never_type)]
#![feature(result_option_inspect)]
#![feature(rustc_attrs)]
#![feature(yeet_expr)]
#![feature(try_blocks)]
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
#![feature(extract_if)]
#![feature(intra_doc_pointers)]
#![feature(yeet_expr)]
#![feature(result_option_inspect)]
#![feature(const_option)]
#![feature(trait_alias)]
#![feature(ptr_alignment_type)]
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_trait_selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#![feature(let_chains)]
#![feature(if_let_guard)]
#![feature(never_type)]
#![feature(result_option_inspect)]
#![feature(type_alias_impl_trait)]
#![feature(min_specialization)]
#![recursion_limit = "512"] // For rustdoc
Expand Down
9 changes: 2 additions & 7 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,8 +1079,6 @@ impl<T> Option<T> {
/// # Examples
///
/// ```
/// #![feature(result_option_inspect)]
///
/// let v = vec![1, 2, 3, 4, 5];
///
/// // prints "got: 4"
Expand All @@ -1090,11 +1088,8 @@ impl<T> Option<T> {
/// let x: Option<&usize> = v.get(5).inspect(|x| println!("got: {x}"));
/// ```
#[inline]
#[unstable(feature = "result_option_inspect", issue = "91345")]
pub fn inspect<F>(self, f: F) -> Self
where
F: FnOnce(&T),
{
#[stable(feature = "result_option_inspect", since = "CURRENT_RUSTC_VERSION")]
pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self {
if let Some(ref x) = self {
f(x);
}
Expand Down
8 changes: 2 additions & 6 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,16 +835,14 @@ impl<T, E> Result<T, E> {
/// # Examples
///
/// ```
/// #![feature(result_option_inspect)]
///
/// let x: u8 = "4"
/// .parse::<u8>()
/// .inspect(|x| println!("original: {x}"))
/// .map(|x| x.pow(3))
/// .expect("failed to parse number");
/// ```
#[inline]
#[unstable(feature = "result_option_inspect", issue = "91345")]
#[stable(feature = "result_option_inspect", since = "CURRENT_RUSTC_VERSION")]
pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self {
if let Ok(ref t) = self {
f(t);
Expand All @@ -858,8 +856,6 @@ impl<T, E> Result<T, E> {
/// # Examples
///
/// ```
/// #![feature(result_option_inspect)]
///
/// use std::{fs, io};
///
/// fn read() -> io::Result<String> {
Expand All @@ -868,7 +864,7 @@ impl<T, E> Result<T, E> {
/// }
/// ```
#[inline]
#[unstable(feature = "result_option_inspect", issue = "91345")]
#[stable(feature = "result_option_inspect", since = "CURRENT_RUSTC_VERSION")]
pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self {
if let Err(ref e) = self {
f(e);
Expand Down

0 comments on commit 85b8450

Please sign in to comment.