# BoxedClone

**URL:** <https://internals.rust-lang.org/t/boxedclone/14115>\
**Category:** libs\
**Created:** [February 25, 2021, 11:36am UTC](https://internals.rust-lang.org/t/boxedclone/14115 "2021-02-25T11:36:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lolgeny](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/lolgeny/32/7632_2.png) [@lolgeny](https://internals.rust-lang.org/u/lolgeny)\
**Post date:** [February 25, 2021, 11:36am UTC](https://internals.rust-lang.org/t/boxedclone/14115/1 "2021-02-25T11:36:24Z")

</div>

This may well have been done before, though I couldn't find anything. Basically, add a `BoxedClone` trait of the form

```rust
trait BoxedClone {
    fn box_clone(&self) -> Box<Self>;
}

```

It can be auto implemented, though we should get specialization sorted first

```rust
impl<T: Clone> BoxedClone for T {
    fn box_clone(&self) -> Box<Self> {
        box self.clone()
    }
}

```

The reason we need this trait is for trait objects. Currently, a trait that extends `Clone` isn't object-safe, because there's no way to return `Self` directly. Thus, many people use this trait anyway.

Actually, alternatively, this could just be a default method on `Clone` itself. Thus we don't need to wait for specialization, and it makes sense to be here.

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [February 25, 2021, 2:56pm UTC](https://internals.rust-lang.org/t/boxedclone/14115/2 "2021-02-25T14:56:21Z")

</div>

Your `BoxedClone` is also not object-safe ([playground example](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f0c004aa069c40982e9ce934c4d72c15)) because it uses `Self` in the return type position. What people usually write is a variation of your code where `Self` is replaced by the trait object of the trait that should extend `BoxedClone`. This however makes it specific to each usage.

You could use a generic parameter and bound it with `Unsize` ([playground example](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=2b95d609a65a747e565418358e3cc30f)), however you won't be able to use it as supertrait because it will create a cycle ([playground example](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=940f3f1f89abafdffe55ccc637a77918)).

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [February 25, 2021, 3:55pm UTC](https://internals.rust-lang.org/t/boxedclone/14115/3 "2021-02-25T15:55:40Z")

</div>

There's already a [crate](https://github.com/dtolnay/dyn-clone) for this.

---

<div class="post-metadata">

**Author:** ![ibraheemdev](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ibraheemdev/32/13406_2.png) [@ibraheemdev](https://internals.rust-lang.org/u/ibraheemdev)\
**Post date:** [February 25, 2021, 4:15pm UTC](https://internals.rust-lang.org/t/boxedclone/14115/4 "2021-02-25T16:15:07Z")

</div>

I think the point is not the exact signature of the trait, but rather that it should be included in `std`. Currently, many large crates just define their own `CloneMyTrait` trait, or use `dyn-clone` which has over 600k downloads. I think it's a reasonable suggestion for this to be included in the standard library.

---

<div class="post-metadata">

**Author:** ![bascule](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bascule/32/3057_2.png) [@bascule](https://internals.rust-lang.org/u/bascule)\
**Post date:** [February 26, 2021, 3:06pm UTC](https://internals.rust-lang.org/t/boxedclone/14115/5 "2021-02-26T15:06:39Z")

</div>

> [@ibraheemdev](#):
>
> I think the point is not the exact signature of the trait, but rather that it should be included in `std` .

Figuring out the exact signature of the trait is a prerequisite for putting it in `std`, and moreover why it's preferred for things which might make sense to eventually include in `std` to "bake" in third party crates which can evolve much more rapidly than `std`.

I haven't done a deep dive on `dyn-clone` but its ergonomics look like they leave a little bit to be desired, to the point there's a [`dyn-clonable`](https://github.com/kardeiz/objekt-clonable) wrapper crate which tries to simplify things using an attribute-based proc macro. And while that macro does seem to make the ergonomics a lot better, I wonder if there is a simpler solution with better ergonomics but less complexity.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [May 27, 2021, 3:07pm UTC](https://internals.rust-lang.org/t/boxedclone/14115/6 "2021-05-27T15:07:16Z")

</div>

Closed on May 27, 2021, 3:07pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
