# Shall there be a trait for creating smart pointers in std?

**URL:** <https://internals.rust-lang.org/t/shall-there-be-a-trait-for-creating-smart-pointers-in-std/1880>\
**Category:** ideas (deprecated)\
**Created:** [April 14, 2015, 1:26pm UTC](https://internals.rust-lang.org/t/shall-there-be-a-trait-for-creating-smart-pointers-in-std/1880 "2015-04-14T13:26:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vi0](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/vi0/32/750_2.png) [@vi0](https://internals.rust-lang.org/u/vi0)\
**Post date:** [April 14, 2015, 1:26pm UTC](https://internals.rust-lang.org/t/shall-there-be-a-trait-for-creating-smart-pointers-in-std/1880/1 "2015-04-14T13:26:33Z")

</div>

Rc and Arc both have “new” method outside any trait. Many other smart pointers can probably also have this trait.

```rust
trait SmartPointer<T> { fn new(t : T) -> Self; }

```

Is it a good idea? How this trait should be named?

---

<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:** [April 14, 2015, 2:49pm UTC](https://internals.rust-lang.org/t/shall-there-be-a-trait-for-creating-smart-pointers-in-std/1880/2 "2015-04-14T14:49:54Z")

</div>

Have you seen the [`box`-RFC](https://github.com/rust-lang/rfcs/pull/809)? It doesn’t work for generics. But at least you can do the following:

```
let x: Rc<i32> = box 5;

```

---

<div class="post-metadata">

**Author:** ![AndyShiue](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@AndyShiue](https://internals.rust-lang.org/u/AndyShiue)\
**Post date:** [April 15, 2015, 6:20am UTC](https://internals.rust-lang.org/t/shall-there-be-a-trait-for-creating-smart-pointers-in-std/1880/3 "2015-04-15T06:20:43Z")

</div>

What you want is actually: `trait SmartPointer<T> { fn new(t : T) -> Self<T>; }` Because `Self` has kind `* -> *`, such trait cannot be declared in current Rust.

---

<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:** [April 15, 2015, 8:27am UTC](https://internals.rust-lang.org/t/shall-there-be-a-trait-for-creating-smart-pointers-in-std/1880/4 "2015-04-15T08:27:24Z")

</div>

Not sure I understand what you mean. It works just as @vi0 suggests. [PlayPen](http://is.gd/4IjH9C)

```
use std::rc::Rc;

trait SmartPointer<T> { fn new(t : T) -> Self; }

impl<T> SmartPointer<T> for Box<T> {
    fn new(t: T) -> Box<T> {
        Box::new(t)
    }
}

impl<T> SmartPointer<T> for Rc<T> {
    fn new(t: T) -> Rc<T> {
        Rc::new(t)
    }
}

fn main() {
    let x = SmartPointer::new(42);
    let y : Rc<i32> = x;
}
```

---

<div class="post-metadata">

**Author:** ![Gankra](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gankra/32/6002_2.png) [@Gankra](https://internals.rust-lang.org/u/Gankra)\
**Post date:** [April 15, 2015, 9:34pm UTC](https://internals.rust-lang.org/t/shall-there-be-a-trait-for-creating-smart-pointers-in-std/1880/5 "2015-04-15T21:34:27Z")

</div>

@ker the problem is that this trait is not particularly _useful_, because if I want to be generic over Rc and Arc, it’s usually using internal implementation details. So e.g. I want to be generic over `Rc<SecretNodeType>` and `Arc<SecretNodeType>`, and there’s no way for a user of my interface to feed in `Smaht<SecretNodeType>` short of me making it public (and that’s awful(. What I want is to accept a `Smaht<*>`, that I can monomorphize with undeclared, and indeed _multiple_ types. This is Higher Kinded Types, which is unlikely to happen for a long while.

---

<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/shall-there-be-a-trait-for-creating-smart-pointers-in-std/1880/6 "2019-03-25T08:24:16Z")

</div>

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