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
144
145
146
147
148
149
150
151
152
153
#![no_std]
#![warn(
    clippy::all,
    clippy::cargo,
    clippy::missing_docs_in_private_items,
    clippy::pedantic,
    clippy::nursery,
    missing_docs
)]
#![cfg_attr(
    feature = "nightly",
    feature(external_doc, track_caller),
    doc(include = "../README.md")
)]
#![cfg_attr(not(feature = "nightly"), doc = "")]

use core::fmt::Debug;

/// Trait for [`unchecked_expect`](UncheckedExpect::unchecked_expect).
pub trait UncheckedExpect<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 `cfg(debug_assertions)` and <span
    /// class="module-item"><span class="stab portability" style="margin-right:
    /// 0">`feature="debug_checks"`</span></span> is enabled.
    ///
    /// Panics if the value is a [`None`] or [`Err`], with a custom panic
    /// message provided by `msg` and if [`Result`] with the content of the
    /// [`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::UncheckedExpect;
    ///
    /// 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);
    /// ```
    #[cfg_attr(feature = "nightly", track_caller)]
    unsafe fn unchecked_expect(self, msg: &str) -> T;
}

/// Trait for [`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::unwrap`]
    /// and [`Result::unwrap`].
    ///
    /// # Panics
    ///
    /// Only panics if `cfg(debug_assertions)` and <span
    /// class="module-item"><span class="stab portability" style="margin-right:
    /// 0">`feature="debug_checks"`</span></span> is enabled.
    ///
    /// Panics if the value is a [`None`] or [`Err`], if [`Result`] with a panic
    /// massage provided by the [`Err`]'s value.
    ///
    /// # 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);
    /// ```
    #[cfg_attr(feature = "nightly", track_caller)]
    unsafe fn unchecked_unwrap(self) -> T;
}

impl<T> UncheckedExpect<T> for Option<T> {
    /// Unwraps an [`Option`], yielding the content of a [`Some`].
    /// This is the unchecked alternative to [`expect`](Option::expect).
    unsafe fn unchecked_expect(self, msg: &str) -> T {
        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> UncheckedExpect<T> for Result<T, E> {
    /// Unwraps a [`Result`], yielding the content of an [`Ok`].
    /// This is the unchecked alternative to [`expect`](Result::expect).
    unsafe fn unchecked_expect(self, msg: &str) -> T {
        if cfg!(debug_assertions) && cfg!(feature = "debug_checks") {
            self.expect(msg)
        } else if let Ok(value) = self {
            value
        } else {
            core::hint::unreachable_unchecked()
        }
    }
}

impl<T> UncheckedUnwrap<T> for Option<T> {
    /// Unwraps a [`Option`], yielding the content of an [`Some`].
    /// This is the unchecked alternative to [`unwrap`](Option::unwrap).
    unsafe fn unchecked_unwrap(self) -> T {
        if cfg!(debug_assertions) && cfg!(feature = "debug_checks") {
            self.unwrap()
        } else if let Some(value) = self {
            value
        } else {
            core::hint::unreachable_unchecked()
        }
    }
}

impl<T, E: Debug> UncheckedUnwrap<T> for Result<T, E> {
    /// Unwraps a [`Result`], yielding the content of an [`Ok`].
    /// This is the unchecked alternative to [`unwrap`](Result::unwrap).
    unsafe fn unchecked_unwrap(self) -> T {
        if cfg!(debug_assertions) && cfg!(feature = "debug_checks") {
            self.unwrap()
        } else if let Ok(value) = self {
            value
        } else {
            core::hint::unreachable_unchecked()
        }
    }
}