@@ -31,63 +31,58 @@ public func v2APIsAreStripped() -> Bool {
31
31
#endif // STRIP_V2_APIS
32
32
}
33
33
34
+ /// Describes types that can be appended to.
35
+ public protocol Appendable {
36
+ associatedtype Element
37
+ mutating func append( _ x: Element )
38
+ }
39
+
40
+ /// Describes types that can be counted.
41
+ public protocol Countable {
42
+ var count : Int { get }
43
+ }
44
+
34
45
public enum BadError : Error , Equatable {
35
46
/// Indicates badness
36
47
case bad
37
48
}
38
49
39
- /// A totally unnecessary wrapper for `Int` that adds mutability.
40
- public struct MutableInt {
41
- @usableFromInline
42
- internal var _value : Int
43
-
44
- public init ( _ value: Int ) { _value = value }
45
- }
46
50
47
- /// A totally unnecessary wrapper for `Int` that provides reference semantics .
48
- public class ReferenceInt {
51
+ /// A totally unnecessary array type for `Int` elements .
52
+ public struct IntArray {
49
53
@usableFromInline
50
- internal var _value : Int
54
+ internal var _values : [ Int ]
51
55
52
- public init ( _ value : Int ) { _value = value }
56
+ public init ( _ values : [ Int ] ) { _values = values }
53
57
}
54
58
55
- /// Describes types that can be incremented.
56
- public protocol Incrementable {
57
- associatedtype Operand
59
+ extension IntArray : Appendable {
60
+ public mutating func append( _ x: Int ) {
61
+ _values. append ( x)
62
+ }
63
+ }
58
64
59
- mutating func incrementByOne ( ) -> String
60
- mutating func increment ( by amount : Operand ) -> Operand
65
+ extension IntArray : Countable {
66
+ public var count : Int { _values . count }
61
67
}
62
68
63
- extension MutableInt : Incrementable {
64
- public mutating func incrementByOne( ) -> String {
65
- _value += 1
66
- return String ( _value)
67
- }
68
69
69
- public mutating func increment( by amount: Int ) -> Int {
70
- _value += amount
71
- return _value
72
- }
73
- }
70
+ /// A totally unnecessary array type for `Int` elements with reference semantics.
71
+ public class ReferenceIntArray {
72
+ @usableFromInline
73
+ internal var _values : [ Int ]
74
74
75
- extension ReferenceInt : Incrementable {
76
- public func incrementByOne( ) -> String {
77
- _value += 1
78
- return String ( _value)
79
- }
75
+ public init ( _ values: [ Int ] ) { _values = values }
76
+ }
80
77
81
- public func increment ( by amount : Int ) -> Int {
82
- _value += amount
83
- return _value
78
+ extension ReferenceIntArray : Appendable {
79
+ public func append ( _ x : Int ) {
80
+ _values . append ( x )
84
81
}
85
82
}
86
83
87
- extension Int {
88
- @usableFromInline internal func byte( at index: Int ) -> UInt8 {
89
- UInt8 ( truncatingIfNeeded: self >> ( index * 8 ) )
90
- }
84
+ extension ReferenceIntArray : Countable {
85
+ public var count : Int { _values. count }
91
86
}
92
87
93
88
// MARK: - Back deployed APIs
@@ -109,87 +104,79 @@ public func pleaseThrow(_ shouldThrow: Bool) throws -> Bool {
109
104
110
105
@available ( BackDeploy 1 . 0 , * )
111
106
@_backDeploy ( before: BackDeploy 2.0 )
112
- public func genericIncrement < T: Incrementable > (
113
- _ x : inout T ,
114
- by amount : T . Operand
115
- ) -> T . Operand {
116
- return x . increment ( by : amount )
107
+ public func genericAppend < T: Appendable > (
108
+ _ a : inout T ,
109
+ _ x : T . Element
110
+ ) {
111
+ return a . append ( x )
117
112
}
118
113
119
114
@available ( BackDeploy 1 . 0 , * )
120
115
@_backDeploy ( before: BackDeploy 2.0 )
121
- public func existentialIncrementByOne ( _ x : inout any Incrementable ) {
122
- testPrint ( handle : #dsohandle , x . incrementByOne ( ) )
116
+ public func existentialCount ( _ c : any Countable ) -> Int {
117
+ c . count
123
118
}
124
119
125
- extension MutableInt {
120
+ extension IntArray {
126
121
@available ( BackDeploy 1 . 0 , * )
127
122
@_backDeploy ( before: BackDeploy 2.0 )
128
- public var value : Int { _value }
123
+ public var values : [ Int ] { _values }
129
124
130
125
@available ( BackDeploy 1 . 0 , * )
131
126
@_backDeploy ( before: BackDeploy 2.0 )
132
127
public func print( ) {
133
- // Tests recursive @_backDeploy since `value ` is also @_backDeploy
134
- testPrint ( handle: #dsohandle, String ( value ) )
128
+ // Tests recursive @_backDeploy since `values ` is also @_backDeploy
129
+ testPrint ( handle: #dsohandle, values . description )
135
130
}
136
131
137
132
@available ( BackDeploy 1 . 0 , * )
138
133
@_backDeploy ( before: BackDeploy 2.0 )
139
- public static var zero : Self { MutableInt ( 0 ) }
140
-
141
- @available ( BackDeploy 1 . 0 , * )
142
- @_backDeploy ( before: BackDeploy 2.0 )
143
- public mutating func decrement( by amount: Int ) -> Int {
144
- _value -= amount
145
- return _value
146
- }
134
+ public static var empty : Self { IntArray ( [ ] ) }
147
135
148
136
@available ( BackDeploy 1 . 0 , * )
149
137
@_backDeploy ( before: BackDeploy 2.0 )
150
- public func toIncrementable ( ) -> any Incrementable { self }
138
+ public func toCountable ( ) -> any Countable { self }
151
139
152
140
@available ( BackDeploy 1 . 0 , * )
153
141
@_backDeploy ( before: BackDeploy 2.0 )
154
- public subscript( byteAt index: Int ) -> UInt8 { _value. byte ( at: index) }
142
+ public subscript( _ i: Int ) -> Int {
143
+ get { _values [ i] }
144
+ _modify { yield & _values[ i] }
145
+ }
155
146
}
156
147
157
- extension ReferenceInt {
148
+ extension ReferenceIntArray {
158
149
@available ( BackDeploy 1 . 0 , * )
159
150
@_backDeploy ( before: BackDeploy 2.0 )
160
- public final var value : Int { _value }
151
+ public final var values : [ Int ] { _values }
161
152
162
153
@available ( BackDeploy 1 . 0 , * )
163
154
@_backDeploy ( before: BackDeploy 2.0 )
164
155
public final func print( ) {
165
- // Tests recursive use of back deployed APIs, since `value ` is also
166
- testPrint ( handle: #dsohandle, String ( value ) )
156
+ // Tests recursive @_backDeploy since `values ` is also @_backDeploy
157
+ testPrint ( handle: #dsohandle, values . description )
167
158
}
168
159
169
160
@available ( BackDeploy 1 . 0 , * )
170
161
@_backDeploy ( before: BackDeploy 2.0 )
171
- public final func copy( ) -> ReferenceInt {
172
- return ReferenceInt ( value )
162
+ public final func copy( ) -> ReferenceIntArray {
163
+ return ReferenceIntArray ( values )
173
164
}
174
165
175
166
@available ( BackDeploy 1 . 0 , * )
176
167
@_backDeploy ( before: BackDeploy 2.0 )
177
- public final class var zero : ReferenceInt { ReferenceInt ( 0 ) }
178
-
179
- @available ( BackDeploy 1 . 0 , * )
180
- @_backDeploy ( before: BackDeploy 2.0 )
181
- public final func decrement( by amount: Int ) -> Int {
182
- _value -= amount
183
- return _value
184
- }
168
+ public final class var empty : ReferenceIntArray { ReferenceIntArray ( [ ] ) }
185
169
186
170
@available ( BackDeploy 1 . 0 , * )
187
171
@_backDeploy ( before: BackDeploy 2.0 )
188
- public final func toIncrementable ( ) -> any Incrementable { self }
172
+ public final func toCountable ( ) -> any Countable { self }
189
173
190
174
@available ( BackDeploy 1 . 0 , * )
191
175
@_backDeploy ( before: BackDeploy 2.0 )
192
- public final subscript( byteAt index: Int ) -> UInt8 { _value. byte ( at: index) }
176
+ public final subscript( _ i: Int ) -> Int {
177
+ get { _values [ i] }
178
+ _modify { yield & _values[ i] }
179
+ }
193
180
}
194
181
195
182
#endif // !STRIP_V2_APIS
0 commit comments