diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 28ea45ed235dc..22c9781a823de 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -739,6 +739,26 @@ impl Option { } } + /// 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 Option { } } + /// 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 Option { } } + /// 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 + 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 + 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 Option { } } + /// 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 + where + F: FnOnce(&'r T) -> Option, + { + 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 + where + F: FnOnce(&'r mut T) -> Option, + { + self.as_mut().and_then(f) + } + /// Returns [`None`] if the option is [`None`], otherwise calls `predicate` /// with the wrapped value and returns: ///