File: attr_into_future.rs

package info (click to toggle)
rust-bon 3.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 720 kB
  • sloc: makefile: 2
file content (257 lines) | stat: -rw-r--r-- 7,683 bytes parent folder | download
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/// [`core::future::IntoFuture`] relies on [`Box`]. Also this trait was
/// introduced in Rust 1.64, while `bon`'s MSRV is 1.59 at the time of this
/// writing.
#[cfg(any(feature = "std", feature = "alloc"))]
#[rustversion::since(1.64)]
mod tests {
    use crate::prelude::*;
    use core::future::{ready, IntoFuture};
    use core::marker::PhantomData;

    async fn assert_send<B>(builder: B) -> B::Output
    where
        B: IntoFuture + Send,
        B::IntoFuture: Send,
    {
        #[expect(clippy::incompatible_msrv)]
        let fut = builder.into_future();
        let _: &dyn Send = &fut;
        fut.await
    }

    #[expect(clippy::future_not_send)]
    async fn non_send_future() {
        // By keeping `Rc` across an await point, we force the compiler to store it
        // as part of the future's state machine struct and thus we make it non-Send
        let non_send = PhantomData::<Rc<()>>;

        ready(()).await;

        let _ = &non_send;
    }

    mod test_fn {
        use super::*;

        #[tokio::test]
        async fn basic() {
            #[builder(derive(IntoFuture(Box)))]
            async fn simple_async_fn(value: u32) -> u32 {
                ready(value * 2).await
            }

            // Test direct call.
            let builder = simple_async_fn().value(21).call();
            assert_eq!(assert_send(builder).await, 42);

            // Test using IntoFuture with await.
            let builder = simple_async_fn().value(21);
            assert_eq!(assert_send(builder).await, 42);
        }

        #[tokio::test]
        async fn non_send() {
            #[builder(derive(IntoFuture(Box, ?Send)))]
            #[expect(clippy::future_not_send)]
            async fn non_send_async_fn(value: u32) -> u32 {
                non_send_future().await;
                // This future can be !Send.
                ready(value * 2).await
            }

            // Test with non-Send future.
            let result = non_send_async_fn().value(21).await;

            assert_eq!(result, 42);
        }

        #[tokio::test]
        async fn result() {
            #[builder(derive(IntoFuture(Box)))]
            async fn async_with_result(value: u32) -> Result<u32, &'static str> {
                ready(if value > 0 {
                    Ok(value * 2)
                } else {
                    Err("Value must be positive")
                })
                .await
            }

            // Test successful case.
            let builder = async_with_result().value(21);
            assert_eq!(assert_send(builder).await.unwrap(), 42);

            // Test error case.
            let builder = async_with_result().value(0);

            assert_send(builder).await.unwrap_err();
        }

        #[tokio::test]
        async fn into_future_with_optional() {
            #[builder(derive(IntoFuture(Box)))]
            async fn optional_param(#[builder(default = 100)] value: u32) -> u32 {
                ready(value).await
            }

            // Test with value.
            let builder = optional_param().value(42);
            assert_eq!(assert_send(builder).await, 42);

            // Test without value (using default).
            let builder = optional_param();
            assert_eq!(assert_send(builder).await, 100);
        }

        #[tokio::test]
        async fn references_in_params() {
            struct Dummy;

            #[builder(derive(IntoFuture(Box)))]
            async fn sut<'named1, 'named2>(
                _x1: &Dummy,
                _x2: &Dummy,
                x3: &'named1 Dummy,
                x4: &'named2 Dummy,
            ) -> &'named2 Dummy {
                let _: &'named1 Dummy = x3;
                ready(x4).await
            }

            // Store the dummy struct in local variables to make sure no `'static`
            // lifetime promotion happens
            let local_x1 = Dummy;
            let local_x2 = Dummy;
            let local_x3 = Dummy;
            let local_x4 = Dummy;

            let builder = sut()
                .x1(&local_x1)
                .x2(&local_x2)
                .x3(&local_x3)
                .x4(&local_x4);

            let &Dummy = assert_send(builder).await;
        }

        #[tokio::test]
        async fn anon_lifetime_in_return_type() {
            struct Dummy;

            #[builder(derive(IntoFuture(Box)))]
            async fn sut(x1: &Dummy) -> &Dummy {
                ready(x1).await
            }

            // Store the dummy struct in local variables to make sure no `'static`
            // lifetime promotion happens
            let local_x1 = Dummy;

            let builder = sut().x1(&local_x1);

            let &Dummy = assert_send(builder).await;
        }
    }

    mod test_method {
        use super::*;

        #[tokio::test]
        async fn basic() {
            struct Calculator;

            #[bon]
            impl Calculator {
                #[builder]
                #[builder(derive(IntoFuture(Box)))]
                async fn multiply(a: u32, b: u32) -> u32 {
                    ready(a * b).await
                }
            }

            // Test using IntoFuture on impl method.
            let builder = Calculator::multiply().a(6).b(7);
            assert_eq!(assert_send(builder).await, 42);
        }

        #[tokio::test]
        async fn non_send() {
            struct Sut;

            #[bon]
            impl Sut {
                #[builder(derive(IntoFuture(Box, ?Send)))]
                #[expect(clippy::future_not_send)]
                async fn sut(self, value: u32) -> u32 {
                    non_send_future().await;

                    // This future can be !Send.
                    ready(value * 2).await
                }
            }

            // Test with non-Send future.
            let result = Sut.sut().value(21).await;
            assert_eq!(result, 42);
        }

        #[tokio::test]
        async fn references_in_params() {
            struct Dummy;

            #[bon]
            impl Dummy {
                #[builder(derive(IntoFuture(Box)))]
                async fn sut<'named1, 'named2>(
                    &self,
                    _x1: &Self,
                    _x2: &Self,
                    x3: &'named1 Self,
                    x4: &'named2 Self,
                ) -> &'named2 Self {
                    let _: &'named1 Self = x3;
                    ready(x4).await
                }
            }

            // Store the dummy struct in local variables to make sure no `'static`
            // lifetime promotion happens
            let local_self = Dummy;
            let local_x1 = Dummy;
            let local_x2 = Dummy;
            let local_x3 = Dummy;
            let local_x4 = Dummy;

            let builder = local_self
                .sut()
                .x1(&local_x1)
                .x2(&local_x2)
                .x3(&local_x3)
                .x4(&local_x4);

            let _: &Dummy = assert_send(builder).await;
        }

        #[tokio::test]
        async fn anon_lifetime_in_return_type() {
            struct Dummy;

            #[bon]
            impl Dummy {
                #[builder(derive(IntoFuture(Box)))]
                async fn sut(&self, _x1: &Self) -> &Self {
                    ready(self).await
                }
            }

            // Store the dummy struct in local variables to make sure no `'static`
            // lifetime promotion happens
            let local_self = Dummy;
            let local_x1 = Dummy;

            let builder = local_self.sut().x1(&local_x1);

            let _: &Dummy = assert_send(builder).await;
        }
    }
}