# How do I store a struct that owns an \`impl Trait\` in another struct?

**URL:** <https://users.rust-lang.org/t/how-do-i-store-a-struct-that-owns-an-impl-trait-in-another-struct/51580>\
**Category:** help\
**Created:** [November 17, 2020, 6:18pm UTC](https://users.rust-lang.org/t/how-do-i-store-a-struct-that-owns-an-impl-trait-in-another-struct/51580 "2020-11-17T18:18:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pYtoner](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/pytoner/32/17987_2.png) [@pYtoner](https://users.rust-lang.org/u/pYtoner)\
**Post date:** [November 17, 2020, 6:18pm UTC](https://users.rust-lang.org/t/how-do-i-store-a-struct-that-owns-an-impl-trait-in-another-struct/51580/1 "2020-11-17T18:18:40Z")

</div>

I am using a crate which has a struct (lets call it `ChosenContainer `) that I want to use. I want to store a `ChosenContainer ` in my `OwnContainer` struct. But I am not sure what the type of the stored value should be.

I made a small code snippet which corresponds to my situation. I tried all different types in my `OwnContainer`.

- I tried `Box`ing the `ChosenContainer ` struct: `c_container: Box<ChosenContainer<dyn Chosen>>`
- I tried trait bounds `c_container: ChosenContainer<T>` where `T: Chosen`
- I tried combining them.
- Googled for a few hours.

I have no idea how to do this. Is this even possible?

```rust
// --- code from a foreign crate simplified below --- //
pub struct Thing;

pub trait Chosen {}
impl Chosen for Thing {}

pub fn get_chosen() -> impl Chosen {
    Thing
}

pub struct ChosenContainer<C: Chosen> {
    chosen: C,
}

impl<C: Chosen> ChosenContainer<C> {
    pub fn new(chosen: C) -> Self {
        ChosenContainer { chosen }
    }
}

// --- my code so we can edit stuff below --- //

// should own a ChosenContainer
struct OwnContainer {
    c_container: ChosenContainer<dyn Chosen>,
}

impl OwnContainer {
    fn new() -> Self { // I want to keep function signiture
        let c = get_chosen();
        let c_container = ChosenContainer::new(c);
        OwnContainer {
            c_container,
        }
    }
}

fn main() {
    // should be able to create one like this:
    let own_container = OwnContainer::new();
}

```

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [November 17, 2020, 7:00pm UTC](https://users.rust-lang.org/t/how-do-i-store-a-struct-that-owns-an-impl-trait-in-another-struct/51580/2 "2020-11-17T19:00:20Z")

</div>

The two road bumps here appear to be that you can't name opaque types such as the one returned from `get_chosen()`, and that `Box<dyn Trait>` doesn't implement `Trait`. You can work around the second one using a new type, assuming you're able to `impl Chosen`. Here's [a sketch based off your example on the playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e3384e48f301d18b231aa5971a1d0fa4).

---

<div class="post-metadata">

**Author:** ![pYtoner](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/pytoner/32/17987_2.png) [@pYtoner](https://users.rust-lang.org/u/pYtoner)\
**Post date:** [November 17, 2020, 7:06pm UTC](https://users.rust-lang.org/t/how-do-i-store-a-struct-that-owns-an-impl-trait-in-another-struct/51580/3 "2020-11-17T19:06:22Z")

</div>

Thanks for the quick answer. I noticed that I can store the `Chosen` in a struct like that but didn't think to use that as a wrapper. Thanks again!

---

<div class="post-metadata">

**Author:** ![2e71828](https://avatars.discourse-cdn.com/v4/letter/2/3e96dc/32.png) [@2e71828](https://users.rust-lang.org/u/2e71828)\
**Post date:** [November 17, 2020, 7:13pm UTC](https://users.rust-lang.org/t/how-do-i-store-a-struct-that-owns-an-impl-trait-in-another-struct/51580/4 "2020-11-17T19:13:07Z")

</div>

You don’t need the newtype here; you can implement `Chosen` for `Box<T>` directly:

```rust

impl<T:Chosen+?Sized> Chosen for Box<T> {
    // delegate to T
}

```

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

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [November 17, 2020, 7:16pm UTC](https://users.rust-lang.org/t/how-do-i-store-a-struct-that-owns-an-impl-trait-in-another-struct/51580/5 "2020-11-17T19:16:38Z")

</div>

I assumed the orphan rule would apply due to the notes about a foreign crate.

---

<div class="post-metadata">

**Author:** ![pYtoner](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/pytoner/32/17987_2.png) [@pYtoner](https://users.rust-lang.org/u/pYtoner)\
**Post date:** [November 17, 2020, 7:18pm UTC](https://users.rust-lang.org/t/how-do-i-store-a-struct-that-owns-an-impl-trait-in-another-struct/51580/6 "2020-11-17T19:18:19Z")

</div>

As @quinedot pointed out the compiler nicely tells me that "implementing a foreign trait is only possible if at least one of the types for which is it implemented is local"

---

<div class="post-metadata">

**Author:** ![2e71828](https://avatars.discourse-cdn.com/v4/letter/2/3e96dc/32.png) [@2e71828](https://users.rust-lang.org/u/2e71828)\
**Post date:** [November 17, 2020, 7:38pm UTC](https://users.rust-lang.org/t/how-do-i-store-a-struct-that-owns-an-impl-trait-in-another-struct/51580/7 "2020-11-17T19:38:40Z")

</div>

This also works, but it’s a bit weird and attaches a type parameter to `OwnContainer`:

```rust
// should own a ChosenContainer
struct OwnContainer<C:Chosen> {
    c_container: ChosenContainer<C>,
}

// `Thing` is any arbitrary type that implements `Chosen`;
// It’s only here to make type inference work nicely.
impl OwnContainer<Thing> {
    fn new() -> OwnContainer<impl Chosen> {
        // I want to keep function signiture
        let chosen = get_chosen();
        let c_container = ChosenContainer::new( chosen );
        OwnContainer { c_container }
    }
}

fn main() {
    // should be able to create one like this:
    let own_container = OwnContainer::new();
}

```

([Playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=07fcd659f7f61918051826042556fcc6))

* * *

On nightly, we have the `type_alias_impl_trait` feature which makes this a bit nicer: we can define a default inner type that lets us skip the type parameter and we can use that alias in our `impl` block:

```rust
#![feature(type_alias_impl_trait)]
type DefaultChosen = impl Chosen;

// should own a ChosenContainer
struct OwnContainer<C:Chosen = DefaultChosen> {
    c_container: ChosenContainer<C>,
}

impl OwnContainer<DefaultChosen> {
    fn new() -> Self {
        // I want to keep function signiture
        let chosen = get_chosen();
        let c_container = ChosenContainer::new( chosen );
        OwnContainer { c_container }
    }
}

fn main() {
    // should be able to create one like this:
    let own_container = OwnContainer::new();
}

```

([Playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f15a696f0408162739e9481abcf5268c))

---

<div class="post-metadata">

**Author:** ![pYtoner](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/pytoner/32/17987_2.png) [@pYtoner](https://users.rust-lang.org/u/pYtoner)\
**Post date:** [November 17, 2020, 7:42pm UTC](https://users.rust-lang.org/t/how-do-i-store-a-struct-that-owns-an-impl-trait-in-another-struct/51580/8 "2020-11-17T19:42:23Z")

</div>

Interesting for sure 🙂

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [February 15, 2021, 7:42pm UTC](https://users.rust-lang.org/t/how-do-i-store-a-struct-that-owns-an-impl-trait-in-another-struct/51580/9 "2021-02-15T19:42:34Z")

</div>

Closed on February 15, 2021, 7:42pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
