Respondable

Trait Respondable 

Source
pub trait Respondable {
    // Required method
    fn respond(self) -> Response;
}
Expand description

A trait for types that can be converted into an HTTP response.

The Respondable trait is intended for types that can be transformed into a valid HTTP response. Any type returned by a handler (or any function) that implements this trait can be converted into a Response<Body>. This is typically used to ensure that different types can be unified into a standard response format for HTTP APIs or services.

This method converts the implementing type into an HTTP response represented by a Response<Body>. It allows various types (e.g., structs, enums, or other custom types) to be returned from a handler and automatically converted into HTTP responses.

§Implementing Respondable

To implement the Respondable trait, you need to define how your custom type can be turned into an HTTP response. This is commonly done by converting your type into a response body (e.g., a string, JSON, or some binary data) and returning it wrapped in a Response<Body>.

§Example

use titan_http::{body::Body, Response};
use titan_core::Respondable;

// Define a type that represents a response body.
pub struct MyResponse {
    message: String,
}

// Implement `Respondable` for `MyResponse`.
impl Respondable for MyResponse {
    fn respond(self) -> Response<Body> {
        // Convert the struct into an HTTP response with the message in the body.
        Response::new(Body::from(self.message))
    }
}

// Now you can return `MyResponse` from a handler, and it will be automatically
// converted into an HTTP response.

Required Methods§

Implementations on Foreign Types§

Source§

impl Respondable for &str

Source§

impl Respondable for Infallible

Source§

impl Respondable for i8

Source§

impl Respondable for i16

Source§

impl Respondable for i32

Source§

impl Respondable for i64

Source§

impl Respondable for i128

Source§

impl Respondable for isize

Source§

impl Respondable for u8

Source§

impl Respondable for u16

Source§

impl Respondable for u32

Source§

impl Respondable for u64

Source§

impl Respondable for u128

Source§

impl Respondable for ()

Source§

impl Respondable for usize

Source§

impl Respondable for String

Source§

impl Respondable for Html

Source§

impl Respondable for Response

Source§

impl<T> Respondable for (StatusCode, T)
where T: Respondable,

Source§

impl<T, E> Respondable for Result<T, E>
where T: Respondable, E: Respondable,

Implementors§