# Nested DSTs and multiple DST fields

**URL:** <https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798>\
**Category:** language design\
**Created:** [December 7, 2025, 11:00am UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798 "2025-12-07T11:00:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![a\_cool\_nickname](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/a_cool_nickname/32/13219_2.png) [@a\_cool\_nickname](https://internals.rust-lang.org/u/a_cool_nickname)\
**Post date:** [December 7, 2025, 11:00am UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798/1 "2025-12-07T11:00:41Z")

</div>

Wouldn't it be neat to be able to do

```rust
struct StructOfArrays {
    xs: [f32],
    ys: [f32]
}

```

?

That is, allow a struct to contain any number of DST fields so long as they are at the end of the definition. In this case, the lengths of both fields comprise the metadata of the pointer in the form of `(usize, usize)`. The offset of the second slice may be calculated at runtime using the length of the first and the elements' alignment.

Another desirable feature is slices of DSTs. For this to work, all elements should possess the same metadata. For example, one could coerce a `&[Foo] where Foo: Trait` to a `&[dyn Trait]`. This way, instead of storing many `Box<dyn Trait>` pointing to the same type, one could allocate a bunch and coerce the whole slice to one of trait objects.

Another usage would be to create nested slices (aka tensors). A matrix, for instance, would be `[[T]]` where the lengths of the 'rows' are all the same and are contained in the metadata of the pointer - `(nrows: usize, row_len: usize)`.

In general, the metadata of a pointer to a slice of DSTs `[T] where T: ?Sized` is `(usize, <T as Pointee>::Metadata)`. For regular slices it's `(usize, ())` which has the same represantation as `usize`

---

<div class="post-metadata">

**Author:** ![Vorpal](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@Vorpal](https://internals.rust-lang.org/u/Vorpal)\
**Post date:** [December 7, 2025, 1:47pm UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798/2 "2025-12-07T13:47:36Z")

</div>

My understanding is that this would require significant major changes in the compiler (from what I have heard, I'm not a compiler developer).

However you could probably make a crate with proc-macros to synthesize something like this (using unsafe under the hood and various wrapper structs), if you accept having to use accessor methods rather than direct field access. I could see this being useful for zero copy parsing of network protocols and file formats with multiple dynamically sized fields.

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [December 7, 2025, 4:28pm UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798/3 "2025-12-07T16:28:18Z")

</div>

> [@a\_cool\_nickname](#):
>
> so long as they are at the end of the definition

I wonder why impose this restriction. Currently it's needed (because it allows the offset of all fields to be known statically, but if you allow two or more DSTs the it's already not true anymore.

---

<div class="post-metadata">

**Author:** ![Ddystopia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ddystopia/32/10915_2.png) [@Ddystopia](https://internals.rust-lang.org/u/Ddystopia)\
**Post date:** [December 7, 2025, 9:24pm UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798/4 "2025-12-07T21:24:56Z")

</div>

It should be possible when custom pointer metadata is allowed, as currently it is fixed at a word.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [December 7, 2025, 11:35pm UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798/5 "2025-12-07T23:35:49Z")

</div>

> [@\[Pre-RFC\] Thin pointers with inlined metadata](https://internals.rust-lang.org/t/pre-rfc-thin-pointers-with-inlined-metadata/23796):
>
> Previous discussions: [Idea: `Thin` wrapper of DSTs](https://internals.rust-lang.org/t/idea-thin-wrapper-of-dsts/23741)Summary This RFC adds Thin\<T\> that wraps T's metadata inline, which makes Thin\<T\> thin even if T is !Sized. Motivation Pointers of dynamically sized types (DSTs) are fat pointers, and they are not FFI-compatible, which obstables some common types like &str, &[T], and &dyn Trait from being passed across the FFI boundaries. 1. Passing pointers of DSTs accross FFI-boundary is hard Currently, it's difficult to using DSTs in FFI-compatible functi…

---

<div class="post-metadata">

**Author:** ![frank-king](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/frank-king/32/8765_2.png) [@frank-king](https://internals.rust-lang.org/u/frank-king)\
**Post date:** [December 8, 2025, 1:33am UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798/6 "2025-12-08T01:33:00Z")

</div>

Oh, `Thin` works fine for this situation, with some little adjustments

```rust
struct StructOfArrays<Xs = Thin<[f32]>, Ys = Thin<[f32]>> {
    xs: Xs,
    ys: Ys,
}

```

In addition, we should add these stuffs related to `ValueSized` (as proposed in the pre-RFC):

- `...Ts: ValueSized` implies `X<...Ts>: ValueSized`, where `...Ts: ValueSized` means for each `T` in `Ts`, there is `T: ValueSized`
- `...Ts: Sized + Unsize<...Us>, ...Us: ValueSized` implies `X<...Ts>: Unsize<X<...Us>>`, where `...Ts: Unsize<...Us>` means for each (`T`, `U`) in (`...Ts`, `...Us`), there is `T: Unsize<U>`.

Then, `StructOfArrays` can be constructed (on stack) in the folllewing steps:

```rust
// 1. create the sized value with metadata
let foo: StructOfArrays<
    Thin<[f32; 3], [f32]>,
    Thin<[f32; 3], [f32]>,
> = StructOfArrays {
    xs: Thin::new_unsized([1.0, 2.0, 3.0]),
    ys: Thin::new_unsizsd([4.0, 5.0, 6.0]),
};
// 2. create the reference to the sized value
let foo: &StructOfArray<
    Thin<[f32; 3], [f32]>,
    Thin<[f32; 3], [f32]>,
> = &foo;
// 3. coerce to the unsized reference
// (given that `Thin<[f32; 3], [f32]>: Unsize<Thin<[f32]>>`)
let foo: &StructOfArray = foo;

```

---

<div class="post-metadata">

**Author:** ![a\_cool\_nickname](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/a_cool_nickname/32/13219_2.png) [@a\_cool\_nickname](https://internals.rust-lang.org/u/a_cool_nickname)\
**Post date:** [December 8, 2025, 7:21am UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798/7 "2025-12-08T07:21:11Z")

</div>

The offsets of the sized fields should indeed be known statically, even if there are multiple DSTs. The offset of nything after a DST field must be computed at runtime, so for performance reasons the DST fields should be at the very end

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [December 8, 2025, 1:22pm UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798/8 "2025-12-08T13:22:01Z")

</div>

If you’re accepting that calculating the start of the second and later DST fields is going to require runtime computation, why not allow a similar cost for sized fields? I see no reason to limit it if this were supported.

---

<div class="post-metadata">

**Author:** ![a\_cool\_nickname](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/a_cool_nickname/32/13219_2.png) [@a\_cool\_nickname](https://internals.rust-lang.org/u/a_cool_nickname)\
**Post date:** [December 8, 2025, 1:40pm UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798/9 "2025-12-08T13:40:03Z")

</div>

For dynamically sized fields it's _impossible_ to get the offset at compile time. With sized ones - it's trivial. Why pay the cost if you don't have to? What would be the benefit of putting statically sized fields after dynamically sized ones?

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [December 8, 2025, 1:57pm UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798/10 "2025-12-08T13:57:36Z")

</div>

> [@a\_cool\_nickname](#):
>
> For dynamically sized fields it's _impossible_ to get the offset at compile time

For the current limitation of one DST it’s not, it’s got a fixed offset just like all other fields. Supporting multiple DST is mostly about adding this feature of dynamically defined field offsets, once that’s supported I don’t see why it should be limited to when the target field is also a DST.

---

<div class="post-metadata">

**Author:** ![a\_cool\_nickname](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/a_cool_nickname/32/13219_2.png) [@a\_cool\_nickname](https://internals.rust-lang.org/u/a_cool_nickname)\
**Post date:** [December 8, 2025, 2:23pm UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798/11 "2025-12-08T14:23:41Z")

</div>

My bad, you're right - the offset of a single DST field is known at compile time.

What I'm asking is: why allow this in the first place? What are the benefits to making a field's offset statically unknown when it can be known?

---

<div class="post-metadata">

**Author:** ![quaternic](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quaternic/32/10440_2.png) [@quaternic](https://internals.rust-lang.org/u/quaternic)\
**Post date:** [December 8, 2025, 6:24pm UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798/12 "2025-12-08T18:24:34Z")

</div>

If you allow `(Dst, Dst)`, then it should also be possible to have `(Dst, (u8, Dst))`, which has the `u8` at a dynamic offset in the outer tuple. It would be surprising to forbid `(Dst, u8, Dst)` then.

---

<div class="post-metadata">

**Author:** ![a\_cool\_nickname](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/a_cool_nickname/32/13219_2.png) [@a\_cool\_nickname](https://internals.rust-lang.org/u/a_cool_nickname)\
**Post date:** [December 8, 2025, 6:41pm UTC](https://internals.rust-lang.org/t/nested-dsts-and-multiple-dst-fields/23798/13 "2025-12-08T18:41:33Z")

</div>

You know what - I'm convinced. I still think the default layout emitted by the compiler should prevent this from happening as much as possible
