AnyEndpoint

Struct AnyEndpoint 

Source
pub struct AnyEndpoint(/* private fields */);
Expand description

Type-erased endpoint that can hold any endpoint implementation behind a trait object.

AnyEndpoint provides dynamic dispatch for endpoints, allowing you to store different endpoint types in the same collection or pass them around without knowing their concrete types at compile time. This is useful for building flexible routing systems or plugin architectures.

§Performance Notes

Using AnyEndpoint involves dynamic dispatch and heap allocation, which has a small performance overhead compared to using concrete types directly. However, this is often negligible in HTTP server contexts.

§Examples

use http_kit::{Request, Response, Result, Endpoint, endpoint::AnyEndpoint};

struct HelloEndpoint;
impl Endpoint for HelloEndpoint {
    async fn respond(&self, _request: &mut Request) -> Result<Response> {
        Ok(Response::new(200, "Hello"))
    }
}

struct GoodbyeEndpoint;
impl Endpoint for GoodbyeEndpoint {
    async fn respond(&self, _request: &mut Request) -> Result<Response> {
        Ok(Response::new(200, "Goodbye"))
    }
}

// Store different endpoint types in a collection
let endpoints: Vec<AnyEndpoint> = vec![
    AnyEndpoint::new(HelloEndpoint),
    AnyEndpoint::new(GoodbyeEndpoint),
];

Implementations§

Source§

impl AnyEndpoint

Source

pub fn new(endpoint: impl Endpoint + 'static) -> Self

Creates a new type-erased endpoint wrapper around the given endpoint implementation.

This method takes any type that implements Endpoint and wraps it in a AnyEndpoint that can be stored alongside other endpoints of different types.

§Arguments
  • endpoint - Any endpoint implementation
§Examples
use http_kit::{Request, Response, Result, Endpoint, endpoint::AnyEndpoint};

struct MyEndpoint {
    message: String,
}

impl Endpoint for MyEndpoint {
    async fn respond(&self, _request: &mut Request) -> Result<Response> {
        Ok(Response::new(200, self.message.as_str()))
    }
}

let endpoint = MyEndpoint { message: "Hello!".to_string() };
let any_endpoint = AnyEndpoint::new(endpoint);
Source

pub fn name(&self) -> &'static str

Returns the type name of the underlying endpoint implementation.

This can be useful for debugging, logging, or introspection purposes.

§Examples
use http_kit::{Request, Response, Result, Endpoint, endpoint::AnyEndpoint};

struct MyEndpoint;
impl Endpoint for MyEndpoint {
    async fn respond(&self, _request: &mut Request) -> Result<Response> {
        Ok(Response::new(200, "OK"))
    }
}

let any_endpoint = AnyEndpoint::new(MyEndpoint);
println!("Endpoint type: {}", any_endpoint.name());

Trait Implementations§

Source§

impl Debug for AnyEndpoint

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Endpoint for AnyEndpoint

Source§

async fn respond(&mut self, request: &mut Request) -> Result<Response>

Processes an HTTP request and generates a response. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.