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