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
impl AnyEndpoint
Sourcepub fn new(endpoint: impl Endpoint + 'static) -> Self
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);Sourcepub fn name(&self) -> &'static str
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());