1 #![allow(deprecated)] 2 3 use Delay; 4 5 use futures::{Async, Future, Poll}; 6 7 use std::error; 8 use std::fmt; 9 use std::time::Instant; 10 11 #[deprecated(since = "0.2.6", note = "use Timeout instead")] 12 #[doc(hidden)] 13 #[derive(Debug)] 14 pub struct Deadline<T> { 15 future: T, 16 delay: Delay, 17 } 18 19 #[deprecated(since = "0.2.6", note = "use Timeout instead")] 20 #[doc(hidden)] 21 #[derive(Debug)] 22 pub struct DeadlineError<T>(Kind<T>); 23 24 /// Deadline error variants 25 #[derive(Debug)] 26 enum Kind<T> { 27 /// Inner future returned an error 28 Inner(T), 29 30 /// The deadline elapsed. 31 Elapsed, 32 33 /// Timer returned an error. 34 Timer(::Error), 35 } 36 37 impl<T> Deadline<T> { 38 /// Create a new `Deadline` that completes when `future` completes or when 39 /// `deadline` is reached. new(future: T, deadline: Instant) -> Deadline<T>40 pub fn new(future: T, deadline: Instant) -> Deadline<T> { 41 Deadline::new_with_delay(future, Delay::new(deadline)) 42 } 43 new_with_delay(future: T, delay: Delay) -> Deadline<T>44 pub(crate) fn new_with_delay(future: T, delay: Delay) -> Deadline<T> { 45 Deadline { future, delay } 46 } 47 48 /// Gets a reference to the underlying future in this deadline. get_ref(&self) -> &T49 pub fn get_ref(&self) -> &T { 50 &self.future 51 } 52 53 /// Gets a mutable reference to the underlying future in this deadline. get_mut(&mut self) -> &mut T54 pub fn get_mut(&mut self) -> &mut T { 55 &mut self.future 56 } 57 58 /// Consumes this deadline, returning the underlying future. into_inner(self) -> T59 pub fn into_inner(self) -> T { 60 self.future 61 } 62 } 63 64 impl<T> Future for Deadline<T> 65 where 66 T: Future, 67 { 68 type Item = T::Item; 69 type Error = DeadlineError<T::Error>; 70 poll(&mut self) -> Poll<Self::Item, Self::Error>71 fn poll(&mut self) -> Poll<Self::Item, Self::Error> { 72 // First, try polling the future 73 match self.future.poll() { 74 Ok(Async::Ready(v)) => return Ok(Async::Ready(v)), 75 Ok(Async::NotReady) => {} 76 Err(e) => return Err(DeadlineError::inner(e)), 77 } 78 79 // Now check the timer 80 match self.delay.poll() { 81 Ok(Async::NotReady) => Ok(Async::NotReady), 82 Ok(Async::Ready(_)) => Err(DeadlineError::elapsed()), 83 Err(e) => Err(DeadlineError::timer(e)), 84 } 85 } 86 } 87 88 // ===== impl DeadlineError ===== 89 90 impl<T> DeadlineError<T> { 91 /// Create a new `DeadlineError` representing the inner future completing 92 /// with `Err`. inner(err: T) -> DeadlineError<T>93 pub fn inner(err: T) -> DeadlineError<T> { 94 DeadlineError(Kind::Inner(err)) 95 } 96 97 /// Returns `true` if the error was caused by the inner future completing 98 /// with `Err`. is_inner(&self) -> bool99 pub fn is_inner(&self) -> bool { 100 match self.0 { 101 Kind::Inner(_) => true, 102 _ => false, 103 } 104 } 105 106 /// Consumes `self`, returning the inner future error. into_inner(self) -> Option<T>107 pub fn into_inner(self) -> Option<T> { 108 match self.0 { 109 Kind::Inner(err) => Some(err), 110 _ => None, 111 } 112 } 113 114 /// Create a new `DeadlineError` representing the inner future not 115 /// completing before the deadline is reached. elapsed() -> DeadlineError<T>116 pub fn elapsed() -> DeadlineError<T> { 117 DeadlineError(Kind::Elapsed) 118 } 119 120 /// Returns `true` if the error was caused by the inner future not 121 /// completing before the deadline is reached. is_elapsed(&self) -> bool122 pub fn is_elapsed(&self) -> bool { 123 match self.0 { 124 Kind::Elapsed => true, 125 _ => false, 126 } 127 } 128 129 /// Creates a new `DeadlineError` representing an error encountered by the 130 /// timer implementation timer(err: ::Error) -> DeadlineError<T>131 pub fn timer(err: ::Error) -> DeadlineError<T> { 132 DeadlineError(Kind::Timer(err)) 133 } 134 135 /// Returns `true` if the error was caused by the timer. is_timer(&self) -> bool136 pub fn is_timer(&self) -> bool { 137 match self.0 { 138 Kind::Timer(_) => true, 139 _ => false, 140 } 141 } 142 143 /// Consumes `self`, returning the error raised by the timer implementation. into_timer(self) -> Option<::Error>144 pub fn into_timer(self) -> Option<::Error> { 145 match self.0 { 146 Kind::Timer(err) => Some(err), 147 _ => None, 148 } 149 } 150 } 151 152 impl<T: error::Error> error::Error for DeadlineError<T> { description(&self) -> &str153 fn description(&self) -> &str { 154 use self::Kind::*; 155 156 match self.0 { 157 Inner(ref e) => e.description(), 158 Elapsed => "deadline has elapsed", 159 Timer(ref e) => e.description(), 160 } 161 } 162 } 163 164 impl<T: fmt::Display> fmt::Display for DeadlineError<T> { fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result165 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 166 use self::Kind::*; 167 168 match self.0 { 169 Inner(ref e) => e.fmt(fmt), 170 Elapsed => "deadline has elapsed".fmt(fmt), 171 Timer(ref e) => e.fmt(fmt), 172 } 173 } 174 } 175