|
229 | 229 |
|
230 | 230 | use crate::iter::{self, FromIterator, FusedIterator, TrustedLen};
|
231 | 231 | use crate::ops::{self, Deref, DerefMut};
|
232 |
| -use crate::{convert, fmt}; |
| 232 | +use crate::{convert, fmt, hint}; |
233 | 233 |
|
234 | 234 | /// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]).
|
235 | 235 | ///
|
@@ -821,6 +821,68 @@ impl<T, E> Result<T, E> {
|
821 | 821 | Err(e) => op(e),
|
822 | 822 | }
|
823 | 823 | }
|
| 824 | + |
| 825 | + /// Returns the contained [`Ok`] value, consuming the `self` value, |
| 826 | + /// without checking that the value is not an [`Err`]. |
| 827 | + /// |
| 828 | + /// # Safety |
| 829 | + /// |
| 830 | + /// Undefined behavior if the value is an [`Err`]. |
| 831 | + /// |
| 832 | + /// # Examples |
| 833 | + /// |
| 834 | + /// ``` |
| 835 | + /// #![feature(option_result_unwrap_unchecked)] |
| 836 | + /// let x: Result<u32, &str> = Ok(2); |
| 837 | + /// assert_eq!(unsafe { x.unwrap_unchecked() }, 2); |
| 838 | + /// ``` |
| 839 | + /// |
| 840 | + /// ```no_run |
| 841 | + /// #![feature(option_result_unwrap_unchecked)] |
| 842 | + /// let x: Result<u32, &str> = Err("emergency failure"); |
| 843 | + /// unsafe { x.unwrap_unchecked(); } // Undefined behavior! |
| 844 | + /// ``` |
| 845 | + #[inline] |
| 846 | + #[track_caller] |
| 847 | + #[unstable(feature = "option_result_unwrap_unchecked", reason = "newly added", issue = "none")] |
| 848 | + pub unsafe fn unwrap_unchecked(self) -> T { |
| 849 | + debug_assert!(self.is_ok()); |
| 850 | + match self { |
| 851 | + Ok(t) => t, |
| 852 | + Err(_) => unsafe { hint::unreachable_unchecked() }, |
| 853 | + } |
| 854 | + } |
| 855 | + |
| 856 | + /// Returns the contained [`Err`] value, consuming the `self` value, |
| 857 | + /// without checking that the value is not an [`Ok`]. |
| 858 | + /// |
| 859 | + /// # Safety |
| 860 | + /// |
| 861 | + /// Undefined behavior if the value is an [`Ok`]. |
| 862 | + /// |
| 863 | + /// # Examples |
| 864 | + /// |
| 865 | + /// ```no_run |
| 866 | + /// #![feature(option_result_unwrap_unchecked)] |
| 867 | + /// let x: Result<u32, &str> = Ok(2); |
| 868 | + /// unsafe { x.unwrap_err_unchecked() }; // Undefined behavior! |
| 869 | + /// ``` |
| 870 | + /// |
| 871 | + /// ``` |
| 872 | + /// #![feature(option_result_unwrap_unchecked)] |
| 873 | + /// let x: Result<u32, &str> = Err("emergency failure"); |
| 874 | + /// assert_eq!(unsafe { x.unwrap_err_unchecked() }, "emergency failure"); |
| 875 | + /// ``` |
| 876 | + #[inline] |
| 877 | + #[track_caller] |
| 878 | + #[unstable(feature = "option_result_unwrap_unchecked", reason = "newly added", issue = "none")] |
| 879 | + pub unsafe fn unwrap_err_unchecked(self) -> E { |
| 880 | + debug_assert!(self.is_err()); |
| 881 | + match self { |
| 882 | + Ok(_) => unsafe { hint::unreachable_unchecked() }, |
| 883 | + Err(e) => e, |
| 884 | + } |
| 885 | + } |
824 | 886 | }
|
825 | 887 |
|
826 | 888 | impl<T: Copy, E> Result<&T, E> {
|
|
0 commit comments