]> git.lizzy.rs Git - rust.git/blob - tests/ui/higher-rank-trait-bounds/issue-95034.rs
Rollup merge of #106670 - albertlarsan68:check-docs-in-pr-ci, r=Mark-Simulacrum
[rust.git] / tests / ui / higher-rank-trait-bounds / issue-95034.rs
1 // check-pass
2 // compile-flags: --edition=2021 --crate-type=lib
3
4 use std::{
5     future::Future,
6     marker::PhantomData,
7     pin::Pin,
8     task::{Context, Poll},
9 };
10
11 mod object {
12     use super::*;
13
14     pub trait Object<'a> {
15         type Error;
16         type Future: Future<Output = Self>;
17         fn create() -> Self::Future;
18     }
19
20     impl<'a> Object<'a> for u8 {
21         type Error = ();
22         type Future = Pin<Box<dyn Future<Output = Self>>>;
23         fn create() -> Self::Future {
24             unimplemented!()
25         }
26     }
27
28     impl<'a, E, A: Object<'a, Error = E>> Object<'a> for (A,) {
29         type Error = ();
30         type Future = CustomFut<'a, E, A>;
31         fn create() -> Self::Future {
32             unimplemented!()
33         }
34     }
35
36     pub struct CustomFut<'f, E, A: Object<'f, Error = E>> {
37         ph: PhantomData<(A::Future,)>,
38     }
39
40     impl<'f, E, A: Object<'f, Error = E>> Future for CustomFut<'f, E, A> {
41         type Output = (A,);
42         fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
43             unimplemented!()
44         }
45     }
46 }
47
48 mod async_fn {
49     use super::*;
50
51     pub trait AsyncFn {
52         type Future: Future<Output = ()>;
53         fn call(&self) -> Self::Future;
54     }
55
56     impl<F, Fut> AsyncFn for F
57     where
58         F: Fn() -> Fut,
59         Fut: Future<Output = ()>,
60     {
61         type Future = Fut;
62         fn call(&self) -> Self::Future {
63             (self)()
64         }
65     }
66 }
67
68 pub async fn test() {
69     use self::{async_fn::AsyncFn, object::Object};
70
71     async fn create<T: Object<'static>>() {
72         T::create().await;
73     }
74
75     async fn call_async_fn(inner: impl AsyncFn) {
76         inner.call().await;
77     }
78
79     call_async_fn(create::<(u8,)>).await;
80 }