| 1 | #![cfg_attr(test, deny(warnings))] |
| 2 | #![deny(missing_docs)] |
| 3 | |
| 4 | //! # unreachable |
| 5 | //! |
| 6 | //! An unreachable code optimization hint in stable rust, and some useful |
| 7 | //! extension traits for `Option` and `Result`. |
| 8 | //! |
| 9 | |
| 10 | #![no_std] |
| 11 | |
| 12 | extern crate voidrust-analyzer cargo unreachable 1.0.0 void/; |
| 13 | |
| 14 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/; |
| 15 | |
| 16 | /// Hint to the optimizer that any code path which calls this function is |
| 17 | /// statically unreachable and can be removed. |
| 18 | /// |
| 19 | /// Calling this function in reachable code invokes undefined behavior. Be |
| 20 | /// very, very sure this is what you want; often, a simple `panic!` is more |
| 21 | /// suitable. |
| 22 | #[inline] |
| 23 | pub unsafe fn unreachablerust-analyzer cargo unreachable 1.0.0 unreachable().() -> ! { |
| 24 | let x: &voidrust-analyzer cargo void 1.0.2 crate/::Voidrust-analyzer cargo void 1.0.2 Void# = memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute().(1usize); |
| 25 | voidrust-analyzer cargo void 1.0.2 crate/::unreachablerust-analyzer cargo void 1.0.2 unreachable().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().x) |
| 26 | } |
| 27 | |
| 28 | /// An extension trait for `Option<T>` providing unchecked unwrapping methods. |
| 29 | pub trait UncheckedOptionExtrust-analyzer cargo unreachable 1.0.0 UncheckedOptionExt#<T> { |
| 30 | /// Get the value out of this Option without checking for None. |
| 31 | unsafe fn unchecked_unwraprust-analyzer cargo unreachable 1.0.0 UncheckedOptionExt#unchecked_unwrap().(self) -> T; |
| 32 | |
| 33 | /// Assert that this Option is a None to the optimizer. |
| 34 | unsafe fn unchecked_unwrap_nonerust-analyzer cargo unreachable 1.0.0 UncheckedOptionExt#unchecked_unwrap_none().(self); |
| 35 | } |
| 36 | |
| 37 | /// An extension trait for `Result<T, E>` providing unchecked unwrapping methods. |
| 38 | pub trait UncheckedResultExtrust-analyzer cargo unreachable 1.0.0 UncheckedResultExt#<T, E> { |
| 39 | /// Get the value out of this Result without checking for Err. |
| 40 | unsafe fn unchecked_unwrap_okrust-analyzer cargo unreachable 1.0.0 UncheckedResultExt#unchecked_unwrap_ok().(self) -> T; |
| 41 | |
| 42 | /// Get the error out of this Result without checking for Ok. |
| 43 | unsafe fn unchecked_unwrap_errrust-analyzer cargo unreachable 1.0.0 UncheckedResultExt#unchecked_unwrap_err().(self) -> E; |
| 44 | } |
| 45 | |
| 46 | impl<T> UncheckedOptionExtrust-analyzer cargo unreachable 1.0.0 UncheckedOptionExt#<T> for Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T> { |
| 47 | unsafe fn unchecked_unwraprust-analyzer cargo unreachable 1.0.0 impl#[`Option<T>`][`UncheckedOptionExt<T>`]unchecked_unwrap().(self) -> T { |
| 48 | match self { |
| 49 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(x) => x, |
| 50 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => unreachablerust-analyzer cargo unreachable 1.0.0 unreachable().() |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | unsafe fn unchecked_unwrap_nonerust-analyzer cargo unreachable 1.0.0 impl#[`Option<T>`][`UncheckedOptionExt<T>`]unchecked_unwrap_none().(self) { |
| 55 | match self { |
| 56 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(_) => unreachablerust-analyzer cargo unreachable 1.0.0 unreachable().(), |
| 57 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => () |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl<T, E> UncheckedResultExtrust-analyzer cargo unreachable 1.0.0 UncheckedResultExt#<T, E> for Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, E> { |
| 63 | unsafe fn unchecked_unwrap_okrust-analyzer cargo unreachable 1.0.0 impl#[`Result<T, E>`][`UncheckedResultExt<T, E>`]unchecked_unwrap_ok().(self) -> T { |
| 64 | match self { |
| 65 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(x) => x, |
| 66 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => unreachablerust-analyzer cargo unreachable 1.0.0 unreachable().() |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | unsafe fn unchecked_unwrap_errrust-analyzer cargo unreachable 1.0.0 impl#[`Result<T, E>`][`UncheckedResultExt<T, E>`]unchecked_unwrap_err().(self) -> E { |
| 71 | match self { |
| 72 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(_) => unreachablerust-analyzer cargo unreachable 1.0.0 unreachable().(), |
| 73 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => e |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 |