@@ -31,63 +31,58 @@ public func v2APIsAreStripped() -> Bool {
3131#endif // STRIP_V2_APIS
3232}
3333
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+
3445public enum BadError : Error , Equatable {
3546 /// Indicates badness
3647 case bad
3748}
3849
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- }
4650
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 {
4953 @usableFromInline
50- internal var _value : Int
54+ internal var _values : [ Int ]
5155
52- public init ( _ value : Int ) { _value = value }
56+ public init ( _ values : [ Int ] ) { _values = values }
5357}
5458
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+ }
5864
59- mutating func incrementByOne ( ) -> String
60- mutating func increment ( by amount : Operand ) -> Operand
65+ extension IntArray : Countable {
66+ public var count : Int { _values . count }
6167}
6268
63- extension MutableInt : Incrementable {
64- public mutating func incrementByOne( ) -> String {
65- _value += 1
66- return String ( _value)
67- }
6869
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 ]
7474
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+ }
8077
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 )
8481 }
8582}
8683
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 }
9186}
9287
9388// MARK: - Back deployed APIs
@@ -109,87 +104,79 @@ public func pleaseThrow(_ shouldThrow: Bool) throws -> Bool {
109104
110105@available ( BackDeploy 1 . 0 , * )
111106@_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 )
117112}
118113
119114@available ( BackDeploy 1 . 0 , * )
120115@_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
123118}
124119
125- extension MutableInt {
120+ extension IntArray {
126121 @available ( BackDeploy 1 . 0 , * )
127122 @_backDeploy ( before: BackDeploy 2.0 )
128- public var value : Int { _value }
123+ public var values : [ Int ] { _values }
129124
130125 @available ( BackDeploy 1 . 0 , * )
131126 @_backDeploy ( before: BackDeploy 2.0 )
132127 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 )
135130 }
136131
137132 @available ( BackDeploy 1 . 0 , * )
138133 @_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 ( [ ] ) }
147135
148136 @available ( BackDeploy 1 . 0 , * )
149137 @_backDeploy ( before: BackDeploy 2.0 )
150- public func toIncrementable ( ) -> any Incrementable { self }
138+ public func toCountable ( ) -> any Countable { self }
151139
152140 @available ( BackDeploy 1 . 0 , * )
153141 @_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+ }
155146}
156147
157- extension ReferenceInt {
148+ extension ReferenceIntArray {
158149 @available ( BackDeploy 1 . 0 , * )
159150 @_backDeploy ( before: BackDeploy 2.0 )
160- public final var value : Int { _value }
151+ public final var values : [ Int ] { _values }
161152
162153 @available ( BackDeploy 1 . 0 , * )
163154 @_backDeploy ( before: BackDeploy 2.0 )
164155 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 )
167158 }
168159
169160 @available ( BackDeploy 1 . 0 , * )
170161 @_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 )
173164 }
174165
175166 @available ( BackDeploy 1 . 0 , * )
176167 @_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 ( [ ] ) }
185169
186170 @available ( BackDeploy 1 . 0 , * )
187171 @_backDeploy ( before: BackDeploy 2.0 )
188- public final func toIncrementable ( ) -> any Incrementable { self }
172+ public final func toCountable ( ) -> any Countable { self }
189173
190174 @available ( BackDeploy 1 . 0 , * )
191175 @_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+ }
193180}
194181
195182#endif // !STRIP_V2_APIS
0 commit comments