# Where's the catch with Box\<Read + Write\>?

**URL:** <https://internals.rust-lang.org/t/wheres-the-catch-with-box-read-write/6617>\
**Category:** language design\
**Created:** [January 20, 2018, 12:04pm UTC](https://internals.rust-lang.org/t/wheres-the-catch-with-box-read-write/6617 "2018-01-20T12:04:11Z")\
**Posts on this page:** 1\
**Showing post:** 16

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [January 21, 2018, 12:21pm UTC](https://internals.rust-lang.org/t/wheres-the-catch-with-box-read-write/6617/16 "2018-01-21T12:21:20Z")

</div>

> [@hanna-kruppe](#):
>
> Dynamically generating means run-time heap allocation, right?

Trait objects with `Box` already do heap allocation regardless, no?

> [@hanna-kruppe](#):
>
> Additionally, IIUC this approach still has the big per-object space overhead that “fat pointer with one vtable pointer per trait” has, it just makes the trait object pointer slightly cheaper to move (and trades that for other costs, like double indirection and heap allocation).

Hm, you are right. With const generics and a bit of compiler magic we probably can remove additional heap allocation and double indirection. Cost of adding additional pointers to stack allocated `TraitObject` struct is unfortunate, but it's probably will be the best solution. Although you'll pay this cost only if you use traits combinations (the more traits in your combination the fatter will be the pointer) so I think it fits under "zero-cost".

To be explicit, in this approach we'll change `TraitObject` struct to:

```rust
pub struct TraitObject<const N: usize> {
    pub data: *mut (),
    pub vtables: [*mut (); N],
}

```

Which with `N=1` will be equivalent to the current `TraitObject`.

---

_[View the full topic](https://internals.rust-lang.org/t/wheres-the-catch-with-box-read-write/6617)._
