@@ -95,7 +95,10 @@ extension InlineArray where Element: ~Copyable {
95
95
@_transparent
96
96
internal var _mutableBuffer : UnsafeMutableBufferPointer < Element > {
97
97
mutating get {
98
- unsafe UnsafeMutableBufferPointer< Element > ( start: _mutableAddress, count: count)
98
+ unsafe UnsafeMutableBufferPointer< Element > (
99
+ start: _mutableAddress,
100
+ count: count
101
+ )
99
102
}
100
103
}
101
104
@@ -121,14 +124,18 @@ extension InlineArray where Element: ~Copyable {
121
124
@available ( SwiftStdlib 6 . 2 , * )
122
125
extension InlineArray where Element: ~ Copyable {
123
126
/// Initializes every element in this array, by calling the given closure
124
- /// for each index.
127
+ /// with each index.
125
128
///
126
129
/// This will call the closure `count` times, where `count` is the static
127
130
/// count of the array, to initialize every element by passing the closure
128
- /// the index of the current element being initialized. The closure is allowed
129
- /// to throw an error at any point during initialization at which point the
130
- /// array will stop initialization, deinitialize every currently initialized
131
- /// element, and throw the given error back out to the caller.
131
+ /// the index of the current element being initialized.
132
+ ///
133
+ /// InlineArray<4, Int> { 1 << $0 } //-> [1, 2, 4, 8]
134
+ ///
135
+ /// The closure is allowed to throw an error at any point during
136
+ /// initialization at which point the array will stop initialization,
137
+ /// deinitialize every currently initialized element, and throw the given
138
+ /// error back out to the caller.
132
139
///
133
140
/// - Parameter body: A closure that returns an owned `Element` to emplace at
134
141
/// the passed in index.
@@ -139,9 +146,7 @@ extension InlineArray where Element: ~Copyable {
139
146
public init< E: Error > ( _ body: ( Index ) throws ( E ) -> Element ) throws ( E) {
140
147
#if $BuiltinEmplaceTypedThrows
141
148
self = try Builtin . emplace { ( rawPtr) throws ( E) -> ( ) in
142
- let buffer = InlineArray < count , Element > . _initializationBuffer (
143
- start: rawPtr
144
- )
149
+ let buffer = Self . _initializationBuffer ( start: rawPtr)
145
150
146
151
for i in 0 ..< count {
147
152
do throws ( E) {
@@ -164,19 +169,23 @@ extension InlineArray where Element: ~Copyable {
164
169
}
165
170
166
171
/// Initializes every element in this array, by calling the given closure
167
- /// for each previously initialized element.
172
+ /// with each preceding element.
168
173
///
169
174
/// This will call the closure `count - 1` times, where `count` is the static
170
175
/// count of the array, to initialize every element by passing the closure an
171
- /// immutable borrow reference to the previous element. The closure is allowed
172
- /// to throw an error at any point during initialization at which point the
173
- /// array will stop initialization, deinitialize every currently initialized
174
- /// element, and throw the given error back out to the caller.
176
+ /// immutable borrow reference to the preceding element.
177
+ ///
178
+ /// InlineArray<4, Int>(first: 1) { $0 << 1 } //-> [1, 2, 4, 8]
179
+ ///
180
+ /// The closure is allowed to throw an error at any point during
181
+ /// initialization at which point the array will stop initialization,
182
+ /// deinitialize every currently initialized element, and throw the given
183
+ /// error back out to the caller.
175
184
///
176
185
/// - Parameters:
177
186
/// - first: The first value to emplace into the array.
178
187
/// - next: A closure that takes an immutable borrow reference to the
179
- /// previous element, and returns an owned `Element` instance to emplace
188
+ /// preceding element, and returns an owned `Element` instance to emplace
180
189
/// into the array.
181
190
///
182
191
/// - Complexity: O(*n*), where *n* is the number of elements in the array.
@@ -194,11 +203,16 @@ extension InlineArray where Element: ~Copyable {
194
203
var o : Element ? = first
195
204
196
205
self = try Builtin . emplace { ( rawPtr) throws ( E) -> ( ) in
197
- let buffer = InlineArray < count , Element > . _initializationBuffer (
198
- start: rawPtr
199
- )
206
+ let buffer = Self . _initializationBuffer ( start: rawPtr)
200
207
201
- unsafe buffer. initializeElement ( at: 0 , to: o. take ( ) . _consumingUncheckedUnwrapped ( ) )
208
+ guard Self . count > 0 else {
209
+ return
210
+ }
211
+
212
+ unsafe buffer. initializeElement (
213
+ at: 0 ,
214
+ to: o. take ( ) . _consumingUncheckedUnwrapped ( )
215
+ )
202
216
203
217
for i in 1 ..< count {
204
218
do throws ( E) {
@@ -232,7 +246,7 @@ extension InlineArray where Element: Copyable {
232
246
@_alwaysEmitIntoClient
233
247
public init ( repeating value: Element ) {
234
248
self = Builtin . emplace {
235
- let buffer = InlineArray < count , Element > . _initializationBuffer ( start: $0)
249
+ let buffer = Self . _initializationBuffer ( start: $0)
236
250
237
251
unsafe buffer. initialize ( repeating: value)
238
252
}
@@ -431,8 +445,8 @@ extension InlineArray where Element: ~Copyable {
431
445
return
432
446
}
433
447
434
- _precondition ( indices . contains ( i ) , " Index out of bounds " )
435
- _precondition ( indices . contains ( j ) , " Index out of bounds " )
448
+ _checkIndex ( i )
449
+ _checkIndex ( j )
436
450
437
451
let ithElement = unsafe _mutableBuffer . moveElement( from: i)
438
452
let jthElement = unsafe _mutableBuffer . moveElement( from: j)
0 commit comments