|
| 1 | +#[derive(Debug)] |
| 2 | +struct Error; |
| 3 | +struct SharedPool { |
| 4 | + a: PoolOptions, |
| 5 | +} |
| 6 | + |
| 7 | +struct PoolOptions; |
| 8 | + |
| 9 | +async fn check<'s: 'p, 'p>( |
| 10 | + mut _conn: Floating<'s>, |
| 11 | + _: &'p PoolOptions, |
| 12 | +) -> Result<(), DecrementSizeGuard<'s>> { |
| 13 | + todo!() |
| 14 | +} |
| 15 | + |
| 16 | +impl SharedPool { |
| 17 | + async fn acquire(&'_ self) -> Result<Floating<'_>, Error> { |
| 18 | + async { |
| 19 | + if let conn = todo!() { |
| 20 | + if let d = check(conn, &self.a).await {} |
| 21 | + } |
| 22 | + } |
| 23 | + .await; |
| 24 | + todo!() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +struct G; |
| 29 | +struct Floating<'s>(std::marker::PhantomData<dyn Fn(&'s ())>); |
| 30 | +impl<'s> Floating<'s> { |
| 31 | + fn attach(self, e: &std::sync::Arc<SharedPool>) -> G { todo!() } |
| 32 | +} |
| 33 | + |
| 34 | +struct DecrementSizeGuard<'s>(std::marker::PhantomData<dyn Fn(&'s ())>); |
| 35 | +struct FooDb; |
| 36 | +impl FooDb { |
| 37 | + fn acquire(&self) -> impl std::future::Future<Output = Result<G, Error>> + 'static { |
| 38 | + let _shared: std::sync::Arc<SharedPool> = todo!(); |
| 39 | + async move { _shared.acquire().await.map(|conn| conn.attach(&_shared)) } |
| 40 | + } |
| 41 | + |
| 42 | + async fn nested(&self) -> Result<Option<String>, ()> { |
| 43 | + (async move { |
| 44 | + match async move { |
| 45 | + async move { |
| 46 | + ( |
| 47 | + async move { |
| 48 | + match async move { |
| 49 | + let db = FooDb; |
| 50 | + let mut _conn = db.acquire().await.unwrap(); |
| 51 | + Ok::<_, ()>(String::default()) |
| 52 | + } |
| 53 | + .await |
| 54 | + { |
| 55 | + Ok(x) => Ok(x), |
| 56 | + Err0 => todo!(), |
| 57 | + } |
| 58 | + }, |
| 59 | + todo!(), |
| 60 | + ) |
| 61 | + .0 |
| 62 | + .await |
| 63 | + } |
| 64 | + .await |
| 65 | + .map(Some) |
| 66 | + } |
| 67 | + .await |
| 68 | + { |
| 69 | + Ok(x) => Ok(x), |
| 70 | + Err(e) => Err(e), |
| 71 | + } |
| 72 | + },) |
| 73 | + .0 |
| 74 | + .await |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +fn main() -> Result<(), Box<dyn std::error::Error + 'static>> { |
| 79 | + factory(Factory::handle(route)); |
| 80 | + todo!() |
| 81 | +} |
| 82 | + |
| 83 | +fn factory<X, Req>(factory: X) -> BoxServiceFactory<Req> |
| 84 | +where X: ServiceFactory<Req> + 'static { |
| 85 | + BoxServiceFactory(Box::new(FactoryWrapper(factory))) |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +async fn route(database: FooDb) -> Result<(), Error> { |
| 90 | + database.nested().await; |
| 91 | + todo!() |
| 92 | +} |
| 93 | + |
| 94 | +struct Factory<I, T, R> { |
| 95 | + _hnd: I, |
| 96 | + j: std::marker::PhantomData<(T, R)>, |
| 97 | +} |
| 98 | + |
| 99 | +impl<I, T, R> Factory<I, T, R> { |
| 100 | + fn handle(_: I) -> Self { todo!() } |
| 101 | +} |
| 102 | + |
| 103 | +struct K; |
| 104 | +struct L; |
| 105 | +impl<I, T, R> ServiceFactory<K> for Factory<I, T, R> |
| 106 | +where |
| 107 | + I: N<T, R>, |
| 108 | + R: std::future::Future, |
| 109 | + R::Output: Responder, |
| 110 | +{ |
| 111 | + type Future = std::future::Ready<Result<Self, ()>>; |
| 112 | + type Service = Self; |
| 113 | + |
| 114 | + fn new_service(&self, _: ()) -> Self::Future { todo!() } |
| 115 | +} |
| 116 | + |
| 117 | +impl<I, T, R> Service<K> for Factory<I, T, R> { |
| 118 | + type Error = Error; |
| 119 | + type Future = std::future::Ready<Result<L, Error>>; |
| 120 | + |
| 121 | + fn poll(&self, _: &mut core::task::Context) -> core::task::Poll<Result<(), Error>> { todo!() } |
| 122 | + |
| 123 | + fn call(&self, m: K) -> Self::Future { todo!() } |
| 124 | +} |
| 125 | + |
| 126 | +trait Responder {} |
| 127 | + |
| 128 | +impl<T, R> Responder for Result<T, R> {} |
| 129 | + |
| 130 | +trait N<T, R>: 'static |
| 131 | +where |
| 132 | + R: std::future::Future, |
| 133 | + R::Output: Responder, { |
| 134 | + fn call(&self, param: T) -> R; |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +type BoxFuture<T> = std::pin::Pin<Box<dyn std::future::Future<Output = T>>>; |
| 139 | + |
| 140 | +struct BoxServiceFactory<Req>( |
| 141 | + Box< |
| 142 | + dyn ServiceFactory< |
| 143 | + Req, |
| 144 | + Service = Box<dyn Service<Req, Error = (), Future = BoxFuture<Result<(), ()>>>>, |
| 145 | + Future = BoxFuture< |
| 146 | + Result<Box<dyn Service<Req, Error = (), Future = BoxFuture<Result<(), ()>>>>, ()>, |
| 147 | + >, |
| 148 | + >, |
| 149 | + >, |
| 150 | +); |
| 151 | + |
| 152 | +trait ServiceFactory<Req> { |
| 153 | + type Service: Service<Req>; |
| 154 | + type Future; |
| 155 | + fn new_service(&self, _: ()) -> Self::Future; |
| 156 | +} |
| 157 | + |
| 158 | +trait Service<Req> { |
| 159 | + type Error; |
| 160 | + type Future; |
| 161 | + fn poll(&self, ctx: &mut std::task::Context) -> std::task::Poll<Result<(), Self::Error>>; |
| 162 | + fn call(&self, req: Req) -> Self::Future; |
| 163 | +} |
| 164 | + |
| 165 | +impl<S, Req> Service<Req> for Box<S> |
| 166 | +where S: Service<Req> + ?Sized |
| 167 | +{ |
| 168 | + type Error = S::Error; |
| 169 | + type Future = S::Future; |
| 170 | + |
| 171 | + fn poll(&self, _: &mut std::task::Context) -> std::task::Poll<Result<(), S::Error>> { todo!() } |
| 172 | + |
| 173 | + fn call(&self, _: Req) -> S::Future { todo!() } |
| 174 | +} |
| 175 | + |
| 176 | +impl<SF, Req> ServiceFactory<Req> for FactoryWrapper<SF> |
| 177 | +where |
| 178 | + SF: ServiceFactory<Req>, |
| 179 | + SF: 'static, |
| 180 | +{ |
| 181 | + type Future = BoxFuture<Result<Self::Service, ()>>; |
| 182 | + type Service = Box<dyn Service<Req, Error = (), Future = BoxFuture<Result<(), ()>>>>; |
| 183 | + |
| 184 | + fn new_service(&self, _: ()) -> Self::Future { todo!() } |
| 185 | +} |
| 186 | + |
| 187 | +impl<Func, A, Res> N<(A,), Res> for Func |
| 188 | +where |
| 189 | + Func: Fn(A) -> Res + 'static, |
| 190 | + Res: std::future::Future, |
| 191 | + Res::Output: Responder, |
| 192 | +{ |
| 193 | + fn call(&self, _: (A,)) -> Res { todo!() } |
| 194 | +} |
| 195 | + |
| 196 | +struct FactoryWrapper<T>(T); |
0 commit comments