Created
March 25, 2020 15:15
-
-
Save DevAndArtist/8a3fbfe3a79075a271caf8c04ee7165d to your computer and use it in GitHub Desktop.
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
import SwiftUI | |
// T.Type -> AlignmentID.Type -> Any.Type -> UInt | |
extension HorizontalAlignment { | |
enum TestH: AlignmentID { | |
static func defaultValue(in context: ViewDimensions) -> CGFloat { | |
0 | |
} | |
} | |
static let testH = HorizontalAlignment(TestH.self) | |
} | |
extension VerticalAlignment { | |
enum TestV: AlignmentID { | |
static func defaultValue(in context: ViewDimensions) -> CGFloat { | |
0 | |
} | |
} | |
static let testV = VerticalAlignment(TestV.self) | |
} | |
var testType = HorizontalAlignment.TestH.self | |
var alignmentIDType: AlignmentID.Type = testType | |
let anyType: Any.Type = alignmentIDType | |
MemoryLayout<HorizontalAlignment.TestH.Type>.size // 0 | |
MemoryLayout<AlignmentID.Type>.size // 16 | |
MemoryLayout<Any.Type>.size // 8 | |
let ob_1 = ObjectIdentifier(testType) | |
let ob_2 = ObjectIdentifier(alignmentIDType) | |
let ob_3 = ObjectIdentifier(anyType) | |
let testH = HorizontalAlignment.testH | |
dump(testH) | |
// Obtain `bits: UInt` from `AlignmentKey` type. | |
let key = Mirror(reflecting: testH).children.first!.value | |
let bits = Mirror(reflecting: key).children.first!.value as! UInt | |
print(String(bits, radix: 16)) | |
let i_1 = UInt(bitPattern: ob_1) | |
let i_2 = UInt(bitPattern: ob_2) | |
let i_3 = UInt(bitPattern: ob_3) | |
let i_4 = unsafeBitCast(anyType, to: UInt.self) | |
i_1 == i_2 && i_2 == i_3 && i_3 == i_4 // true | |
print(String(i_1, radix: 16)) | |
// UInt -> Any.Type -> AlignmentID.Type | |
let e_1 = unsafeBitCast(i_1, to: Any.Type.self) | |
let e_2 = unsafeBitCast(bits, to: Any.Type.self) | |
anyType == e_1 | |
anyType == e_2 | |
let a_1 = e_1 as! AlignmentID.Type | |
a_1 == testType // true | |
a_1 == alignmentIDType // true | |
let a_2 = e_2 as! AlignmentID.Type | |
a_2 == testType // true | |
a_2 == alignmentIDType // true | |
func extract(_ guide: HorizontalAlignment) -> AlignmentID.Type { | |
let key = Mirror(reflecting: guide).children.first!.value | |
let bits = Mirror(reflecting: key).children.first!.value as! UInt | |
let existential = unsafeBitCast(bits, to: Any.Type.self) | |
return existential as! AlignmentID.Type | |
} | |
func extract(_ guide: VerticalAlignment) -> AlignmentID.Type { | |
let key = Mirror(reflecting: guide).children.first!.value | |
var bits = Mirror(reflecting: key).children.first!.value as! UInt | |
// align the value as VerticalAlignment key is intentionally misaligned | |
bits -= 1 | |
let existential = unsafeBitCast(bits, to: Any.Type.self) | |
return existential as! AlignmentID.Type | |
} | |
print(extract(.leading)) // Leading | |
print(extract(HorizontalAlignment.center)) // Center | |
print(extract(.trailing)) // Trailing | |
print(extract(HorizontalAlignment.testH)) // TestH | |
print(extract(.top)) // Top | |
print(extract(VerticalAlignment.center)) // Center | |
print(extract(.bottom)) // Bottom | |
print(extract(VerticalAlignment.testV)) // TestV | |
print(extract(.firstTextBaseline)) // FirstTextBaseline | |
print(extract(.lastTextBaseline)) // LastTextBaseline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment