Last active
May 20, 2019 15:35
-
-
Save pofat/43accdfa2b071c6d7caa84941b4fb363 to your computer and use it in GitHub Desktop.
Existential Conatiner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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