Trait swim::View

source ·
pub trait View: Debug + Send + Sync + 'static {
    fn get<'life0, 'async_trait>(
        &'life0 self,
        request: Request<Body>
    ) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
, { ... } fn post<'life0, 'async_trait>(
        &'life0 self,
        request: Request<Body>
    ) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
, { ... } fn put<'life0, 'async_trait>(
        &'life0 self,
        request: Request<Body>
    ) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
, { ... } fn patch<'life0, 'async_trait>(
        &'life0 self,
        request: Request<Body>
    ) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
, { ... } fn delete<'life0, 'async_trait>(
        &'life0 self,
        request: Request<Body>
    ) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
, { ... } }
Expand description

Implement this trait to create a view.

A view is a collection of methods that are called when a request is made to a specific route.

The methods are called based on the HTTP method of the request. For example, if a request is made to a route with a GET method, the get method will be called.

If a method is not implemented, a 405 Method Not Allowed response will be returned.

Example

#[derive(Debug)]
pub struct HelloView;

#[async_trait::async_trait]
impl View for HelloView {
    async fn get(&self, request: Request<Body>) -> Result<Response<Body>> {
        Ok(Response::builder()
            .status(StatusCode::OK)
            .body(Body::from("Say hello to Swim!"))
            .unwrap())
    }
}

Provided Methods§

source

fn get<'life0, 'async_trait>(
    &'life0 self,
    request: Request<Body>
) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait, Global>>where
    'life0: 'async_trait,
    Self: 'async_trait,

Called when a request is made to a route with a GET method.

source

fn post<'life0, 'async_trait>(
    &'life0 self,
    request: Request<Body>
) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait, Global>>where
    'life0: 'async_trait,
    Self: 'async_trait,

Called when a request is made to a route with a POST method.

source

fn put<'life0, 'async_trait>(
    &'life0 self,
    request: Request<Body>
) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait, Global>>where
    'life0: 'async_trait,
    Self: 'async_trait,

Called when a request is made to a route with a PUT method.

source

fn patch<'life0, 'async_trait>(
    &'life0 self,
    request: Request<Body>
) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait, Global>>where
    'life0: 'async_trait,
    Self: 'async_trait,

Called when a request is made to a route with a PATCH method.

source

fn delete<'life0, 'async_trait>(
    &'life0 self,
    request: Request<Body>
) -> Pin<Box<dyn Future<Output = Result<Response<Body>, Error>> + Send + 'async_trait, Global>>where
    'life0: 'async_trait,
    Self: 'async_trait,

Called when a request is made to a route with a DELETE method.

Implementors§