How can I create an inline array inside a struct?

19 views
Skip to first unread message

Jens Alfke

unread,
Jun 7, 2015, 7:16:00 PM6/7/15
to swift-l...@googlegroups.com
I want to define a struct that contains a fixed-size blob of data — for example, a SHA-1 digest, which is 20 bytes long. Is there any way to define this in Swift such that the data lives in the struct, instead of being a separate heap block?

In other words, I want the Swift equivalent of
typedef struct {
char bytes[20];
} SHA1Digest;

I couldn’t find anything in either official Swift book about working with C arrays. The closest thing is the section “Pointers” in Using Swift With Cocoa, but it only seems to talk about calling other Swift code.

As far as I can tell, to get a block of n bytes (where n is known at compile time) I have to use an Array. Which implies creating a separate internal object to store the array contents. This isn’t very inefficient if I only have a couple of these, but if I’ve got millions of them, or need to process them at a very high rate (i.e. in some numeric or crypto code) it can be a big bottleneck — this is the sort of reason why people fall back to C or C++ when doing computation-intensive code.

Go can handle this just fine (a fixed-size array, as opposed to a slice, needn’t be heap-allocated, and is copied by value) and I was imagining Swift could too, until I tried to create such a thing today and got stuck.

—Jens

Brent Royal-Gordon

unread,
Jun 15, 2015, 7:13:31 PM6/15/15
to Jens Alfke, swift-l...@googlegroups.com
A tuple is fixed-size and usually inline. (And, in fact, that's how that struct would be translated into Swift!) You can't index into a tuple with an integer, of course, but if you're willing to write a giant switch statement or something to do that, that might do the trick.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/FE6014F6-BFEA-49A7-AC39-4F5101E3F369%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.

Jens Alfke

unread,
Jun 15, 2015, 7:38:45 PM6/15/15
to Brent Royal-Gordon, swift-l...@googlegroups.com

On Jun 15, 2015, at 4:13 PM, Brent Royal-Gordon <br...@brentdax.com> wrote:

A tuple is fixed-size and usually inline. (And, in fact, that's how that struct would be translated into Swift!) You can't index into a tuple with an integer, of course, but if you're willing to write a giant switch statement or something to do that, that might do the trick.

Indexing doesn’t matter; these values are basically opaque blobs, they just need to be handed to/from the underlying APIs like CC_SHA1( ). So the question is, can I in Swift get the address of such a tuple as an UnsafeMutablePointer?

—Jens

Brent Royal-Gordon

unread,
Jun 17, 2015, 12:03:41 AM6/17/15
to Jens Alfke, swift-l...@googlegroups.com
Playgrounds are acting up for me right now, but you can pass a tuple to withUnsafeMutablePointer...
Reply all
Reply to author
Forward
0 new messages