# \`Aligned\` trait?

**URL:** https://internals.rust-lang.org/t/aligned-trait/17443
**Category:** language design
**Created:** [September 23, 2022, 8:29pm UTC](https://internals.rust-lang.org/t/aligned-trait/17443 "2022-09-23T20:29:06Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)
#### Post date: [September 23, 2022, 8:29pm UTC](https://internals.rust-lang.org/t/aligned-trait/17443/1 "2022-09-23T20:29:06Z")

</div>

[`core::mem::align_of`](https://doc.rust-lang.org/core/mem/fn.align_of.html) requires `Self: Sized`, even though it only needs to know the type's alignment. Notably, slices have an alignment known at compile time, even though they are not `Sized`. Should Rust have an `Aligned` trait (as a supertrait of `Sized`), so this bound can be loosened?

---

<div class="post-metadata">

### Author: ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)
#### Post date: [September 23, 2022, 8:59pm UTC](https://internals.rust-lang.org/t/aligned-trait/17443/2 "2022-09-23T20:59:10Z")

</div>

It makes sense to me, but do you have a practical example where you would have used this?

---

<div class="post-metadata">

### Author: ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)
#### Post date: [September 23, 2022, 9:05pm UTC](https://internals.rust-lang.org/t/aligned-trait/17443/3 "2022-09-23T21:05:15Z")

</div>

I'm trying to write a `Vec`-like type that can store unsized values (slices) as elements. I would prefer for my `Vec` to start out with an aligned dangling pointer, so that I don't need to change said pointer until I necessary. It's a minor annoyance, which I can easily work around, but still nice to have.

---

<div class="post-metadata">

### Author: ![afetisov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/afetisov/32/8508_2.png) [@afetisov](https://internals.rust-lang.org/u/afetisov)
#### Post date: [September 23, 2022, 9:38pm UTC](https://internals.rust-lang.org/t/aligned-trait/17443/4 "2022-09-23T21:38:15Z")

</div>

Is there an example of `!Aligned` type, other than extern types?

---

<div class="post-metadata">

### Author: ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)
#### Post date: [September 23, 2022, 9:38pm UTC](https://internals.rust-lang.org/t/aligned-trait/17443/5 "2022-09-23T21:38:35Z")

</div>

`dyn Trait`

---

<div class="post-metadata">

### Author: ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)
#### Post date: [September 23, 2022, 9:44pm UTC](https://internals.rust-lang.org/t/aligned-trait/17443/6 "2022-09-23T21:44:58Z")

</div>

> [@Jules-Bertholet](#):
>
> I'm trying to write a `Vec`-like type that can store unsized values (slices) as elements. I would prefer for my `Vec` to start out with an aligned dangling pointer, so that I don't need to change said pointer until I necessary.

So if it's essentially `Vec<[T]>`, can't you use `align_of::<T>()` for that pointer? You could also initialize that pointing to `[T; 0]`, not even dangling.

---

<div class="post-metadata">

### Author: ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)
#### Post date: [September 23, 2022, 9:46pm UTC](https://internals.rust-lang.org/t/aligned-trait/17443/7 "2022-09-23T21:46:48Z")

</div>

> [@cuviper](#):
>
> can't you use `align_of::<T>()` for that pointer

I could, hence why I stated it's "easy to work around". Though this becomes less fun if you also want to support structs with a slice as their last field.

---

<div class="post-metadata">

### Author: ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)
#### Post date: [September 23, 2022, 9:55pm UTC](https://internals.rust-lang.org/t/aligned-trait/17443/8 "2022-09-23T21:55:03Z")

</div>

Yeah, custom slice-based DSTs are more interesting.

Language-wise, `?Sized` constraints are special, but I suppose that would also have to imply `?Aligned` for backward compatibility. And vice versa, `?Aligned` must imply `?Sized` for the supertrait. You would explicitly combine `T: Aligned + ?Sized` for dealing with the middle ground.

---

<div class="post-metadata">

### Author: ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)
#### Post date: [September 24, 2022, 4:41pm UTC](https://internals.rust-lang.org/t/aligned-trait/17443/9 "2022-09-24T16:41:43Z")

</div>

[RFC posted](https://github.com/rust-lang/rfcs/pull/3319)

---

<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: [December 23, 2022, 4:42pm UTC](https://internals.rust-lang.org/t/aligned-trait/17443/10 "2022-12-23T16:42:13Z")

</div>

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