]>
git.proxmox.com Git - rustc.git/blob - vendor/thread_local/src/unreachable.rs
1 // Copyright 2017 Amanieu d'Antras
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.
9 //! inlined from https://github.com/reem/rust-unreachable/
11 //! An unreachable code optimization hint in stable rust, and some useful
12 //! extension traits for `Option` and `Result`.
15 /// Hint to the optimizer that any code path which calls this function is
16 /// statically unreachable and can be removed.
18 /// Calling this function in reachable code invokes undefined behavior. Be
19 /// very, very sure this is what you want; often, a simple `panic!` is more
22 pub unsafe fn unreachable() -> ! {
23 /// The empty type for cases which can't occur.
25 let x
: &Void
= ::std
::mem
::transmute(1usize
);
29 /// An extension trait for `Option<T>` providing unchecked unwrapping methods.
30 pub trait UncheckedOptionExt
<T
> {
31 /// Get the value out of this Option without checking for None.
32 unsafe fn unchecked_unwrap(self) -> T
;
34 /// Assert that this Option is a None to the optimizer.
35 unsafe fn unchecked_unwrap_none(self);
38 /// An extension trait for `Result<T, E>` providing unchecked unwrapping methods.
39 pub trait UncheckedResultExt
<T
, E
> {
40 /// Get the value out of this Result without checking for Err.
41 unsafe fn unchecked_unwrap_ok(self) -> T
;
43 /// Get the error out of this Result without checking for Ok.
44 unsafe fn unchecked_unwrap_err(self) -> E
;
47 impl<T
> UncheckedOptionExt
<T
> for Option
<T
> {
48 unsafe fn unchecked_unwrap(self) -> T
{
55 unsafe fn unchecked_unwrap_none(self) {
56 if self.is_some() { unreachable() }
60 impl<T
, E
> UncheckedResultExt
<T
, E
> for Result
<T
, E
> {
61 unsafe fn unchecked_unwrap_ok(self) -> T
{
64 Err(_
) => unreachable()
68 unsafe fn unchecked_unwrap_err(self) -> E
{
70 Ok(_
) => unreachable(),