---
# System prepended metadata

title: 'Draft RFC: Derive SmartPointer'
tags: [T-lang]

---

---
title: "Draft RFC: Derive SmartPointer"
tags: ["T-lang"]
date: 2024-03-07
url: https://hackmd.io/IhchhYGgTGS43BoLe8xvxA
---

# Summary

In this RFC, we add a new `derive(SmartPointer)` macro to the standard library.  This macro can be applied to a type definition, causing that type to implement the unstable traits [`DispatchFromDyn`] and [`Receiver`].  This allows the smart pointer type to be used as the `self` type in method definitions.  These methods can be called with either static or dynamic dispatch.

TODO: Are we just implementing `DispatchFromDyn` or also `Receiver` here?

[`DispatchFromDyn`]: https://doc.rust-lang.org/core/ops/trait.DispatchFromDyn.html
[`Receiver`]: https://github.com/adetaylor/rfcs/blob/arbitrary-self-types-v2/text/3519-arbitrary-self-types-v2.md

# Background / Motivation

TODO

# Mechanics

Using this macro would look like this:

```rust
#[derive(SmartPointer)]
struct Foo<#[pointee] T> {
    ptr: *const T,
}
```

Or, equivalently:

```rust
#[derive(SmartPointer)]
struct Foo<T>
where
    #[pointee] T:,
{
    ptr: *const T,
}
```


This would expand into something along the lines of:

```rust
struct Foo<T> {
    ptr: *const T,
}

// These impls can *only* be written on stable using this macro.
impl<T> DyspatchFromDyn for Foo<T> {}
// TODO: Are we also implementing `Receiver`?
impl<T> Receiver for Foo<T> {
    type Target = T;
}
```

The macro would enforce these constraints on the type to which it is applied:

- TODO
