[][src]Trait tide::Endpoint

pub trait Endpoint<State>: Send + Sync + 'static {
    fn call<'a>(
        &'a self,
        req: Request<State>
    ) -> Pin<Box<dyn Future<Output = Result> + Send + 'a>>; }

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"))
}

fn main() {
    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>> {
    futures::future::ready(Ok(String::from("hello")))
}

fn main() {
    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

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

Invoke the endpoint within the given context

Loading content...

Implementors

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

impl<State> Endpoint<State> for PermanentRedirect[src]

impl<State> Endpoint<State> for TemporaryRedirect[src]

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

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

Loading content...