How to project an indirect enum value? #56
Unanswered
stephencelis
asked this question in
Q&A
Replies: 1 comment 2 replies
-
@stephencelis How'd you end up going with this? I'm keen to get something working. The below works on your example but there's probably a much better way to go about it. Was hoping you could point me in the right direction. extension AnyExistentialContainer {
public mutating func projectEnumValue() -> UnsafeRawPointer {
let projectedValue = self.projectValue()
guard let enumMetadata = metadata as? EnumMetadata else { return projectedValue }
let tag = enumMetadata.enumVwt.getEnumTag(for: projectedValue)
let field = enumMetadata.descriptor.fields.records[Int(tag)]
guard field.flags.isIndirectCase else { return projectedValue }
let alignMask = UInt(metadata.vwt.flags.alignmentMask)
let heapObjSize = UInt(MemoryLayout<HeapObject>.size)
let byteOffset = (heapObjSize + alignMask) & ~alignMask
let bytePtr = withUnsafePointer(to: &self) {
$0.withMemoryRebound(to: UnsafePointer<HeapObject>.self, capacity: 1) {
$0.pointee.raw
}
}
return bytePtr + Int(byteOffset)
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
A previous discussion mentions
projectValue()
as a means of unpacking an enum case's associated values, but this doesn't seem to work for indirect/recursive enum cases.Given the following enum:
It's easy enough to extract an
Int
from thebar
case:But attempting to extract a
Foo
from thefoo
case fails (because it returns identity):Is there a way to properly extract such a case?
Beta Was this translation helpful? Give feedback.
All reactions