Created
October 10, 2019 07:13
-
-
Save lukaskollmer/20b2bef18ee8c5c6232b796cce261f8a to your computer and use it in GitHub Desktop.
This file contains 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
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