1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
#![no_std] #![warn( clippy::cargo, clippy::missing_docs_in_private_items, clippy::nursery, clippy::pedantic, missing_docs )] #![cfg_attr( feature = "nightly", feature(external_doc), doc(include = "../README.md") )] #![cfg_attr(not(feature = "nightly"), doc = "")] use core::fmt::Debug; /// Trait for [`unchecked_expect`](UncheckedUnwrap::unchecked_expect) and /// [`unchecked_unwrap`](UncheckedUnwrap::unchecked_unwrap). pub trait UncheckedUnwrap<T> { /// Unwraps an [`Option`] or [`Result`], yielding the content of a [`Some`] /// or [`Ok`]. /// /// This is the unchecked alternative to [`Option::expect`] and /// [`Result::expect`]. /// /// # Panics /// /// Only panics if <span class="module-item"><span class="stab portability" /// style="margin-right: 0">`cfg(debug_assertions)`</span></span> and <span /// class="module-item"><span class="stab portability" style="margin-right: /// 0">`feature="debug_checks"`</span></span> is enabled and the value is a /// [`None`] or [`Err`]. /// /// # Safety /// /// Callers of this function are responsible that [`Option`] or [`Result`] /// carries a [`Some`] or [`Ok`]. /// /// Failing that, the returned value may reference invalid memory or cause /// undefined behaviour. /// /// # Examples /// /// ``` /// use unchecked_unwrap::UncheckedUnwrap; /// /// let x = Some("value"); /// assert_eq!( /// unsafe { x.unchecked_expect("the world is ending") }, /// "value" /// ); /// /// let x: Result<u32, &str> = Ok(2); /// assert_eq!(unsafe { x.unchecked_expect("the sky is falling down") }, 2); /// ``` #[track_caller] unsafe fn unchecked_expect(self, msg: &str) -> T; /// Unwraps an [`Option`] or [`Result`], yielding the content of a [`Some`] /// or [`Ok`]. /// /// This is the unchecked alternative to [`Option::unwrap`] and /// [`Result::unwrap`]. /// /// # Panics /// /// Only panics if <span class="module-item"><span class="stab portability" /// style="margin-right: 0">`cfg(debug_assertions)`</span></span> and <span /// class="module-item"><span class="stab portability" style="margin-right: /// 0">`feature="debug_checks"`</span></span> is enabled and the value is a /// [`None`] or [`Err`]. /// /// # Safety /// /// Callers of this function are responsible that [`Option`] or [`Result`] /// carries a [`Some`] or [`Ok`]. /// /// Failing that, the returned value may reference invalid memory or cause /// undefined behaviour. /// /// # Examples /// /// ``` /// use unchecked_unwrap::UncheckedUnwrap; /// /// let x = Some("air"); /// assert_eq!(unsafe { x.unchecked_unwrap() }, "air"); /// /// let x: Result<u32, &str> = Ok(2); /// assert_eq!(unsafe { x.unchecked_unwrap() }, 2); /// ``` #[track_caller] unsafe fn unchecked_unwrap(self) -> T; } impl<T> UncheckedUnwrap<T> for Option<T> { unsafe fn unchecked_unwrap(self) -> T { #[allow(clippy::option_if_let_else)] if cfg!(debug_assertions) && cfg!(feature = "debug_checks") { self.unwrap() } else if let Some(value) = self { value } else { core::hint::unreachable_unchecked() } } unsafe fn unchecked_expect(self, msg: &str) -> T { #[allow(clippy::option_if_let_else)] if cfg!(debug_assertions) && cfg!(feature = "debug_checks") { self.expect(msg) } else if let Some(value) = self { value } else { core::hint::unreachable_unchecked() } } } impl<T, E: Debug> UncheckedUnwrap<T> for Result<T, E> { unsafe fn unchecked_unwrap(self) -> T { #[allow(clippy::option_if_let_else)] if cfg!(debug_assertions) && cfg!(feature = "debug_checks") { self.unwrap() } else if let Ok(value) = self { value } else { core::hint::unreachable_unchecked() } } unsafe fn unchecked_expect(self, msg: &str) -> T { #[allow(clippy::option_if_let_else)] if cfg!(debug_assertions) && cfg!(feature = "debug_checks") { self.expect(msg) } else if let Ok(value) = self { value } else { core::hint::unreachable_unchecked() } } }