actix/fut/future/
timeout.rs1use std::{
2 future::Future,
3 pin::Pin,
4 task::{Context, Poll},
5 time::Duration,
6};
7
8use pin_project_lite::pin_project;
9
10use crate::{
11 actor::Actor,
12 clock::{sleep, Sleep},
13 fut::ActorFuture,
14};
15
16pin_project! {
17 #[derive(Debug)]
22 #[must_use = "futures do nothing unless polled"]
23 pub struct Timeout<F>{
24 #[pin]
25 fut: F,
26 #[pin]
27 timeout: Sleep,
28 }
29}
30
31impl<F> Timeout<F> {
32 pub(super) fn new(future: F, timeout: Duration) -> Self {
33 Self {
34 fut: future,
35 timeout: sleep(timeout),
36 }
37 }
38}
39
40impl<F, A> ActorFuture<A> for Timeout<F>
41where
42 F: ActorFuture<A>,
43 A: Actor,
44{
45 type Output = Result<F::Output, ()>;
46
47 fn poll(
48 self: Pin<&mut Self>,
49 act: &mut A,
50 ctx: &mut A::Context,
51 task: &mut Context<'_>,
52 ) -> Poll<Self::Output> {
53 let this = self.project();
54 match this.fut.poll(act, ctx, task) {
55 Poll::Ready(res) => Poll::Ready(Ok(res)),
56 Poll::Pending => this.timeout.poll(task).map(Err),
57 }
58 }
59}