Skip to content

Commit dd40ff2

Browse files
committed
[stdlib][NFC] Put stored properties & primary initializers before other members in struct declarations
1 parent fb8ec6f commit dd40ff2

31 files changed

+574
-563
lines changed

stdlib/public/core/ArrayBuffer.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ internal typealias _ArrayBridgeStorage
2525
@usableFromInline
2626
@frozen
2727
internal struct _ArrayBuffer<Element>: _ArrayBufferProtocol {
28+
@usableFromInline
29+
internal var _storage: _ArrayBridgeStorage
30+
31+
@inlinable
32+
internal init(storage: _ArrayBridgeStorage) {
33+
_storage = storage
34+
}
2835

2936
/// Create an empty buffer.
3037
@inlinable
@@ -74,15 +81,6 @@ internal struct _ArrayBuffer<Element>: _ArrayBufferProtocol {
7481
// NSArray's need an element typecheck when the element type isn't AnyObject
7582
return !_isNativeTypeChecked && !(AnyObject.self is Element.Type)
7683
}
77-
78-
//===--- private --------------------------------------------------------===//
79-
@inlinable
80-
internal init(storage: _ArrayBridgeStorage) {
81-
_storage = storage
82-
}
83-
84-
@usableFromInline
85-
internal var _storage: _ArrayBridgeStorage
8684
}
8785

