     let bitfields = bf.members
     let names = Set(bitfields.map(\.name.camelCase.swiftQuoted))
     let deprecated = bitfields.lazy.filter { !names.contains($0.name.swiftName) }
-    let code = head + bitfields.map(bitfieldValueCode(bf, indent)).joined(separator: "\n") + "\n\n"
-                    + deprecated.map(bitfieldDeprecated(bf, indent)).joined(separator: "\n") + "\n}"
+    let fields = bitfields.map(bitfieldValueCode(bf, indent)).joined(separator: "\n") + "\n\n"
+                    + deprecated.map(bitfieldDeprecated(bf, indent)).joined(separator: "\n")
+    let tail = "\n}\n" +
+        "func cast<I: BinaryInteger>(_ param: I) -> \(bf.escapedName.swift) { \(bf.escapedName.swift)(rawValue: cast(param)) }\n" +
+        "func cast(_ param: \(bf.escapedName.swift)) -> UInt32 { cast(param.rawValue) }\n"
+    let code = head + fields + tail
     return code
 }
 

    let values = e.members
    let names = Set(values.map(\.name.camelCase.swiftQuoted))
    let deprecated = values.lazy.filter { !names.contains($0.name.swiftName) }
    let code = alias + "\n\n\(pub)extension \(name)\(ext) {\n" +
        values.map(vcf).joined(separator: "\n") + "\n" +
        deprecated.map(vdf).joined(separator: "\n") +
    "\n}"
    return code
}


               func cast<S: SignedInteger>(_ param: S) -> Int { Int(param) }
               func cast<U: UnsignedInteger>(_ param: Int) -> U { U(param) }
               func cast<S: SignedInteger>(_ param: Int) -> S  { S(param) }
               func cast<I: BinaryInteger>(_ param: I) -> Int32 { Int32(param) }
               func cast<I: BinaryInteger>(_ param: I) -> UInt32 { UInt32(param) }
               func cast<I: BinaryInteger>(_ param: I) -> Bool { param != 0 }
               func cast<I: BinaryInteger>(_ param: Bool) -> I { param ? 1 : 0 }

