# Pre-RFC: #\[single\_repr\] attribute for ADTs

**URL:** <https://internals.rust-lang.org/t/pre-rfc-single-repr-attribute-for-adts/4175>\
**Category:** Unsafe Code Guidelines\
**Created:** [October 8, 2016, 8:46pm UTC](https://internals.rust-lang.org/t/pre-rfc-single-repr-attribute-for-adts/4175 "2016-10-08T20:46:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![arielb1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arielb1/32/3044_2.png) [@arielb1](https://internals.rust-lang.org/u/arielb1)\
**Post date:** [October 8, 2016, 8:46pm UTC](https://internals.rust-lang.org/t/pre-rfc-single-repr-attribute-for-adts/4175/1 "2016-10-08T20:46:34Z")

</div>

## Motivation

Currently, all ADTs in Rust have `undef` padding. This means that each particular value of an ADT may be represented in many ways as a bit-pattern.

This forms a problem for 2 reasons

- The bit-pattern of an ADT can’t be used as a key in maps, because multiple such values might exist.
- The ADT can’t be used for “memcpy serialization”, because that would leak potentially-sensitive stack data.

These problems can’t easily be bypassed - because padding is _always_ treated as `undef`, merely moving an ADT rerandomizes the padding. You have to write a potentially-expensive translation function, which has to branch on the variant in the case of an enum.

## Design

Add a `#[single_repr]` attribute to ADTs.

```rust
#[single_repr]
struct Foo { .. }

#[single_repr]
enum Foo { .. }

#[single_repr]
union Foo { .. }

```

An ADT with a `#[single_repr]` has a single bit-pattern for each series of bit-patterns for fields. For `#[repr(C)]`, this is defined to be the bit-pattern with all padding 0.

Loading (that looks like a better name for my `VExpr::Use`, bikeshed) a value of that ADT with a non-canonical bitpattern is UB.

Of course, if one of the fields has multiple bit-patterns, that would be preserved. We should have a lint against that.

---

<div class="post-metadata">

**Author:** ![Ericson2314](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ericson2314/32/246_2.png) [@Ericson2314](https://internals.rust-lang.org/u/Ericson2314)\
**Post date:** [October 9, 2016, 9:41pm UTC](https://internals.rust-lang.org/t/pre-rfc-single-repr-attribute-for-adts/4175/2 "2016-10-09T21:41:59Z")

</div>

`#[no_padding]`/`#[effortless_packing]` might also be useful to assert the same property on hand-packed structs, while avoiding the cost of initializing padding to a known value.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [October 11, 2016, 6:05pm UTC](https://internals.rust-lang.org/t/pre-rfc-single-repr-attribute-for-adts/4175/3 "2016-10-11T18:05:12Z")

</div>

I don’t have an opinion on the proposal itself, but can we please call the attribute `unique_repr`? IMHO that captures its, well, uniqueness much better. 😉

---

<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:** [October 12, 2016, 4:34pm UTC](https://internals.rust-lang.org/t/pre-rfc-single-repr-attribute-for-adts/4175/4 "2016-10-12T16:34:30Z")

</div>

> [@arielb1](#):
>
> Loading (that looks like a better name for my VExpr::Use, bikeshed) a value of that ADT with a non-canonical bitpattern is UB.

Would there be a way to have a compiler generated function (intrinsic) that takes a slice, returns an `Ok(value)` iff the slice's bytes contain the correct bit patterns. This function should obviously only exist for `Copy` types.

Without such a function in the standard lib, I'd fear ppl would start writing their own without any checks.

Also, to be able to optimize (specialize) generic datastructures for `unique_repr` types, how about these types automatically implement a marker trait?

---

<div class="post-metadata">

**Author:** ![arielb1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arielb1/32/3044_2.png) [@arielb1](https://internals.rust-lang.org/u/arielb1)\
**Post date:** [October 12, 2016, 6:08pm UTC](https://internals.rust-lang.org/t/pre-rfc-single-repr-attribute-for-adts/4175/5 "2016-10-12T18:08:17Z")

</div>

> [@ker](#):
>
> Would there be a way to have a compiler generated function (intrinsic) that takes a slice, returns an `Ok(value)` iff the slice's bytes contain the correct bit patterns. This function should obviously only exist for `Copy` types.

Structures can contain things like references, so loading them out of a byte-buffer is certainly unsafe. However, an

```Rust
extern "Rust" fn verify_adt<T>(adt: *const T) -> bool;

```

that verifies an ADT is certainly a possibility, if there is sufficient demand.

---

<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:27am UTC](https://internals.rust-lang.org/t/pre-rfc-single-repr-attribute-for-adts/4175/6 "2019-03-25T08:27:06Z")

</div>

Closed on March 25, 2019, 8:27am UTC

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