Hi everybody,
I'm trying to understand this error message: only named lifetimes are allowed in `impl Trait`, but `` was found in the type
.
What I want to do is to chain two futures (using and_then
) so my fut1
function implements part of the logic.fut2
then picks up from fut1
and adds more functionalities. Everything works fine unless I pass a lifetime-bound struct in the parameters: the first function works but the second one gives the above error.
Here is the repro. This code works (boxing the futures):
extern crate tokio_core;
use tokio_core::reactor::Core;
extern crate futures;
use futures::future::*;
#[derive(Debug)]
struct MyStruct<'a> {
s: &'a str,
}
fn func<'a>(a: &MyStruct<'a>) -> Result<String, ()> {
println!("func called {:?}", a);
Ok(a.s.to_owned())
}
fn fut1<'a>(a: &MyStruct<'a>) -> Box<Future<Item = (), Error = ()>> {
Box::new(done(func(a)).and_then(|_| ok(())))
}
fn fut2<'a>(a: &MyStruct<'a>) -> Box<Future<Item = (), Error = ()>> {
Box::new(fut1(a).and_then(|_| {
println!("chained!");
ok(())
}))
}
fn main() {
let ms = MyStruct { s: "str" };
let fut1 = fut1(&ms);
let fut2 = fut2(&ms);
let mut core = Core::new().unwrap();
core.run(fut1).unwrap();
core.run(fut2).unwrap();
}
This code does not (it's just the previous code with impl Future
instead of Box<Future>
:
#![feature(conservative_impl_trait)]
extern crate tokio_core;
use tokio_core::reactor::Core;
extern crate futures;
use futures::future::*;
#[derive(Debug)]
struct MyStruct<'a> {
s: &'a str,
}
fn func<'a>(a: &MyStruct<'a>) -> Result<String, ()> {
println!("func called {:?}", a);
Ok(a.s.to_owned())
}
fn fut1<'a>(a: &MyStruct<'a>) -> impl Future<Item = (), Error = ()> {
done(func(a)).and_then(|_| ok(()))
}
fn fut2<'a>(a: &MyStruct<'a>) -> impl Future<Item = (), Error = ()> {
fut1(a).and_then(|_| {
println!("chained!");
ok(())
})
}
fn main() {
let ms = MyStruct { s: "str" };
let fut1 = fut1(&ms);
let fut2 = fut2(&ms);
let mut core = Core::new().unwrap();
core.run(fut1).unwrap();
core.run(fut2).unwrap();
}
The error given is
Compiling tst_fut v0.1.0 (file:///home/user/src/rust/tst_fut)
error[E0564]: only named lifetimes are allowed in `impl Trait`, but `` was found in the type `futures::AndThen<impl futures::Future, futures::FutureResult<(), ()>, [closure@src/main.rs:24:22: 27:6]>`
--> src/main.rs:23:34
|
23 | fn fut2<'a>(a: &MyStruct<'a>) -> impl Future<Item = (), Error = ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
How can I solve this problem?
I know I can replicate the whole code in fut2
and it will work (instead of calling the first future returning function) but I prefer not to copy-paste my code around.
Thank you in in advance,
Francesco