-
Notifications
You must be signed in to change notification settings - Fork 634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
0.3: How can I return a Future from a trait function? #1058
Comments
The plan is that this limitation will one day be lifted, also we one day want to avoid boxing entirely and use let task_obj = TaskObj::new(PinBox::new(future)); // task_obj implements Future |
I think the I would need something like this but with a generic output type to be able to migrate our current project that now uses the What do you think about converting |
Yes, I agree. Since you opened that issue I've been working on exactly that. ^^' I think it is useful. I've It could even be possible to make the lifetime generic as well. Although, I'm not sure how yet. |
|
Yeah I don't see a problem with a generic T either, but the generic lifetime seems much trickier (because of the It can obviously be replicated into |
I tabled the lifetime thing for now. We should look into it however at some point. Edit: I'll do some experiments later Here's my PR rust-lang/rust#51944 |
I checked our project and currently we do use custom lifetimes. A simple use case that's currently working for us with the 0.2.x branch: trait T {
fn f<'a>(&'a self) -> PinBox<Future<Item = u32, Error = MyError> + 'a>;
}
struct S;
impl T for S {
#[async(boxed)]
fn f(&self) -> MyResult<u32> {
await!(/*...*/);
Ok(5)
}
} So it would be great to be able to write this with 0.3: trait T {
fn f<'a>(&'a self) -> LocalFutureObj<'a, MyResult<u32>>;
}
struct S;
impl T for S {
fn f<'a>(&'a self) -> LocalFutureObj<'a, MyResult<u32>> {
LocalFutureObj::new(PinBox::new(async {
await!(/*...*/);
Ok(5)
}))
}
} |
Make custom trait object for `Future` generic - `TaskObj` -> `FutureObj<'static, ()>` - The `impl From<...> for FutureObj<'a, T>` impls are impossible because of the type parameter `T`. The impl has to live in libstd, but `FutureObj<'a, T>` is from libcore. Therefore `Into<FutureObj<'a, T>>` was implemented instead. Edit: This didn‘t compile without warnings. I am now using non-generic Form impls. See rust-lang/futures-rs#1058 r? @cramertj Edit: Added lifetime
Now that this is merged we just need to wait for the next nightly. Currently there seem to be some troubles with the build infrastructure. https://rust-lang-nursery.github.io/rust-toolstate/ In the meantime, a version of |
With 0.1.x and 0.2.x it was possible to return
Box<Future<Item = ..., Error = ...>>
, but with the 0.3 branch it's no longer possible.This code gives the following compile error:
Returning with
impl Trait
is not allowed in trait functions, so that method does not work on any branch.The text was updated successfully, but these errors were encountered: