Skip to content

Instantly share code, notes, and snippets.

@lukaskollmer
Created October 10, 2019 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaskollmer/20b2bef18ee8c5c6232b796cce261f8a to your computer and use it in GitHub Desktop.
Save lukaskollmer/20b2bef18ee8c5c6232b796cce261f8a to your computer and use it in GitHub Desktop.
func privateHelperFunctionThatMayOrMayNotBeSemanticallyEquivalentToTheForceUnwrapOperator<T>(_ arg: T?) -> T {
let oof = { fatalError("OH MY GOD it was null all along") }
if MemoryLayout<T?>.size == MemoryLayout<T>.size { // zero abstraction optional?
if withUnsafeBytes(of: arg, { $0.allSatisfy { $0 == 0 } }) {
oof()
}
return unsafeBitCast(arg, to: T.self)
}
assert(MemoryLayout<T?>.size == 1 + MemoryLayout<T>.size, "not sure what's going on here")
if withUnsafeBytes(of: arg, { $0.load(fromByteOffset: MemoryLayout<T>.size, as: UInt8.self) != 0 }) {
oof()
}
return withUnsafeBytes(of: arg) { $0.load(as: T.self) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment