# Dynamic Futures

**URL:** <https://users.rust-lang.org/t/dynamic-futures/24664>\
**Category:** uncategorized\
**Created:** [January 27, 2019, 12:43pm UTC](https://users.rust-lang.org/t/dynamic-futures/24664 "2019-01-27T12:43:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dakom](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dakom/32/23697_2.png) [@dakom](https://users.rust-lang.org/u/dakom)\
**Post date:** [January 27, 2019, 12:43pm UTC](https://users.rust-lang.org/t/dynamic-futures/24664/1 "2019-01-27T12:43:07Z")

</div>

So - trying to get something that changes at runtime but satisfies a particular Trait... seems it's either:

1. Wrap everything in enums
2. Use Trait Objects

Fine. But now I want to get that as a Future... going the Trait Objects route, seems that I then need to _also_ have the Future be a Trait Object, which means it needs to be boxed as well? (not totally clear on why... just where I ended up with fighting the compiler)

Here's the best I could do. Any tips?

Playground: [Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=70255f15582a65140fd73f6be2c3f3ff)

```rust
use std::fmt::{Display, Formatter, Result};
use futures::*;

struct Foo {}
struct Bar {}

impl Display for Foo {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(f, "foo")
    }
}

impl Display for Bar {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(f, "bar")
    }
}

impl Foo {
    fn new() -> impl Future<Item = Box<Foo>, Error = ()> {
        future::ok(Box::new(Foo{}))
    }
}

impl Bar {
    fn new() -> impl Future<Item = Box<Bar>, Error = ()> {
        future::ok(Box::new(Bar{}))
    }
}

//Can't do this
/*
fn get_dynamic_future (name:&str) -> impl Future<Item = Box<dyn Display + Send>, Error = ()> {
    match name {
        "foo" => Foo::new().map(|v| v as Box<Display + Send>),
        "bar" => Foo::new().map(|v| v as Box<Display + Send>),
        _ => future::err(())
    }
}
*/

//Gotta do this??
fn get_dynamic_future (name:&str) -> Box<dyn Future<Item = Box<dyn Display + Send>, Error = ()> + Send> {
    match name {
        "foo" => Box::new(Foo::new().map(|v| v as Box<Display + Send>)),
        "bar" => Box::new(Bar::new().map(|v| v as Box<Display + Send>)),
        _ => Box::new(future::err(()))
    }
}

fn main() {
    
    tokio::run(get_dynamic_future("foo").map(|value| {
        println!("{}", value);
        ()
    }));

    tokio::run(get_dynamic_future("bar").map(|value| {
        println!("{}", value);
        ()
    }));

}

```

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [January 27, 2019, 2:59pm UTC](https://users.rust-lang.org/t/dynamic-futures/24664/2 "2019-01-27T14:59:07Z")

</div>

Yes, `impl Future` is still one exact type, you just don't need to type it. It's closer to type inference than an abstraction.

`Box<dyn Future>` is the way to go. `Either` may be OK too. Each future type can have a different size, so you can't return it without boxing. The caller wouldn't know how much space to reserve and how to read that type without some kind of tag… like enum.

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mbrubeck/32/648_2.png) [@mbrubeck](https://users.rust-lang.org/u/mbrubeck)\
**Post date:** [January 22, 2020, 4:58pm UTC](https://users.rust-lang.org/t/dynamic-futures/24664/3 "2020-01-22T16:58:27Z")

</div>

Closed on January 22, 2020, 4:58pm UTC


