1 // Copyright 2020 Amari Robinson
2 //
3 // Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4 // http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5 // http://opensource.org/licenses/MIT>, at your option. This file may not be
6 // copied, modified, or distributed except according to those terms.
7 
8 use core::hint;
9 
10 /// An extension trait on `Option`.
11 pub trait UncheckedOptionExt<T> {
12     /// Returns the contained value.
13     ///
14     /// # Safety
15     ///
16     /// It is up to the caller to guarantee that the `Option<T>` is `Some(v)`.
17     /// Calling this when it is `None` causes undefined behavior.
unwrap_unchecked(self) -> T18     unsafe fn unwrap_unchecked(self) -> T;
19 }
20 
21 impl<T> UncheckedOptionExt<T> for Option<T> {
22     #[inline]
unwrap_unchecked(self) -> T23     unsafe fn unwrap_unchecked(self) -> T {
24         match self {
25             Some(x) => x,
26             None => {
27                 if cfg!(debug_assertions) {
28                     unreachable!()
29                 } else {
30                     hint::unreachable_unchecked()
31                 }
32             }
33         }
34     }
35 }
36