【发布时间】:2020-12-24 01:29:23
【问题描述】:
到目前为止,我正在尝试 Rust 和❤️。
但目前我被通用特征卡住了:)
现状:
我想实现这个特性,但我无法修改:
pub trait Handler<R, B, E> {
fn run(&mut self, event: http::Request<B>) -> Result<R, E>;
}
同一库中该特征的一个实现是:
impl<Function, R, B, E> Handler<R, B, E> for Function
where
Function: FnMut(http::Request<B>) -> Result<R, E>,
{
fn run(&mut self, event: http::Request<B>) -> Result<R, E> {
(*self)(event)
}
}
而这个实现可以如下使用:
fn handler(req: http::Request<Body>) -> Result<impl IntoResponse, MyError> {
...
}
使用IntoReponse 特征:
pub trait IntoResponse {
fn into_response(self) -> Response<Body>;
}
我想做的事:
我想为结构实现该特征,以便能够与上述类型一起使用。
我试过了:
impl Handler<impl IntoResponse, Body, MyError> for GQLHandler {
fn run(&mut self, req: http::Request<Body>) -> Result<impl IntoResponse, MyError> {
...
}
}
但这会导致错误:
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
--> handlers/gql.rs:18:14
|
18 | impl Handler<impl IntoResponse, Body, NowError> for GQLHandler {
| ^^^^^^^^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
--> handlers/gql.rs:19:59
|
19 | fn run(&mut self, req: http::Request<Body>) -> Result<impl IntoResponse, NowError> {
| ^^^^^^^^^^^^^^^^^
如果我为特定类型实现它是有原因的,例如
impl Handler<http::Response<Body>, Body, NowError> for GQLHandler {
fn run(&mut self, req: http::Request<Body>) -> Result<http::Response<Body>, NowError> {
但我想以某种方式保留impl Trait。
期待任何建议。
感谢和干杯 托马斯
编辑:
跟进@MaxV 的回答(谢谢!),遗憾的是这对我不起作用(这就是为什么我还没有接受这个回答)。
当尝试使用实现 IntoResponse 的类型返回 Ok(...) 时,我收到以下错误:
|
3 | impl<T: IntoResponse> Handler<T, Body, MyError> for GQLHandler {
| - this type parameter
4 | fn run(&mut self, req: Request<Body>) -> Result<T, MyError> {
5 | Ok(Response::<()>::new(()))
| ^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found struct `http::Response`
|
= note: expected type parameter `T`
found struct `http::Response<()>`
即使我为Response 实现了IntoResponse:
trait IntoResponse{
fn foo(&self);
}
impl IntoResponse for Response<()>
{
fn foo(&self) {}
}
我错过了什么?
【问题讨论】: