Removed unused bloat
This commit is contained in:
parent
00cf8bfb4a
commit
8533941047
104
src/lib.rs
104
src/lib.rs
@ -1,6 +1,3 @@
|
|||||||
#![feature(type_alias_impl_trait)]
|
|
||||||
|
|
||||||
use std::prelude::v1::define_opaque;
|
|
||||||
mod samplers;
|
mod samplers;
|
||||||
mod invoking_llms;
|
mod invoking_llms;
|
||||||
mod db;
|
mod db;
|
||||||
@ -18,7 +15,6 @@ use std::future::Future;
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::OnceLock;
|
use std::sync::OnceLock;
|
||||||
use std::task::{Context, Poll};
|
|
||||||
use tera::{Tera};
|
use tera::{Tera};
|
||||||
use tower_http::services::ServeDir;
|
use tower_http::services::ServeDir;
|
||||||
|
|
||||||
@ -94,106 +90,6 @@ async fn website_authentication_with_cookie(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum AuthResponseFutureState<Fut> {
|
|
||||||
Redirect(Option<Response>),
|
|
||||||
Running(Fut),
|
|
||||||
Done,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct AuthResponseFuture<Fut> {
|
|
||||||
state: AuthResponseFutureState<Fut>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Fut> AuthResponseFuture<Fut> {
|
|
||||||
fn redirect(response: Response) -> Self {
|
|
||||||
Self {
|
|
||||||
state: AuthResponseFutureState::Redirect(Some(response)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn running(fut: Fut) -> Self {
|
|
||||||
Self {
|
|
||||||
state: AuthResponseFutureState::Running(fut),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Fut, Res> Future for AuthResponseFuture<Fut>
|
|
||||||
where
|
|
||||||
Fut: Future<Output = Res>,
|
|
||||||
Res: IntoResponse,
|
|
||||||
{
|
|
||||||
type Output = Response;
|
|
||||||
|
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
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<Res>
|
|
||||||
where
|
|
||||||
Res: IntoResponse,
|
|
||||||
{
|
|
||||||
Redirect(Response),
|
|
||||||
Response(Res),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Res> IntoResponse for EitherResponseOrRedirect<Res>
|
|
||||||
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<Res> {
|
|
||||||
fn call(self) -> impl Future<Output = Option<Res>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Res, F, Fut> AsyncWrapper<Res> for F
|
|
||||||
where
|
|
||||||
F: Fn() -> Fut,
|
|
||||||
Fut: Future<Output = Res> + 'static,
|
|
||||||
{
|
|
||||||
fn call(self) -> impl Future<Output = Option<Res>> {
|
|
||||||
async move { Some(self().await) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Your function now returns impl the trait (no nesting in sig)
|
|
||||||
fn wrap_async<F, Fut, Res>(async_fn: F) -> impl AsyncWrapper<Res>
|
|
||||||
where
|
|
||||||
F: Fn() -> Fut + 'static,
|
|
||||||
Fut: Future<Output = Res> + 'static,
|
|
||||||
Res: 'static,
|
|
||||||
{
|
|
||||||
async_fn
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn axum_handler_with_auth<T, H, Res>(
|
fn axum_handler_with_auth<T, H, Res>(
|
||||||
handler: H,
|
handler: H,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user