DSTs without fat pointers

i believe the typical way to implement (eg. for C ffi) this is with a single trailing ZST field, but this seems to violate Stacked Borrows (at least according to MIRI).

so my question is: is there a better way to implement this?

alternatively, is there a way to force a pointer to be thin, even if it points to a dynamically sized type?

You can get both (not violate Stacked Borrows and force the pointer to be thin even though it points to a DST) by using an extern type (in fact this is basically their usecase), though unfortunately they are still not stable. On stable you'll have to use a type erased raw pointer and never convert it to a reference.

Due to pointers-to-subobjects existing, that is simply not possible.

If you want to take a fat pointer/reference to a subset of a value, e.g. &some_slice[i..j] or similar, then you'll have to change the pointer metadata (e.g., the length) accordingly. But that means you'll have to create a different memory region for the new, changed metadata – which is not possible if you want the metadata to be part of the pointed allocation, because there's simply no place to put it anywhere.

the metadata is stored inline as part of the pointer.

Isn't that what I am saying? This has to be the case (ie., pointers to DSTs are fat pointers) because the alternative (metadata in the allocation) doesn't work.

i think your mistake is assuming i'm asking for a way to do this in safe rust? i fully expect to have to do a decent number of pointer casts.

or do you think i want to change the built-in DSTs to not use fat pointers?

in any case, extern types are enough for my usecase, and i don't mind the requirement on nightly.

Great. Btw I'm not saying anything about requiring or not requiring unsafe; I'm talking purely about the (im)possibility of it.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.