Skip to content

Instantly share code, notes, and snippets.

@pofat
Last active May 20, 2019 15:35
Show Gist options
  • Save pofat/43accdfa2b071c6d7caa84941b4fb363 to your computer and use it in GitHub Desktop.
Save pofat/43accdfa2b071c6d7caa84941b4fb363 to your computer and use it in GitHub Desktop.
Existential Conatiner
struct ExistentialContainer {
var valueBuffer: (Int, Int, Int)
var vwt: UnsafePointer<ValueWitness>
var pwt: UnsafePointer<ProtocolWitness>
}
protocol Fooable {
func foo()
}
struct Concrete: Fooable {
let x = 99
let y = 2
func foo() {
print("foo in concrete")
}
}
// In main.swift
var concrete: Fooable = Concrete()
withUnsafePointer(to: &concrete) { pointer in
pointer.withMemoryRebound(to: ExistentialContainer.self, capacity: 1) { wpointer in
print(wpointer.pointee.valueBuffer.0) // 99
print(wpointer.pointee.valueBuffer.1) // 2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment