File: resilient_enum.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (235 lines) | stat: -rw-r--r-- 5,002 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import resilient_struct

// Fixed-layout enum with resilient members
@frozen public enum SimpleShape {
  case KleinBottle
  case Triangle(Size)
}

// Fixed-layout enum with resilient members
@frozen public enum Shape {
  case Point
  case Rect(Size)
  case RoundedRect(Size, Size)
}

// Fixed-layout enum with indirect resilient members
@frozen public enum FunnyShape {
  indirect case Parallelogram(Size)
  indirect case Trapezoid(Size)
}

@frozen public enum FullyFixedLayout {
  case noPayload
  case hasPayload(Int)
}

// The enum payload has fixed layout inside this module, but
// resilient layout outside. Make sure we emit the payload
// size in the metadata.

public struct Color {
  public let r: Int, g: Int, b: Int

  public init(r: Int, g: Int, b: Int) {
    self.r = r
    self.g = g
    self.b = b
  }
}

@frozen public enum CustomColor {
  case Black
  case White
  case Custom(Color)
  case Bespoke(Color, Color)
}

// Resilient enum
public enum Medium {
  // Indirect case
  indirect case Pamphlet(Medium)  // -1

  // Case with resilient payload
  case Postcard(Size)             // -2

  // Empty cases
  case Paper                      // 0
  case Canvas                     // 1
}

// Indirect resilient enum
public indirect enum IndirectApproach {
  case Angle(Double)              // -1
}

// Resilient enum with resilient empty payload case
public struct EmptyStruct {
  public init() {}
}

public enum ResilientEnumWithEmptyCase {
  case A                          // 0
  case B                          // 1
  case Empty(EmptyStruct)         // -1
}

public func getResilientEnumWithEmptyCase() -> [ResilientEnumWithEmptyCase] {
  return [.A, .B, .Empty(EmptyStruct())]
}

// Specific enum implementations for executable tests
public enum ResilientEmptyEnum {
  case X
}

public enum ResilientSingletonEnum {
  case X(AnyObject)
}

public enum ResilientSingletonGenericEnum<T> {
  case X(T)
}

public enum ResilientNoPayloadEnum {
  case A
  case B
  case C
}

public enum ResilientSinglePayloadEnum {
  case X(AnyObject)               // -1
  case A                          // 0
  case B                          // 1
  case C                          // 2
}

public enum ResilientSinglePayloadGenericEnum<T> {
  case X(T)                       // -1
  case A                          // 0
  case B                          // 1
  case C                          // 2
}

public class ArtClass {
  public init() {}
}

public enum ResilientMultiPayloadEnum {
  case A
  case B
  case C
  case X(Int)
  case Y(Int)
}

public func makeResilientMultiPayloadEnum(_ n: Int, i: Int)
    -> ResilientMultiPayloadEnum {
  switch i {
  case 0:
    return .A
  case 1:
    return .B
  case 2:
    return .C
  case 3:
    return .X(n)
  case 4:
    return .Y(n)
  default:
    while true {}
  }
}

public enum ResilientMultiPayloadEnumSpareBits {
  case A                          // 0
  case B                          // 1
  case C                          // 2
  case X(ArtClass)                // -1
  case Y(ArtClass)                // -2
}

public func makeResilientMultiPayloadEnumSpareBits(_ o: ArtClass, i: Int)
    -> ResilientMultiPayloadEnumSpareBits {
  switch i {
  case 0:
    return .A
  case 1:
    return .B
  case 2:
    return .C
  case 3:
    return .X(o)
  case 4:
    return .Y(o)
  default:
    while true {}
  }
}

public typealias SevenSpareBits = (Bool, Int8, Int8, Int8, Int8, Int8, Int8, Int8)

public enum ResilientMultiPayloadEnumSpareBitsAndExtraBits {
  // On 64-bit little endian, 7 spare bits at the LSB end
  case P1(SevenSpareBits)
  // On 64-bit, 8 spare bits at the MSB end and 3 at the LSB end
  case P2(ArtClass)
  case P3(ArtClass)
  case P4(ArtClass)
  case P5(ArtClass)
  case P6(ArtClass)
  case P7(ArtClass)
  case P8(ArtClass)
}

public enum ResilientMultiPayloadGenericEnum<T> {
  case A                          // 0
  case B                          // 1
  case C                          // 2
  case X(T)                       // -1
  case Y(T)                       // -2
}

public enum ResilientMultiPayloadGenericEnumFixedSize<T> {
  case A                          // 0
  case B                          // 1
  case C                          // 2
  case X(Int)                     // -1
  case Y(Int)                     // -2
}

public enum ResilientIndirectEnum {
  // 0
  case Base

  // -1
  indirect case A(ResilientIndirectEnum)

  // -2
  indirect case B(ResilientIndirectEnum, ResilientIndirectEnum)
}

public enum ResilientEnumWithUnavailableCase {
  case available

  @available(*, unavailable)
  case unavailable
}

public enum ResilientEnumWithUnavailableCaseAndPayload {
  @available(*, unavailable)
  case int(_ i: UnavailableResilientInt)

  case double(_ d: ResilientDouble)
}

extension ResilientEnumWithUnavailableCaseAndPayload {
  @_alwaysEmitIntoClient
  public var intValue: Int {
    switch self {
    case .int(let i): return i.i
    case .double(let d): return Int(d.d)
    default: return -1
    }
  }
}