Cannot construct router in trait #2906
-
SummaryI have a peculiar situation. I want to implement a semi-generic interface that abstracts some boilerplate and I'm having trouble with one of the limitations imposed by pub trait AxumPrometheusMetricsServer {
fn gather_metrics() -> Vec<prometheus::proto::MetricFamily>;
async fn telemetry_handler() -> axum::response::Response {
use axum::response::IntoResponse as _;
let mut buffer = String::new();
let metrics = Self::gather_metrics();
prometheus::TextEncoder::new()
.encode_utf8(&metrics, &mut buffer)
.map_or_else(
|e| (axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
|()| (axum::http::StatusCode::OK, buffer),
)
.into_response()
}
fn root_router() -> axum::Router {
axum::Router::new().route("/", axum::routing::get(Self::telemetry_handler))
}
} Applying Can you advise as to what I can do in this situation? Failing that, can you at least explain why I can't capture axum version0.7.5 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi! I think the problem is likely with trait implementations being allowed to override The fix here should be to change the definition of fn telemetry_handler(
) -> impl std::future::Future<Output = axum::response::Response> + Send {
async {
use axum::response::IntoResponse as _;
let mut buffer = String::new();
let metrics = Self::gather_metrics();
prometheus::TextEncoder::new()
.encode_utf8(&metrics, &mut buffer)
.map_or_else(
|e| (axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
|()| (axum::http::StatusCode::OK, buffer),
)
.into_response()
}
} |
Beta Was this translation helpful? Give feedback.
Hi!
I think the problem is likely with trait implementations being allowed to override
async fn telemetry_handler
with an async fn whoseFuture
type does not implementSend
. That error aboutSelf
is unrelated, it's just a consequence of howdebug_handler
works internally - unfortunately you can't use it in a trait impl where theasync fn
calls an associated function of the same trait (I think).The fix here should be to change the definition of
telemetry_handler
to