在实现 actix 中间件时,我应该如何理解复杂的 where 子句?
How should I understand a compilcated where clause when implementing actix middleware?
我正在查看 actix examples 以了解如何在 actix_web 中实现中间件。他们实现了这样的特征:
impl<S, B> Transform<S> for SayHi
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = SayHiMiddleware<S>;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, service: S) -> Self::Future {
ok(SayHiMiddleware { service })
}
}
我在理解这行代码时遇到了一些问题:
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>
是说 S
受 Service
特征的约束,并且在该特征中有一个名为 Request
的类型,在这种情况下 Request
实际上是一个ServiceRequest
?
如果是这样,为什么需要在块中指定 type Request = ServiceRequest;
?
Is it saying that S
is bound by the Service
trait and in that trait there is a type named Request
and in THIS case Request
is actually a ServiceRequest
?
有两个特征,Transform
和 Service
,它们都有一个名为 Request
的关联类型。这些没有关系。
If so, why is there a need to specify type Request = ServiceRequest;
in the block?
通过为 SayHi
编写 Transform
的实现,您实际上是在 创建 这种关系。您已经定义了这些关联类型是什么,用于仅在满足 where
子句中的所有条件时才有效的实现。
我正在查看 actix examples 以了解如何在 actix_web 中实现中间件。他们实现了这样的特征:
impl<S, B> Transform<S> for SayHi
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = SayHiMiddleware<S>;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, service: S) -> Self::Future {
ok(SayHiMiddleware { service })
}
}
我在理解这行代码时遇到了一些问题:
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>
是说 S
受 Service
特征的约束,并且在该特征中有一个名为 Request
的类型,在这种情况下 Request
实际上是一个ServiceRequest
?
如果是这样,为什么需要在块中指定 type Request = ServiceRequest;
?
Is it saying that
S
is bound by theService
trait and in that trait there is a type namedRequest
and in THIS caseRequest
is actually aServiceRequest
?
有两个特征,Transform
和 Service
,它们都有一个名为 Request
的关联类型。这些没有关系。
If so, why is there a need to specify
type Request = ServiceRequest;
in the block?
通过为 SayHi
编写 Transform
的实现,您实际上是在 创建 这种关系。您已经定义了这些关联类型是什么,用于仅在满足 where
子句中的所有条件时才有效的实现。