[swift-evolution] Ability to pack, order and align certain types of Structs (like in C)

hitstergtd+swiftevo at gmail.com hitstergtd+swiftevo at gmail.com
Thu Apr 7 14:28:59 CDT 2016


To the community:

Neither am I a compiler/language design expert nor have I previously
written a proposal to that effect, so I apologise if I have
trivialised the matter especially with regard to other parts of Swift
or if some of it already exists. I am also aware that what I've
proposed herein maybe possible through alternate means. However, the
aim of this is to make it a first-class feature in Swift itself. For
those unfamiliar with what is being requested, please see [2], [5] and [8].

I have also filed this as a language feature request at [7].

Proposal:
* As a Swift language user, I would like the ability to specify
packing, order and alignment for certain type of Structs and their
members.
* The characteristics requested are: packing, padding, ordering and
byte-level alignment of Struct elements
* All the requested characteristics would work similarly to the way they do in C
* Ideally, this should be achieved in a way that makes it simple to
use with appropriate syntactic sugar

Potential Preconditions (for the feature):
* Only Structs with certain, basic named types such as Int, UInt, etc.?
* Allow Enums and Tuples as well, so long as only basic named types
are allowed within them?
* No other nested types unless they meet the above criteria?
* Type aliased names can be used within the Struct so long as they
meet the above preconditions?

Alternatives:
* Augment and integrate something similar to [1], which mimics the
Python struct module [6]
* Recommend interfacing with C, for any language user who requires the
aforementioned characteristics; but this route would devoid the
language of self-sufficiency from a systems programming aspect.

Ultimately even if the above is deemed totally undesirable (I hope
not), at the very least, I hope it stirs discussion about being able
to use Swift as a systems programming language and how we could
possibly move toward that direction.

Thanks for your consideration!

For reference:

[1] https://github.com/nst/BinUtils/
[2] http://stackoverflow.com/questions/24139149/how-do-i-create-a-packed-data-structure-in-swift
[3] http://stackoverflow.com/questions/13688625/how-to-declare-packed-struct-without-padding-for-llvm
[4] https://llvm.org/svn/llvm-project/cfe/branches/eh-experiment/test/Sema/struct-packed-align.c
[5] http://www.catb.org/esr/structure-packing/
[6] https://docs.python.org/3/library/struct.html
[7] https://bugs.swift.org/browse/SR-1134
[8] https://mikeash.com/pyblog/friday-qa-2014-08-01-exploring-swift-memory-layout-part-ii.html


More information about the swift-evolution mailing list