# Forward-implement traits on smart pointers / make smart pointers more transparent

**URL:** <https://internals.rust-lang.org/t/forward-implement-traits-on-smart-pointers-make-smart-pointers-more-transparent/2380>\
**Category:** libs\
**Created:** [July 20, 2015, 9:48am UTC](https://internals.rust-lang.org/t/forward-implement-traits-on-smart-pointers-make-smart-pointers-more-transparent/2380 "2015-07-20T09:48:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nwin](https://avatars.discourse-cdn.com/v4/letter/n/4da419/32.png) [@nwin](https://internals.rust-lang.org/u/nwin)\
**Post date:** [July 20, 2015, 9:48am UTC](https://internals.rust-lang.org/t/forward-implement-traits-on-smart-pointers-make-smart-pointers-more-transparent/2380/1 "2015-07-20T09:48:03Z")

</div>

Currently the support to use smart pointers in generic code is very limited because they do not forward the traits implemented by their content. Given one needs a `[T]`, one might write a generic struct like

```rust
struct Container<T, V: AsRef<[T]>> {
    inner: V, _marker: PhantomData<T>
}

```

In such a case one could use a `Vec<T>` and a `&[T]` but not a `Box<[T]>` as `Box` does not implement `AsRef`.

My proposal would be to add

```rust
impl<T: ?Sized, V: AsRef<T> + ?Sized> AsRef<T> for Box<V> {
    #[inline]
    fn as_ref(&self) -> &T {
        unsafe {self.0.get()}.as_ref()
    }
}

```

to `Box` and equivalent implementations to `Rc` and `Arc`. `Box` should also forward `AsMut`.

Is this a sensible approach or are other things planned for smart pointers (like syntax sugar)? If so, which other traits beside `AsRef` would profit from such an approach?

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [July 21, 2015, 6:16am UTC](https://internals.rust-lang.org/t/forward-implement-traits-on-smart-pointers-make-smart-pointers-more-transparent/2380/2 "2015-07-21T06:16:43Z")

</div>

for now you can use `Deref`:

```rust
use std::ops::Deref;

fn test<T: Deref<Target=[u8]>>(t: T) {
    println!("{:?}", &*t);
}

fn main() {
    test(vec![5, 6, 7].into_boxed_slice());
    test(vec![5, 6, 7]);
    let a: &[u8] = &[5, 6, 7];
    test(a);
}

```

[Playpen](http://is.gd/Ceb70r)

---

<div class="post-metadata">

**Author:** ![nwin](https://avatars.discourse-cdn.com/v4/letter/n/4da419/32.png) [@nwin](https://internals.rust-lang.org/u/nwin)\
**Post date:** [July 21, 2015, 7:09am UTC](https://internals.rust-lang.org/t/forward-implement-traits-on-smart-pointers-make-smart-pointers-more-transparent/2380/3 "2015-07-21T07:09:27Z")

</div>

Just that `Deref` is not the “right” trait. Fixed-size arrays don’t and shouldn’t implement `Deref`.

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [July 21, 2015, 9:29am UTC](https://internals.rust-lang.org/t/forward-implement-traits-on-smart-pointers-make-smart-pointers-more-transparent/2380/4 "2015-07-21T09:29:45Z")

</div>

Good point… But at the same time `AsRef` isn’t the right trait for getting a reference to the inner value of a box. That would be `Borrow` imo, which actually exists. But it doesn’t exist on fixed-size arrays.

[PlayPen](http://is.gd/ywEIcB)

---

<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:** [March 25, 2019, 8:24am UTC](https://internals.rust-lang.org/t/forward-implement-traits-on-smart-pointers-make-smart-pointers-more-transparent/2380/5 "2019-03-25T08:24:51Z")

</div>

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