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
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
import Foundation import Dispatch /// A threadsafe shared mutable wrapper for a `SharedValue` instance. /// /// - Warning: The shared value has reference semantics; it's up to the programmer to ensure that /// the value only used monotonically. public final class SharedMutable<SharedValue: Sendable>: @unchecked Sendable { /// The synchronization mechanism that makes `self` threadsafe. private let mutex = DispatchQueue(label: "org.hylo-lang.\(SharedValue.self)") /// The (thread-unsafe) stored instance. private var storage: SharedValue /// Creates an instance storing `toBeShared`. public init(_ toBeShared: SharedValue) { self.storage = toBeShared } /// Returns the result of thread-safely applying `f` to the wrapped instance. public func read<R>(applying f: (SharedValue) throws -> R) rethrows -> R { try mutex.sync { try f(storage) } } /// Returns the result of thread-safely applying `modification` to the wrapped instance. /// /// - Requires: `modification` does not mutate any existing state. /// - Warning: Swift silently creates mutable captures in closures! If `modification` mutates /// anything other than its local variables, you can create data races and undefined behavior. public func modify<R>(applying modification: (inout SharedValue) throws -> R) rethrows -> R { try mutex.sync { try modification(&storage) } } } /// A key used to access the coding state of encoders/decoders. private let stateKey = CodingUserInfoKey(rawValue: UUID().uuidString)! /// An object that flattens values into—or reconstitutes values from—a serialized `Encoding`, /// maintaining state across the (de)serialization of individual parts. /// /// - Note: this protocol matches Foundation's `XXXEncoder`/`XXXDecoder` types, /// which—confusingly—do not themselves conform to the `Encoder`/`Decoder` protocols. public protocol StatefulCoder { /// The complete result of serializing anything with `self`. associatedtype Encoding = Data /// The storage vehicle for state. var userInfo: [CodingUserInfoKey: any Sendable] { get set } } extension StatefulCoder { /// Injects initialState into `self` for use as mutable storage during encoding/decoding. public mutating func setState<T: Sendable>(_ initialState: T) { userInfo[stateKey] = SharedMutable(initialState) } } /// An object that flattens values into a serialized `Encoding`, maintaining state across the /// serialization of individual parts. /// /// - Note: this protocol matches Foundation's `XXXEncoder` types, which—confusingly—do not /// themselves conform to the `Encoder` protocol. public protocol StatefulEncoder: StatefulCoder { /// Returns a serialized representation of `subject`. func encode<T>(_ subject: T) throws -> Encoding where T: Encodable } extension StatefulEncoder { /// Returns a serialized representation of `subject`, using `startState` as the initial encoding /// state. public mutating func encode<T, State: Sendable>(_ value: T, startState: State) throws -> Encoding where T: Encodable { setState(startState) defer { setState(0) } // Discard state, just in case `self` persists. return try encode(value) } } /// An object that reconstitutes values from a serialized `Encoding`, maintaining state across the /// deserialization of individual parts. /// /// - Note: this protocol matches Foundation's `XXXEncoder` types, which—confusingly—do not /// themselves conform to the `Encoder` protocol. public protocol StatefulDecoder: StatefulCoder { /// Returns the `T` value that was serialized into `archive`. func decode<T>(_ type: T.Type, from archive: Encoding) throws -> T where T: Decodable } extension StatefulDecoder { /// Returns the `T` value that was serialized into `archive`, using `startState` as the initial /// decoding state. public mutating func decode<T, State: Sendable>(_ type: T.Type, from encodedT: Encoding, startState: State) throws -> T where T: Decodable { setState(startState) defer { setState(0) } // Discard state, just in case `self` persists. return try decode(type, from: encodedT) } } extension Encoder { /// Accesses the previously-injected encoding state of type `T`. /// /// - Precondition: `self` is the product of some `StatefulEncoder` `e` on which `e.setState(s)` /// has been called, where `s` has type `T`. public subscript<T>(state state: T.Type) -> T { get { (userInfo[stateKey]! as! Box<T>).wrapped } nonmutating set { (userInfo[stateKey]! as! Box<T>).wrapped = newValue } nonmutating _modify { yield &(userInfo[stateKey]! as! Box<T>).wrapped } } } extension Decoder { /// Accesses the previously-injected decoding state of type `T`. /// /// - Precondition: `self` is the product of some `StatefulDecoder` `e` on which `e.setState(s)` /// has been called, where `s` has type `T`. public subscript<T>(state state: T.Type) -> T { get { (userInfo[stateKey]! as! Box<T>).wrapped } nonmutating set { (userInfo[stateKey]! as! Box<T>).wrapped = newValue } nonmutating _modify { yield &(userInfo[stateKey]! as! Box<T>).wrapped } } } // Conformances for known types. extension JSONEncoder: StatefulEncoder {} extension JSONDecoder: StatefulDecoder {} extension PropertyListEncoder: StatefulEncoder {} extension PropertyListDecoder: StatefulDecoder {} /// A (thread-unsafe) shared mutable wrapper for a `WrappedType` instance. /// /// An immutable `Box` reference is stored in encoders/decoders as a hack to create mutability where /// it is otherwise unavailable. private final class Box<WrappedType> { /// Creates an instance containing `contents`. init(_ contents: WrappedType) { self.wrapped = contents } /// The wrapped instance. var wrapped: WrappedType }
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