Trait EndpointServiceFn
pub trait EndpointServiceFn<T, State>:
Sealed<T, State>
+ Clone
+ Send
+ Sync
+ 'static { }http and std only.Expand description
rama_core::Service implemented for functions taking extractors.
§Rejections
An extractor that fails returns its Rejection instead of calling the function.
How that rejection becomes a response depends on the return type:
- A function returning
impl IntoResponserequiresErrorResponse: From<Rejection>for every extractor argument. This conversion is provided for any rejection implementingIntoResponse. - A function returning
Result<O, E>propagates rejections throughE, so it requiresE: From<Rejection>for every extractor argument.Resultitself does not implementIntoResponse, so there is no fallback when such aFromimpl is missing.
A function that breaks either rule does not implement this trait, which the compiler
reports as an unsatisfied IntoEndpointService or IntoEndpointServiceWithState
bound rather than naming the missing impl.
When registering a Result<O, E> handler with WebService,
response conversion additionally requires O: IntoResponse and E: Into<ErrorResponse>.
Implementing IntoResponse for E provides the latter conversion, but is not
required: ErrorResponse itself also satisfies it, as used by
response::Result.
§Example
use rama_http::Response;
use rama_http::service::web::WebService;
use rama_http::service::web::extract::{Query, query::FailedToDeserializeQueryString};
use rama_http::service::web::response::IntoResponse;
#[derive(serde::Deserialize)]
struct Params {
page: u32,
}
enum AppError {
BadQuery(FailedToDeserializeQueryString),
}
// required for `Query` to be usable in a function returning `Result<_, AppError>`
impl From<FailedToDeserializeQueryString> for AppError {
fn from(rejection: FailedToDeserializeQueryString) -> Self {
Self::BadQuery(rejection)
}
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
match self {
Self::BadQuery(rejection) => rejection.into_response(),
}
}
}
async fn list(Query(params): Query<Params>) -> Result<String, AppError> {
Ok(format!("page {}", params.page))
}
let _service = WebService::default().with_get("/items", list);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".