编译器告诉我需要增加显式生命周期限制,但我不知道应该如何实现。
这是我编写的最简代码,用于重现错误。当
但是,由于它不知道
error[E0309]: the parameter type `E` may not live long enough
--> src/main.rs:39:9
|
34 | impl<S: Into<juniper::Value>, E: Into<juniper::FieldError>> Registrable for FieldInfo<S,E>
| -- help: consider adding an explicit lifetime bound...: `E: 'a +`
...
39 | Box::pin(to_graphql((self.resolver)(executor)))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: ...so that the type `impl std::future::Future` will meet its required lifetime bounds
--> src/main.rs:39:9
|
39 | Box::pin(to_graphql((self.resolver)(executor)))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
server_1
error[E0309]: the parameter type `S` may not live long enough
--> src/main.rs:39:9
|
34 | impl<S: Into<juniper::Value>, E: Into<juniper::FieldError>> Registrable for FieldInfo<S,E>
| -- help: consider adding an explicit lifetime bound...: `S: 'a +`
...
39 | Box::pin(to_graphql((self.resolver)(executor)))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: ...so that the type `impl std::future::Future` will meet its required lifetime bounds
--> src/main.rs:39:9
|
39 | Box::pin(to_graphql((self.resolver)(executor)))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
这是我编写的最简代码,用于重现错误。当
ReturnType1
不泛型化 V
和 E
时,它能够正常工作,但我现在正在尝试让其支持多种类型:use juniper;
#[derive(Clone)]
struct Context {}
impl juniper::Context for Context {}
type ReturnType1<'a, V: Into<juniper::Value>, E: Into<juniper::FieldError>> = juniper::BoxFuture<'a, Result<V,E>>;
type InnerReturnType = juniper::ExecutionResult<juniper::DefaultScalarValue>;
type ReturnType<'a> = juniper::BoxFuture<'a, InnerReturnType>;
type Resolver<V: Into<juniper::Value>, E: Into<juniper::FieldError>> = for<'a> fn(
&'a juniper::Executor<Context, juniper::DefaultScalarValue>
) -> ReturnType1<'a, V, E>;
async fn to_graphql<'a, V: Into<juniper::Value>, E: Into<juniper::FieldError>>(f: ReturnType1<'a, V, E>) -> InnerReturnType {
f.await
.map(|scalar| scalar.into())
.map_err(|err| err.into())
}
trait Registrable: Send + Sync
{
fn resolve<'a>(self: &Self, executor: &'a juniper::Executor<Context, juniper::DefaultScalarValue>) -> ReturnType<'a>;
}
struct FieldInfo<S, E>
where S: Into<juniper::Value>,
E: Into<juniper::FieldError>
{
resolver: Resolver<S,E>
}
impl<S: Into<juniper::Value>, E: Into<juniper::FieldError>> Registrable for FieldInfo<S,E>
where S: juniper::GraphQLType<TypeInfo=()> + Send + Sync
{
fn resolve<'a>(self: &Self, executor: &'a juniper::Executor<Context, juniper::DefaultScalarValue>) -> ReturnType<'a>
{
Box::pin(to_graphql((self.resolver)(executor)))
}
}
fn main() {}
Cargo.toml:
[package]
name = "pgql"
version = "0.1.0"
authors = ["Mathieu Rochette <mathieu@texthtml.net>"]
edition = "2018"
[dependencies]
juniper = { git = "https://github.com/graphql-rust/juniper", branch = "master" }
令我困扰的是泛型类型在impl
块上声明,但问题似乎出现在其中的fn
上,因此在impl级别添加生命周期似乎不是正确的方法。
如果我尝试在fn resolve()
上添加where S: 'a, E: 'a
子句:
impl<S: Into<juniper::Value>, E: Into<juniper::FieldError>> Registrable for FieldInfo<S,E>
where S: juniper::GraphQLType<TypeInfo=()> + Send + Sync
{
fn resolve<'a>(self: &Self, executor: &'a juniper::Executor<Context, juniper::DefaultScalarValue>) -> ReturnType<'a>
where S: 'a, E: 'a {
Box::pin(to_graphql((self.resolver)(executor)))
}
}
提示方法与特性声明不匹配:
error[E0195]: lifetime parameters or bounds on method `resolve` do not match the trait declaration
--> src/main.rs:37:15
|
24 | fn resolve<'a>(self: &Self, executor: &'a juniper::Executor<Context, juniper::DefaultScalarValue>) -> ReturnType<'a>;
| ---- lifetimes in impl do not match this method in trait
...
37 | fn resolve<'a>(self: &Self, executor: &'a juniper::Executor<Context, juniper::DefaultScalarValue>) -> ReturnType<'a>
| ^^^^ lifetimes do not match method in trait
但是,由于它不知道
S
& E
,我无法在特征上添加它......您可以在此PR中看到我试图进行的更改:https://github.com/mathroc/pgql-rs/pull/1/files。
context
? - Shepmaster