Skip to content

Commit a11a7d7

Browse files
support returning a value from the closure
1 parent 2ead1c9 commit a11a7d7

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

Sources/VariableLengthArray/VLArray.swift

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -183,105 +183,111 @@ extension VLArray where Element: ~Copyable {
183183
// MARK: Create
184184
// copyable
185185
extension VLArray {
186+
@discardableResult
186187
@inlinable
187-
public static func create<E: Error>(
188+
public static func create<E: Error, T>(
188189
amount: Int,
189190
default: Element,
190-
_ closure: (consuming Self) throws(E) -> Void
191-
) rethrows {
192-
try withUnsafeTemporaryAllocation(of: Element.self, capacity: amount, { p in
191+
_ closure: (consuming Self) throws(E) -> T
192+
) rethrows -> T {
193+
return try withUnsafeTemporaryAllocation(of: Element.self, capacity: amount, { p in
193194
p.initialize(repeating: `default`)
194195
defer {
195196
p.deinitialize()
196197
}
197198
let array = Self(_storage: p)
198-
try closure(array)
199+
return try closure(array)
199200
})
200201
}
201202

203+
@discardableResult
202204
@inlinable
203-
public static func create<E: Error>(
205+
public static func create<E: Error, T>(
204206
amount: Int,
205207
initialize: (Index) -> Element,
206-
_ closure: (consuming Self) throws(E) -> Void
207-
) rethrows {
208-
try withUnsafeTemporaryAllocation(of: Element.self, capacity: amount, { p in
208+
_ closure: (consuming Self) throws(E) -> T
209+
) rethrows -> T {
210+
return try withUnsafeTemporaryAllocation(of: Element.self, capacity: amount, { p in
209211
for i in 0..<amount {
210212
p[i] = initialize(i)
211213
}
212214
defer {
213215
p.deinitialize()
214216
}
215217
let array = Self(_storage: p)
216-
try closure(array)
218+
return try closure(array)
217219
})
218220
}
219221
}
220222

221223
// noncopyable
222224
extension VLArray where Element: ~Copyable {
225+
@discardableResult
223226
@inlinable
224-
public static func create<E: Error>(
227+
public static func create<E: Error, T>(
225228
amount: Int,
226229
initialize: (Index) -> Element,
227-
_ closure: (consuming Self) throws(E) -> Void
228-
) rethrows {
229-
try withUnsafeTemporaryAllocation(of: Element.self, capacity: amount, { p in
230+
_ closure: (consuming Self) throws(E) -> T
231+
) rethrows -> T {
232+
return try withUnsafeTemporaryAllocation(of: Element.self, capacity: amount, { p in
230233
for i in 0..<amount {
231234
p[i] = initialize(i)
232235
}
233236
defer {
234237
p.deinitialize()
235238
}
236239
let array = Self(_storage: p)
237-
try closure(array)
240+
return try closure(array)
238241
})
239242
}
240243
}
241244

242245
// other
243246
extension VLArray where Element == UInt8 {
244247

248+
@discardableResult
245249
@inlinable
246-
public static func create<E: Error>(
250+
public static func create<E: Error, T>(
247251
string: StaticString,
248-
_ closure: (consuming Self) throws(E) -> Void
249-
) rethrows {
250-
try withUnsafeTemporaryAllocation(of: Element.self, capacity: string.utf8CodeUnitCount, { p in
252+
_ closure: (consuming Self) throws(E) -> T
253+
) rethrows -> T {
254+
return try withUnsafeTemporaryAllocation(of: Element.self, capacity: string.utf8CodeUnitCount, { p in
251255
string.withUTF8Buffer {
252256
p.initialize(fromContentsOf: $0)
253257
}
254258
defer {
255259
p.deinitialize()
256260
}
257261
let array = Self(_storage: p)
258-
try closure(array)
262+
return try closure(array)
259263
})
260264
}
261265

266+
@discardableResult
262267
@inlinable
263-
public static func create<E: Error>(
268+
public static func create<E: Error, T>(
264269
string: some StringProtocol,
265-
_ closure: (consuming Self) throws(E) -> Void
266-
) rethrows {
270+
_ closure: (consuming Self) throws(E) -> T
271+
) rethrows -> T {
267272
let utf8 = string.utf8
268-
try withUnsafeTemporaryAllocation(of: Element.self, capacity: utf8.count, { p in
273+
return try withUnsafeTemporaryAllocation(of: Element.self, capacity: utf8.count, { p in
269274
let endIndexPlusOne = p.initialize(fromContentsOf: utf8)
270275
defer {
271276
p.deinitialize()
272277
}
273278
let array = Self(_storage: p)
274-
try closure(array)
279+
return try closure(array)
275280
})
276281
}
277282

283+
@discardableResult
278284
@inlinable
279-
public static func create<E: Error>(
285+
public static func create<E: Error, T>(
280286
collection: some Collection<UInt8>,
281-
_ closure: (consuming Self) throws(E) -> Void
282-
) rethrows {
287+
_ closure: (consuming Self) throws(E) -> T
288+
) rethrows -> T {
283289
let count = collection.count
284-
try withUnsafeTemporaryAllocation(
290+
return try withUnsafeTemporaryAllocation(
285291
of: Element.self,
286292
capacity: count
287293
) { p in
@@ -290,7 +296,7 @@ extension VLArray where Element == UInt8 {
290296
p.deinitialize()
291297
}
292298
let array = Self(_storage: p)
293-
try closure(array)
299+
return try closure(array)
294300
}
295301
}
296302
}

0 commit comments

Comments
 (0)