actix_service/
macros.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/// An implementation of [`poll_ready`]() that always signals readiness.
///
/// This should only be used for basic leaf services that have no concept of un-readiness.
/// For wrapper or other service types, use [`forward_ready!`] for simple cases or write a bespoke
/// `poll_ready` implementation.
///
/// [`poll_ready`]: crate::Service::poll_ready
///
/// # Examples
/// ```no_run
/// use actix_service::Service;
/// use futures_util::future::{ready, Ready};
///
/// struct IdentityService;
///
/// impl Service<u32> for IdentityService {
///     type Response = u32;
///     type Error = ();
///     type Future = Ready<Result<Self::Response, Self::Error>>;
///
///     actix_service::always_ready!();
///
///     fn call(&self, req: u32) -> Self::Future {
///         ready(Ok(req))
///     }
/// }
/// ```
///
/// [`forward_ready!`]: crate::forward_ready
#[macro_export]
macro_rules! always_ready {
    () => {
        #[inline]
        fn poll_ready(
            &self,
            _: &mut ::core::task::Context<'_>,
        ) -> ::core::task::Poll<Result<(), Self::Error>> {
            ::core::task::Poll::Ready(Ok(()))
        }
    };
}

/// An implementation of [`poll_ready`] that forwards readiness checks to a
/// named struct field.
///
/// Tuple structs are not supported.
///
/// [`poll_ready`]: crate::Service::poll_ready
///
/// # Examples
/// ```no_run
/// use actix_service::Service;
/// use futures_util::future::{ready, Ready};
///
/// struct WrapperService<S> {
///     inner: S,
/// }
///
/// impl<S> Service<()> for WrapperService<S>
/// where
///     S: Service<()>,
/// {
///     type Response = S::Response;
///     type Error = S::Error;
///     type Future = S::Future;
///
///     actix_service::forward_ready!(inner);
///
///     fn call(&self, req: ()) -> Self::Future {
///         self.inner.call(req)
///     }
/// }
/// ```
#[macro_export]
macro_rules! forward_ready {
    ($field:ident) => {
        #[inline]
        fn poll_ready(
            &self,
            cx: &mut ::core::task::Context<'_>,
        ) -> ::core::task::Poll<Result<(), Self::Error>> {
            self.$field
                .poll_ready(cx)
                .map_err(::core::convert::Into::into)
        }
    };
}

#[cfg(test)]
mod tests {
    use core::{
        cell::Cell,
        convert::Infallible,
        task::{self, Context, Poll},
    };

    use futures_util::{
        future::{ready, Ready},
        task::noop_waker,
    };

    use crate::Service;

    struct IdentityService;

    impl Service<u32> for IdentityService {
        type Response = u32;
        type Error = Infallible;
        type Future = Ready<Result<Self::Response, Self::Error>>;

        always_ready!();

        fn call(&self, req: u32) -> Self::Future {
            ready(Ok(req))
        }
    }

    struct CountdownService(Cell<u32>);

    impl Service<()> for CountdownService {
        type Response = ();
        type Error = Infallible;
        type Future = Ready<Result<Self::Response, Self::Error>>;

        fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
            let count = self.0.get();

            if count == 0 {
                Poll::Ready(Ok(()))
            } else {
                self.0.set(count - 1);
                cx.waker().wake_by_ref();
                Poll::Pending
            }
        }

        fn call(&self, _: ()) -> Self::Future {
            ready(Ok(()))
        }
    }

    struct WrapperService<S> {
        inner: S,
    }

    impl<S> Service<()> for WrapperService<S>
    where
        S: Service<()>,
    {
        type Response = S::Response;
        type Error = S::Error;
        type Future = S::Future;

        forward_ready!(inner);

        fn call(&self, _: ()) -> Self::Future {
            self.inner.call(())
        }
    }

    #[test]
    fn test_always_ready_macro() {
        let waker = noop_waker();
        let mut cx = task::Context::from_waker(&waker);

        let svc = IdentityService;

        assert!(svc.poll_ready(&mut cx).is_ready());
        assert!(svc.poll_ready(&mut cx).is_ready());
        assert!(svc.poll_ready(&mut cx).is_ready());
    }

    #[test]
    fn test_forward_ready_macro() {
        let waker = noop_waker();
        let mut cx = task::Context::from_waker(&waker);

        let svc = WrapperService {
            inner: CountdownService(Cell::new(3)),
        };

        assert!(svc.poll_ready(&mut cx).is_pending());
        assert!(svc.poll_ready(&mut cx).is_pending());
        assert!(svc.poll_ready(&mut cx).is_pending());
        assert!(svc.poll_ready(&mut cx).is_ready());
    }
}