diff --git a/src/lib.rs b/src/lib.rs index 1e2eaa0..47b6d09 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,3 @@ -#![feature(type_alias_impl_trait)] - -use std::prelude::v1::define_opaque; mod samplers; mod invoking_llms; mod db; @@ -18,7 +15,6 @@ use std::future::Future; use std::net::SocketAddr; use std::pin::Pin; use std::sync::OnceLock; -use std::task::{Context, Poll}; use tera::{Tera}; use tower_http::services::ServeDir; @@ -94,106 +90,6 @@ async fn website_authentication_with_cookie( } } -enum AuthResponseFutureState { - Redirect(Option), - Running(Fut), - Done, -} - -struct AuthResponseFuture { - state: AuthResponseFutureState, -} - -impl AuthResponseFuture { - fn redirect(response: Response) -> Self { - Self { - state: AuthResponseFutureState::Redirect(Some(response)), - } - } - - fn running(fut: Fut) -> Self { - Self { - state: AuthResponseFutureState::Running(fut), - } - } -} - -impl Future for AuthResponseFuture -where - Fut: Future, - Res: IntoResponse, -{ - type Output = Response; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = unsafe { self.get_unchecked_mut() }; - match &mut this.state { - AuthResponseFutureState::Redirect(response) => { - let response = response - .take() - .expect("AuthFuture polled after completion"); - this.state = AuthResponseFutureState::Done; - Poll::Ready(response) - } - AuthResponseFutureState::Running(fut) => { - let fut = unsafe { Pin::new_unchecked(fut) }; - match fut.poll(cx) { - Poll::Ready(res) => { - this.state = AuthResponseFutureState::Done; - Poll::Ready(res.into_response()) - } - Poll::Pending => Poll::Pending, - } - } - AuthResponseFutureState::Done => panic!("AuthFuture polled after completion"), - } - } -} - -pub enum EitherResponseOrRedirect -where - Res: IntoResponse, -{ - Redirect(Response), - Response(Res), -} - -impl IntoResponse for EitherResponseOrRedirect -where Res: IntoResponse, -{ - fn into_response(self) -> Response { - match self { - EitherResponseOrRedirect::Redirect(resp) => resp, - EitherResponseOrRedirect::Response(res) => res.into_response(), - } - } -} - -// Define a trait using RPITIT (stable since 1.75) -trait AsyncWrapper { - fn call(self) -> impl Future>; -} - -impl AsyncWrapper for F -where - F: Fn() -> Fut, - Fut: Future + 'static, -{ - fn call(self) -> impl Future> { - async move { Some(self().await) } - } -} - -// Your function now returns impl the trait (no nesting in sig) -fn wrap_async(async_fn: F) -> impl AsyncWrapper -where - F: Fn() -> Fut + 'static, - Fut: Future + 'static, - Res: 'static, -{ - async_fn -} - fn axum_handler_with_auth( handler: H,