The compiler tells me to add an explicit lifetime bound, but I can’t figure out how I’m supposed to that.
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)))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is the minimal code I came up with reproducing the error. It was working when ReturnType1 was not generic over V and E but I’m now trying to make it work with multiple types:
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" }
What’s bothering me is that the generic type is declared on the impl block but the issue seems to be on the fn within, so adding a lifetime at the impl level seems the wrong way to go.
If I try to add a where S: 'a, E: 'a clause on the fn resolve():
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)))
}
}
it says the method does not match the trait declaration:
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
but I can’t add it on the trait since it does not know about S & E...
you can see the changes I’m trying to make in this PR : https://github.com/mathroc/pgql-rs/pull/1/files