#![stable(feature = "rust1", since = "1.0.0")]
use crate::iter::{FromIterator, FusedIterator, TrustedLen};
-use crate::{convert, fmt, hint, mem, ops::{self, Deref, DerefMut}};
use crate::pin::Pin;
+use crate::{
+ convert, fmt, hint, mem,
+ ops::{self, Deref, DerefMut},
+};
// Note that this is not a lang item per se, but it has a hidden dependency on
// `Iterator`, which is one. The compiler assumes that the `next` method of
/// The `Option` type. See [the module level documentation](index.html) for more.
#[derive(Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
+#[rustc_diagnostic_item = "option_type"]
#[stable(feature = "rust1", since = "1.0.0")]
pub enum Option<T> {
/// No value
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_some(&self) -> bool {
- match *self {
- Some(_) => true,
- None => false,
- }
+ matches!(*self, Some(_))
}
/// Returns `true` if the option is a [`None`] value.
#[must_use]
#[inline]
#[unstable(feature = "option_result_contains", issue = "62358")]
- pub fn contains<U>(&self, x: &U) -> bool where U: PartialEq<T> {
+ pub fn contains<U>(&self, x: &U) -> bool
+ where
+ U: PartialEq<T>,
+ {
match self {
Some(y) => x == y,
None => false,
}
}
-
/// Converts from [`Pin`]`<&Option<T>>` to `Option<`[`Pin`]`<&T>>`.
///
/// [`Pin`]: ../pin/struct.Pin.html
#[inline]
#[stable(feature = "pin", since = "1.33.0")]
pub fn as_pin_ref(self: Pin<&Self>) -> Option<Pin<&T>> {
- unsafe {
- Pin::get_ref(self).as_ref().map(|x| Pin::new_unchecked(x))
- }
+ unsafe { Pin::get_ref(self).as_ref().map(|x| Pin::new_unchecked(x)) }
}
/// Converts from [`Pin`]`<&mut Option<T>>` to `Option<`[`Pin`]`<&mut T>>`.
#[inline]
#[stable(feature = "pin", since = "1.33.0")]
pub fn as_pin_mut(self: Pin<&mut Self>) -> Option<Pin<&mut T>> {
- unsafe {
- Pin::get_unchecked_mut(self).as_mut().map(|x| Pin::new_unchecked(x))
- }
+ unsafe { Pin::get_unchecked_mut(self).as_mut().map(|x| Pin::new_unchecked(x)) }
}
/////////////////////////////////////////////////////////////////////////
/// x.expect("the world is ending"); // panics with `the world is ending`
/// ```
#[inline]
+ #[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn expect(self, msg: &str) -> T {
match self {
/// assert_eq!(x.unwrap(), "air"); // fails
/// ```
#[inline]
+ #[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn unwrap(self) -> T {
match self {
pub fn filter<P: FnOnce(&T) -> bool>(self, predicate: P) -> Self {
if let Some(x) = self {
if predicate(&x) {
- return Some(x)
+ return Some(x);
}
}
None
/// }
/// ```
#[inline]
+ #[track_caller]
#[unstable(feature = "option_expect_none", reason = "newly added", issue = "62633")]
pub fn expect_none(self, msg: &str) {
if let Some(val) = self {
/// }
/// ```
#[inline]
+ #[track_caller]
#[unstable(feature = "option_unwrap_none", reason = "newly added", issue = "62633")]
pub fn unwrap_none(self) {
if let Some(val) = self {
// This is a separate function to reduce the code size of .expect() itself.
#[inline(never)]
#[cold]
+#[track_caller]
fn expect_failed(msg: &str) -> ! {
panic!("{}", msg)
}
// This is a separate function to reduce the code size of .expect_none() itself.
#[inline(never)]
#[cold]
+#[track_caller]
fn expect_none_failed(msg: &str, value: &dyn fmt::Debug) -> ! {
panic!("{}: {:?}", msg, value)
}
/// assert!(opt.is_none());
/// ```
#[inline]
- fn default() -> Option<T> { None }
+ fn default() -> Option<T> {
+ None
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[derive(Clone, Debug)]
struct Item<A> {
- opt: Option<A>
+ opt: Option<A>,
}
impl<A> Iterator for Item<A> {
/// [`Option::iter`]: enum.Option.html#method.iter
#[stable(feature = "rust1", since = "1.0.0")]
#[derive(Debug)]
-pub struct Iter<'a, A: 'a> { inner: Item<&'a A> }
+pub struct Iter<'a, A: 'a> {
+ inner: Item<&'a A>,
+}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, A> Iterator for Iter<'a, A> {
type Item = &'a A;
#[inline]
- fn next(&mut self) -> Option<&'a A> { self.inner.next() }
+ fn next(&mut self) -> Option<&'a A> {
+ self.inner.next()
+ }
#[inline]
- fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ self.inner.size_hint()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, A> DoubleEndedIterator for Iter<'a, A> {
#[inline]
- fn next_back(&mut self) -> Option<&'a A> { self.inner.next_back() }
+ fn next_back(&mut self) -> Option<&'a A> {
+ self.inner.next_back()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
/// [`Option::iter_mut`]: enum.Option.html#method.iter_mut
#[stable(feature = "rust1", since = "1.0.0")]
#[derive(Debug)]
-pub struct IterMut<'a, A: 'a> { inner: Item<&'a mut A> }
+pub struct IterMut<'a, A: 'a> {
+ inner: Item<&'a mut A>,
+}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, A> Iterator for IterMut<'a, A> {
type Item = &'a mut A;
#[inline]
- fn next(&mut self) -> Option<&'a mut A> { self.inner.next() }
+ fn next(&mut self) -> Option<&'a mut A> {
+ self.inner.next()
+ }
#[inline]
- fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ self.inner.size_hint()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, A> DoubleEndedIterator for IterMut<'a, A> {
#[inline]
- fn next_back(&mut self) -> Option<&'a mut A> { self.inner.next_back() }
+ fn next_back(&mut self) -> Option<&'a mut A> {
+ self.inner.next_back()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
/// [`Option::into_iter`]: enum.Option.html#method.into_iter
#[derive(Clone, Debug)]
#[stable(feature = "rust1", since = "1.0.0")]
-pub struct IntoIter<A> { inner: Item<A> }
+pub struct IntoIter<A> {
+ inner: Item<A>,
+}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A> Iterator for IntoIter<A> {
type Item = A;
#[inline]
- fn next(&mut self) -> Option<A> { self.inner.next() }
+ fn next(&mut self) -> Option<A> {
+ self.inner.next()
+ }
#[inline]
- fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ self.inner.size_hint()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A> DoubleEndedIterator for IntoIter<A> {
#[inline]
- fn next_back(&mut self) -> Option<A> { self.inner.next_back() }
+ fn next_back(&mut self) -> Option<A> {
+ self.inner.next_back()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
///
/// [`Iterator`]: ../iter/trait.Iterator.html
#[inline]
- fn from_iter<I: IntoIterator<Item=Option<A>>>(iter: I) -> Option<V> {
+ fn from_iter<I: IntoIterator<Item = Option<A>>>(iter: I) -> Option<V> {
// FIXME(#11084): This could be replaced with Iterator::scan when this
// performance bug is closed.
- iter.into_iter()
- .map(|x| x.ok_or(()))
- .collect::<Result<_, _>>()
- .ok()
+ iter.into_iter().map(|x| x.ok_or(())).collect::<Result<_, _>>().ok()
}
}