8886
extension _ArrayBuffer {

stdlib/public/core/BridgingBuffer.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
internal struct _BridgingBufferHeader {
14-
internal init(_ count: Int) { self.count = count }
1514
internal var count: Int
15+
16+
internal init(_ count: Int) { self.count = count }
1617
}
1718

1819
// NOTE: older runtimes called this class _BridgingBufferStorage.

stdlib/public/core/Codable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,8 +3369,8 @@ public enum DecodingError: Error {
33693369
// The following extensions allow for easier error construction.
33703370

33713371
internal struct _GenericIndexKey: CodingKey, Sendable {
3372-
internal var stringValue: String
3373-
internal var intValue: Int?
3372+
internal var stringValue: String
3373+
internal var intValue: Int?
33743374

33753375
internal init?(stringValue: String) {
33763376
return nil

stdlib/public/core/Collection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,18 @@ public struct IndexingIterator<Elements: Collection> {
7272
@usableFromInline
7373
internal var _position: Elements.Index
7474

75+
/// Creates an iterator over the given collection.
7576
@inlinable
7677
@inline(__always)
77-
/// Creates an iterator over the given collection.
7878
public /// @testable
7979
init(_elements: Elements) {
8080
self._elements = _elements
8181
self._position = _elements.startIndex
8282
}
8383

84+
/// Creates an iterator over the given collection.
8485
@inlinable
8586
@inline(__always)
86-
/// Creates an iterator over the given collection.
8787
public /// @testable
8888
init(_elements: Elements, _position: Elements.Index) {
8989
self._elements = _elements

stdlib/public/core/CollectionDifference.swift

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -72,59 +72,6 @@ public struct CollectionDifference<ChangeElement> {
7272
/// The removals contained by this difference, from lowest offset to highest.
7373
public let removals: [Change]
7474

75-
/// The public initializer calls this function to ensure that its parameter
76-
/// meets the conditions set in its documentation.
77-
///
78-
/// - Parameter changes: a collection of `CollectionDifference.Change`
79-
/// instances intended to represent a valid state transition for
80-
/// `CollectionDifference`.
81-
///
82-
/// - Returns: whether the parameter meets the following criteria:
83-
///
84-
/// 1. All insertion offsets are unique
85-
/// 2. All removal offsets are unique
86-
/// 3. All associations between insertions and removals are symmetric
87-
///
88-
/// Complexity: O(`changes.count`)
89-
private static func _validateChanges<Changes: Collection>(
90-
_ changes : Changes
91-
) -> Bool where Changes.Element == Change {
92-
if changes.isEmpty { return true }
93-
94-
var insertAssocToOffset = Dictionary<Int,Int>()
95-
var removeOffsetToAssoc = Dictionary<Int,Int>()
96-
var insertOffset = Set<Int>()
97-
var removeOffset = Set<Int>()
98-
99-
for change in changes {
100-
let offset = change._offset
101-
if offset < 0 { return false }
102-
103-
switch change {
104-
case .remove(_, _, _):
105-
if removeOffset.contains(offset) { return false }
106-
removeOffset.insert(offset)
107-
case .insert(_, _, _):
108-
if insertOffset.contains(offset) { return false }
109-
insertOffset.insert(offset)
110-
}
111-
112-
if let assoc = change._associatedOffset {
113-
if assoc < 0 { return false }
114-
switch change {
115-
case .remove(_, _, _):
116-
if removeOffsetToAssoc[offset] != nil { return false }
117-
removeOffsetToAssoc[offset] = assoc
118-
case .insert(_, _, _):
119-
if insertAssocToOffset[assoc] != nil { return false }
120-
insertAssocToOffset[assoc] = offset
121-
}
122-
}
123-
}
124-
125-
return removeOffsetToAssoc == insertAssocToOffset
126-
}
127-
12875
/// Creates a new collection difference from a collection of changes.
12976
///
13077
/// To find the difference between two collections, use the
@@ -201,6 +148,59 @@ public struct CollectionDifference<ChangeElement> {
201148
insertions = Array(sortedChanges[firstInsertIndex..<sortedChanges.count])
202149
}
203150

151+
/// The public initializer calls this function to ensure that its parameter
152+
/// meets the conditions set in its documentation.
153+
///
154+
/// - Parameter changes: a collection of `CollectionDifference.Change`
155+
/// instances intended to represent a valid state transition for
156+
/// `CollectionDifference`.
157+
///
158+
/// - Returns: whether the parameter meets the following criteria:
159+
///
160+
/// 1. All insertion offsets are unique
161+
/// 2. All removal offsets are unique
162+
/// 3. All associations between insertions and removals are symmetric
163+
///
164+
/// Complexity: O(`changes.count`)
165+
private static func _validateChanges<Changes: Collection>(
166+
_ changes : Changes
167+
) -> Bool where Changes.Element == Change {
168+
if changes.isEmpty { return true }
169+
170+
var insertAssocToOffset = Dictionary<Int,Int>()
171+
var removeOffsetToAssoc = Dictionary<Int,Int>()
172+
var insertOffset = Set<Int>()
173+
var removeOffset = Set<Int>()
174+
175+
for change in changes {
176+
let offset = change._offset
177+
if offset < 0 { return false }
178+
179+
switch change {
180+
case .remove(_, _, _):
181+
if removeOffset.contains(offset) { return false }
182+
removeOffset.insert(offset)
183+
case .insert(_, _, _):
184+
if insertOffset.contains(offset) { return false }
185+
insertOffset.insert(offset)
186+
}
187+
188+
if let assoc = change._associatedOffset {
189+
if assoc < 0 { return false }
190+
switch change {
191+
case .remove(_, _, _):
192+
if removeOffsetToAssoc[offset] != nil { return false }
193+
removeOffsetToAssoc[offset] = assoc
194+
case .insert(_, _, _):
195+
if insertAssocToOffset[assoc] != nil { return false }
196+
insertAssocToOffset[assoc] = offset
197+
}
198+
}
199+
}
200+
201+
return removeOffsetToAssoc == insertAssocToOffset
202+
}
203+
204204
public func inverse() -> Self {
205205
return CollectionDifference(_validatedChanges: self.map { c in
206206
switch c {

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ internal final class _ContiguousArrayStorage<
253253
@usableFromInline
254254
@frozen
255255
internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
256+
@usableFromInline
257+
internal var _storage: __ContiguousArrayStorageBase
256258

257259
/// Make a buffer with uninitialized elements. After using this
258260
/// method, you must either initialize the `count` elements at the
@@ -846,9 +848,6 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
846848
}
847849
return true
848850
}
849-
850-
@usableFromInline
851-
internal var _storage: __ContiguousArrayStorageBase
852851
}
853852

854853
/// Append the elements of `rhs` to `lhs`.

stdlib/public/core/Diffing.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,6 @@ private struct _V {
193193
private let isOdd: Bool
194194
#endif
195195

196-
// The way negative indexes are implemented is by interleaving them in the empty slots between the valid positive indexes
197-
@inline(__always) private static func transform(_ index: Int) -> Int {
198-
// -3, -1, 1, 3 -> 3, 1, 0, 2 -> 0...3
199-
// -2, 0, 2 -> 2, 0, 1 -> 0...2
200-
return (index <= 0 ? -index : index &- 1)
201-
}
202-
203196
init(maxIndex largest: Int) {
204197
#if INTERNAL_CHECKS_ENABLED
205198
_internalInvariant(largest >= 0)
@@ -208,6 +201,13 @@ private struct _V {
208201
a = [Int](repeating: 0, count: largest + 1)
209202
}
210203

204+
// The way negative indexes are implemented is by interleaving them in the empty slots between the valid positive indexes
205+
@inline(__always) private static func transform(_ index: Int) -> Int {
206+
// -3, -1, 1, 3 -> 3, 1, 0, 2 -> 0...3
207+
// -2, 0, 2 -> 2, 0, 1 -> 0...2
208+
return (index <= 0 ? -index : index &- 1)
209+
}
210+
211211
subscript(index: Int) -> Int {
212212
get {
213213
#if INTERNAL_CHECKS_ENABLED

stdlib/public/core/DropWhile.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
@frozen // lazy-performance
1616
public struct LazyDropWhileSequence<Base: Sequence> {
1717
public typealias Element = Base.Element
18-
18+
19+
@usableFromInline // lazy-performance
20+
internal var _base: Base
21+
@usableFromInline // lazy-performance
22+
internal let _predicate: (Element) -> Bool
23+
1924
/// Create an instance with elements `transform(x)` for each element
2025
/// `x` of base.
2126
@inlinable // lazy-performance
@@ -24,10 +29,6 @@ public struct LazyDropWhileSequence<Base: Sequence> {
2429
self._predicate = predicate
2530
}
2631

27-
@usableFromInline // lazy-performance
28-
internal var _base: Base
29-
@usableFromInline // lazy-performance
30-
internal let _predicate: (Element) -> Bool
3132
}
3233

3334
extension LazyDropWhileSequence {
@@ -40,19 +41,19 @@ extension LazyDropWhileSequence {
4041
@frozen // lazy-performance
4142
public struct Iterator {
4243
public typealias Element = Base.Element
43-
44-
@inlinable // lazy-performance
45-
internal init(_base: Base.Iterator, predicate: @escaping (Element) -> Bool) {
46-
self._base = _base
47-
self._predicate = predicate
48-
}
4944

5045
@usableFromInline // lazy-performance
5146
internal var _predicateHasFailed = false
5247
@usableFromInline // lazy-performance
5348
internal var _base: Base.Iterator
5449
@usableFromInline // lazy-performance
5550
internal let _predicate: (Element) -> Bool
51+
52+
@inlinable // lazy-performance
53+
internal init(_base: Base.Iterator, predicate: @escaping (Element) -> Bool) {
54+
self._base = _base
55+
self._predicate = predicate
56+
}
5657
}
5758
}
5859

stdlib/public/core/ExistentialCollection.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,16 @@ extension AnyIterator: Sequence { }
108108
@usableFromInline
109109
@frozen
110110
internal struct _ClosureBasedIterator<Element>: IteratorProtocol {
111+
@usableFromInline
112+
internal let _body: () -> Element?
113+
111114
@inlinable
112115
internal init(_ body: @escaping () -> Element?) {
113116
self._body = body
114117
}
115118

116119
@inlinable
117120
internal func next() -> Element? { return _body() }
118-
119-
@usableFromInline
120-
internal let _body: () -> Element?
121121
}
122122

123123
@_fixed_layout

stdlib/public/core/FloatingPointTypes.swift.gyb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ extension ${Self}: BinaryFloatingPoint {
286286
@usableFromInline
287287
internal var _storage: (UInt64, UInt16, /* pad */ UInt16, UInt16, UInt16)
288288

289+
@usableFromInline
290+
@_transparent
291+
internal init(explicitSignificand: UInt64, signAndExponent: UInt16) {
292+
_storage = (explicitSignificand, signAndExponent, 0, 0, 0)
293+
}
294+
289295
@usableFromInline
290296
@_transparent
291297
internal var explicitSignificand: UInt64 { return _storage.0 }
@@ -305,12 +311,6 @@ extension ${Self}: BinaryFloatingPoint {
305311
internal var exponentBitPattern: UInt {
306312
return UInt(signAndExponent) & 0x7fff
307313
}
308-
309-
@usableFromInline
310-
@_transparent
311-
internal init(explicitSignificand: UInt64, signAndExponent: UInt16) {
312-
_storage = (explicitSignificand, signAndExponent, 0, 0, 0)
313-
}
314314
}
315315

316316
@inlinable
@@ -1279,6 +1279,7 @@ extension ${Self}: Strideable {
12791279
% if bits != 16:
12801280
internal struct _${Self}AnyHashableBox: _AnyHashableBox {
12811281
internal typealias Base = ${Self}
1282+
12821283
internal let _value: Base
12831284

12841285
internal init(_ value: Base) {

0 commit comments

Comments
 (0)