Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Yul (Solidity IR)
Zig
Javascript
GIMPLE
Ygen
sway
swift source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
aarch64 swiftc 6.0.3
aarch64 swiftc 6.1
aarch64 swiftc 6.2
x86-64 swiftc 3.1.1
x86-64 swiftc 4.0.2
x86-64 swiftc 4.0.3
x86-64 swiftc 4.1
x86-64 swiftc 4.1.1
x86-64 swiftc 4.1.2
x86-64 swiftc 4.2
x86-64 swiftc 5.0
x86-64 swiftc 5.1
x86-64 swiftc 5.10
x86-64 swiftc 5.2
x86-64 swiftc 5.3
x86-64 swiftc 5.4
x86-64 swiftc 5.5
x86-64 swiftc 5.6
x86-64 swiftc 5.7
x86-64 swiftc 5.8
x86-64 swiftc 5.9
x86-64 swiftc 6.0.3
x86-64 swiftc 6.1
x86-64 swiftc 6.2
x86-64 swiftc devsnapshot
x86-64 swiftc nightly
Options
Source code
struct Foo: Encodable, MyEncodable { enum CodingKeys: CodingKey { case foo, bar } var foo = 10 var bar = "hello" func encode(to encoder: some MyEncoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(foo, forKey: .foo) try container.encode(bar, forKey: .bar) } } func main1() { let foo = Foo() let encoder = _Encoder() try! foo.encode(to: encoder) } main1() @usableFromInline @inline(never) func printField<T>(name: String, value: T) { print(name, value) } public protocol KeyedEncodingContainerProtocol { associatedtype Key: CodingKey mutating func encode<T: Encodable>(_ value: T, forKey key: Key) throws } public struct MyKeyedEncodingContainer<K: CodingKey> : KeyedEncodingContainerProtocol { public typealias Key = K /// The container for the concrete encoder. internal var _box: _KeyedEncodingContainerBase /// Creates a new instance with the given container. /// /// - parameter container: The container to hold. public init<Container: KeyedEncodingContainerProtocol>( _ container: Container ) where Container.Key == Key { _box = _KeyedEncodingContainerBox(container) } public mutating func encode<T: Encodable>( _ value: T, forKey key: Key ) throws { try _box.encode(value, forKey: key) } } internal class _KeyedEncodingContainerBase { internal init(){} deinit {} internal func encode<T: Encodable, K: CodingKey>(_ value: T, forKey key: K) throws { fatalError("_KeyedEncodingContainerBase cannot be used directly.") } } internal final class _KeyedEncodingContainerBox< Concrete: KeyedEncodingContainerProtocol >: _KeyedEncodingContainerBase { typealias Key = Concrete.Key internal var concrete: Concrete internal init(_ container: Concrete) { concrete = container } override internal func encode<T: Encodable, K: CodingKey>( _ value: T, forKey key: K ) throws { let key = unsafeBitCast(key, to: Key.self) try concrete.encode(value, forKey: key) } } protocol MyEncoder { func container<Key>(keyedBy type: Key.Type) -> MyKeyedEncodingContainer<Key> } protocol MyEncodable { func encode(to encoder: some MyEncoder) throws } @usableFromInline final class _Encoder: MyEncoder { @inlinable init() { } @inlinable func container<Key: CodingKey>(keyedBy _: Key.Type) -> MyKeyedEncodingContainer<Key> { MyKeyedEncodingContainer(_KeyedEncodingContainer<Key>()) } } @usableFromInline struct _KeyedEncodingContainer<Key: CodingKey>: KeyedEncodingContainerProtocol { @inlinable init( ) { } @inlinable func encode<T>(_ value: T, forKey key: Key) throws where T: Encodable { let name = key.stringValue printField(name: name, value: value) } }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree