1 // Copyright 2016 Amanieu d'Antras
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 instant::Instant;
9 use std::time::Duration;
10 
11 // Option::unchecked_unwrap
12 pub trait UncheckedOptionExt<T> {
unchecked_unwrap(self) -> T13     unsafe fn unchecked_unwrap(self) -> T;
14 }
15 
16 impl<T> UncheckedOptionExt<T> for Option<T> {
17     #[inline]
unchecked_unwrap(self) -> T18     unsafe fn unchecked_unwrap(self) -> T {
19         match self {
20             Some(x) => x,
21             None => unreachable(),
22         }
23     }
24 }
25 
26 // hint::unreachable_unchecked() in release mode
27 #[inline]
unreachable() -> !28 unsafe fn unreachable() -> ! {
29     if cfg!(debug_assertions) {
30         unreachable!();
31     } else {
32         core::hint::unreachable_unchecked()
33     }
34 }
35 
36 #[inline]
to_deadline(timeout: Duration) -> Option<Instant>37 pub fn to_deadline(timeout: Duration) -> Option<Instant> {
38     Instant::now().checked_add(timeout)
39 }
40