String Interpolation
Swift 5 added some nice improvements to string interpolation that many people may not be aware of.
The first is the ability to control interpolation into a custom type that adopts ExpressibleByStringInterpolation
. This allows you to create types like HTML
and drives the new OSLog string formatting (eg log.debug("value \(x, . . .
Cross-process Rendering
Surfaces save us
The proper way to render across processes on Apple platforms is to use an IOSurface
.
Though the headers don't declare it,
IOSurface
andIOSurfaceRef
are toll-free bridged. If calling an API that takesIOSurfaceRef
from Swift, useunsafeBitCast(surface, to: IOSurfaceRef.self)
An IOSurface is a kernel-managed . . .
The Law
Atomics are hard
Swift 5 turns on exclusivity checking by default. This has some interesting interactions with atomics, especially when running under the Thread Sanitizer (TSAN). If you've ever seen a TSAN report on some simple Swift code that looks obviously correct then you're probably running into this issue:
// Incorrect! Do not use this!
. . .
Take Delight in Small Joys
Extensions everywhere
This is a small post about a small joy. I really enjoy how natural extensions can feel in Swift.
I consider it quite unfortunate that UnsafeMutableRawBufferPointer.baseAddress
is optional. It makes that type so ugly to use in practice. I also dislike having to specify alignment on allocation; a sensible default is Int.bitWidth / 8
on . . .
Fixed-sized Arrays
Who doesn't like a tuple with 1024 elements?
Let's say we want to statfs()
a mount point to determine which BSD device name it belongs to. In english, that means discover that /Volumes/MyDisk
comes from /dev/disk6s2
.
struct statfs fsinfo;
if (statfs(path, &fsinfo) != 0) {
//error
}
The equivalent Swift code, along with a POSIX error helper:
func . . .
SingleValueCodable
A simple exercise in leverage
The new Codable
protocol is flexible enough to allow a different encoded representation from the in-memory representation which is a nice property to have in a serialization mechanism. Today I'm going to build SingleValueCodable
to automate that work when dealing with RawRepresentable
types.
The Setup
I want to encode . . .
AMD System V ABI Reference
An ABI reference for quick lookup. If you crash or just want to step through disassembly, check the contents of the registers to see where parameters are being passed.
Arg | Register | Notes |
---|---|---|
1 | RDI |
Usually self or address to hold return value |
2 | RSI |
Usually _cmd but may . . . |