Is there an equivalent to Rust‘s `repr(transparent)`?

I‘ve stumbled upon @_transparent but it doesn‘t seem to work on structs.

Short answer: No.
The long answer depends on what of #[repr(transparent)] you need,
there are attributes in Swift or other guarantees Swift makes which have some of the same effects in some situations.

@_transparent is a function attribute only intended for Stdlib use,
which is described here, don't use it.

1 Like

Where could I read up on them, both attributes and guarantees? Thanks!

I don't recall where such guarantees are written, unfortunately.
Some places to look:
The swift book, which is a general overview and has a reference.
The swift evolution list, similar to Rust's RFCs.
The compiler docs folder, but that is mostly compiler internals.
Apple WWDC videos on Swift.
Various blogs from compiler engineers, etc.
Some official statements here by the core team.

The particular things I recall are:
A struct with exactly one stored property (including ZSTs), is guaranteed to be transparent when in memory.
The attribute @frozen guarantees the memory layout won't change between compiler versions,
if one uses -enable-library-evolution.

Both of these are described in SE-0260.

Terms of Service

Privacy Policy

Cookie Policy