Trait summer_boot::Endpoint

source ·
pub trait Endpoint<State: Clone + Send + Sync + 'static>: Send + Sync + 'static {
    // Required method
    fn call<'life0, 'async_trait>(
        &'life0 self,
        req: Request<State>
    ) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

HTTP请求处理。

这个特效是为了 Fn 类型自动实现的,所以很少实现,由开发者提供

实际上 endpoint是用Request<State>作为参数的函数,然后将实现的类型T(泛型)返回Into<Response>

Examples

这里利用的异步函数,但是只有Nightly版本才可以使用,如果要使用的话就需要启用Nightly版本 这个例子对GET请求调用返回String

async fn hello(_req: summer_boot::Request<()>) -> summer_boot::Result<String> {
    Ok(String::from("hello"))
}

let mut app = summer_boot::new();
app.at("/hello").get(hello);

如果不使用async异步的话,例子如下:

use core::future::Future;
fn hello(_req: summer_boot::Request<()>) -> impl Future<Output = summer_boot::Result<String>> {
    async_std::future::ready(Ok(String::from("hello")))
}

let mut app = summer_boot::new();
app.at("/hello").get(hello);

summer_boot也可以使用带有Fn的endpoint,但是一般建议用async异步

Required Methods§

source

fn call<'life0, 'async_trait>( &'life0 self, req: Request<State> ) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

上下文中调用endpoint

Trait Implementations§

source§

impl<State: Clone + Send + Sync + 'static> Endpoint<State> for Box<dyn Endpoint<State>>

source§

fn call<'life0, 'async_trait>( &'life0 self, request: Request<State> ) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

上下文中调用endpoint

Implementations on Foreign Types§

source§

impl<State: Clone + Send + Sync + 'static> Endpoint<State> for Box<dyn Endpoint<State>>

source§

fn call<'life0, 'async_trait>( &'life0 self, request: Request<State> ) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Implementors§

source§

impl<State, F, Fut, Res> Endpoint<State> for F
where State: Clone + Send + Sync + 'static, F: Send + Sync + 'static + Fn(Request<State>) -> Fut, Fut: Future<Output = Result<Res>> + Send + 'static, Res: Into<Response> + 'static,