glacier-web 0.3.0

A batteries-included web framework for Rust, inspired by Rails, Phoenix and Laravel.
Documentation
//! Request handlers.

use std::future::Future;
use std::pin::Pin;

use crate::context::Context;
use crate::response::{IntoResponse, Response};

pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send + 'static>>;

/// Something that turns a request into a response.
///
/// Implemented for any `async fn(Context) -> impl IntoResponse`, so plain
/// functions and closures can be used as route handlers.
pub trait Handler: Send + Sync + 'static {
    fn call(&self, ctx: Context) -> BoxFuture<Response>;
}

impl<F, Fut, R> Handler for F
where
    F: Fn(Context) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = R> + Send + 'static,
    R: IntoResponse,
{
    fn call(&self, ctx: Context) -> BoxFuture<Response> {
        let fut = self(ctx);
        Box::pin(async move { fut.await.into_response() })
    }
}