[][src]Trait tide::Endpoint

pub trait Endpoint<State: Clone + Send + Sync + 'static>: Send + Sync + 'static {
#[must_use]    fn call<'life0, 'async_trait>(
        &'life0 self,
        req: Request<State>
    ) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
; }

An HTTP request handler.

This trait is automatically implemented for Fn types, and so is rarely implemented directly by Tide users.

In practice, endpoints are functions that take a Request<State> as an argument and return a type T that implements Into<Response>.

Examples

Endpoints are implemented as asynchronous functions that make use of language features currently only available in Rust Nightly. For this reason, we have to explicitly enable the attribute will be omitted in most of the documentation.

A simple endpoint that is invoked on a GET request and returns a String:

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

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

An endpoint with similar functionality that does not make use of the async keyword would look something like this:

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

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

Tide routes will also accept endpoints with Fn signatures of this form, but using the async keyword has better ergonomics.

Required methods

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

Invoke the endpoint within the given context

Loading content...

Implementations on Foreign Types

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

Loading content...

Implementors

impl<F, Fut, State> Endpoint<State> for SseEndpoint<F, Fut, State> where
    State: Clone + Send + Sync + 'static,
    F: Fn(Request<State>, Sender) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<()>> + Send + 'static, 
[src]

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, 
[src]

impl<State, T> Endpoint<State> for Redirect<T> where
    State: Clone + Send + Sync + 'static,
    T: AsRef<str> + Send + Sync + 'static, 
[src]

impl<State: Clone + Sync + Send + 'static, InnerState: Clone + Sync + Send + 'static> Endpoint<State> for Server<InnerState>[src]

Loading content...