Skip to content

Commit 628c98a

Browse files
added support to return noncopyable types from VLArray closures; 1 fix
1 parent 4315a00 commit 628c98a

File tree

4 files changed

+47
-45
lines changed

4 files changed

+47
-45
lines changed

.swift-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.1.2

Sources/VariableLengthArray/Joined.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11

22
#if compiler(>=6.2)
33

4+
extension VLArray {
5+
@inlinable
6+
public func join<let count: Int>(
7+
_ arrays: consuming InlineArray<count, VLArray>,
8+
_ body: (inout Joined) throws -> Void
9+
) rethrows {
10+
try withUnsafeTemporaryAllocation(
11+
of: UnsafeMutableBufferPointer<Element>.self,
12+
capacity: 1 + count
13+
) { p in
14+
p.initializeElement(at: 0, to: self._storage)
15+
for i in arrays.indices {
16+
p.initializeElement(at: 1 + i, to: arrays[i]._storage)
17+
}
18+
defer {
19+
p.deinitialize()
20+
}
21+
var joined = Joined.init(_storage: p)
22+
try body(&joined)
23+
}
24+
}
25+
}
26+
427
extension VLArray {
528
public struct Joined: ~Copyable, @unchecked Sendable {
629
public typealias Index = Int

Sources/VariableLengthArray/VLArray.swift

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -191,27 +191,27 @@ extension VLArray where Element: ~Copyable {
191191
extension VLArray {
192192
@discardableResult
193193
@inlinable
194-
public static func create<T>(
194+
public static func create<T: ~Copyable>(
195195
amount: Int,
196196
default: Element,
197-
_ closure: (consuming Self) throws -> T
197+
_ body: (consuming Self) throws -> T
198198
) rethrows -> T {
199199
return try withUnsafeTemporaryAllocation(of: Element.self, capacity: amount, { p in
200200
p.initialize(repeating: `default`)
201201
defer {
202202
p.deinitialize()
203203
}
204204
let array = Self(_storage: p)
205-
return try closure(array)
205+
return try body(array)
206206
})
207207
}
208208

209209
@discardableResult
210210
@inlinable
211-
public static func create<T>(
211+
public static func create<T: ~Copyable>(
212212
amount: Int,
213213
initialize: (Index) -> Element,
214-
_ closure: (consuming Self) throws -> T
214+
_ body: (consuming Self) throws -> T
215215
) rethrows -> T {
216216
return try withUnsafeTemporaryAllocation(of: Element.self, capacity: amount, { p in
217217
for i in 0..<amount {
@@ -221,7 +221,7 @@ extension VLArray {
221221
p.deinitialize()
222222
}
223223
let array = Self(_storage: p)
224-
return try closure(array)
224+
return try body(array)
225225
})
226226
}
227227
}
@@ -230,10 +230,10 @@ extension VLArray {
230230
extension VLArray where Element: ~Copyable {
231231
@discardableResult
232232
@inlinable
233-
public static func create<T>(
233+
public static func create<T: ~Copyable>(
234234
amount: Int,
235235
initialize: (Index) -> Element,
236-
_ closure: (consuming Self) throws -> T
236+
_ body: (consuming Self) throws -> T
237237
) rethrows -> T {
238238
return try withUnsafeTemporaryAllocation(of: Element.self, capacity: amount, { p in
239239
for i in 0..<amount {
@@ -243,7 +243,7 @@ extension VLArray where Element: ~Copyable {
243243
p.deinitialize()
244244
}
245245
let array = Self(_storage: p)
246-
return try closure(array)
246+
return try body(array)
247247
})
248248
}
249249
}
@@ -253,9 +253,9 @@ extension VLArray where Element == UInt8 {
253253

254254
@discardableResult
255255
@inlinable
256-
public static func create<T>(
256+
public static func create<T: ~Copyable>(
257257
string: StaticString,
258-
_ closure: (consuming Self) throws -> T
258+
_ body: (consuming Self) throws -> T
259259
) rethrows -> T {
260260
return try withUnsafeTemporaryAllocation(of: Element.self, capacity: string.utf8CodeUnitCount, { p in
261261
string.withUTF8Buffer {
@@ -265,15 +265,15 @@ extension VLArray where Element == UInt8 {
265265
p.deinitialize()
266266
}
267267
let array = Self(_storage: p)
268-
return try closure(array)
268+
return try body(array)
269269
})
270270
}
271271

272272
@discardableResult
273273
@inlinable
274-
public static func create<T>(
274+
public static func create<T: ~Copyable>(
275275
string: some StringProtocol,
276-
_ closure: (consuming Self) throws -> T
276+
_ body: (consuming Self) throws -> T
277277
) rethrows -> T {
278278
let utf8 = string.utf8
279279
return try withUnsafeTemporaryAllocation(of: Element.self, capacity: utf8.count, { p in
@@ -282,15 +282,15 @@ extension VLArray where Element == UInt8 {
282282
p.deinitialize()
283283
}
284284
let array = Self(_storage: p)
285-
return try closure(array)
285+
return try body(array)
286286
})
287287
}
288288

289289
@discardableResult
290290
@inlinable
291-
public static func create<T>(
291+
public static func create<T: ~Copyable>(
292292
collection: some Collection<UInt8>,
293-
_ closure: (consuming Self) throws -> T
293+
_ body: (consuming Self) throws -> T
294294
) rethrows -> T {
295295
let count = collection.count
296296
return try withUnsafeTemporaryAllocation(
@@ -302,33 +302,7 @@ extension VLArray where Element == UInt8 {
302302
p.deinitialize()
303303
}
304304
let array = Self(_storage: p)
305-
return try closure(array)
305+
return try body(array)
306306
}
307307
}
308-
}
309-
310-
// MARK: Join
311-
#if compiler(>=6.2)
312-
extension VLArray {
313-
@inlinable
314-
public func join<let count: Int>(
315-
_ arrays: consuming InlineArray<count, VLArray>,
316-
_ closure: (inout Joined) throws -> Void
317-
) rethrows {
318-
try withUnsafeTemporaryAllocation(
319-
of: UnsafeMutableBufferPointer<Element>.self,
320-
capacity: 1 + count
321-
) { p in
322-
p.initializeElement(at: 0, to: self._storage)
323-
for i in arrays.indices {
324-
p.initializeElement(at: 1 + i, to: arrays[i]._storage)
325-
}
326-
defer {
327-
p.deinitialize()
328-
}
329-
var joined = Joined.init(_storage: p)
330-
try closure(&joined)
331-
}
332-
}
333-
}
334-
#endif
308+
}

Tests/swift-variablelengtharrayTests/VariableLengthArrayTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ struct VariableLengthArrayTests {
5353
for i in array.indices {
5454
#expect(i == array[i].bro)
5555
}
56+
return TestNonCopyable(bro: 0)
5657
})
5758
}
5859

@@ -67,6 +68,7 @@ struct VariableLengthArrayTests {
6768
amount += 1
6869
let contained = array.contains(where: { $0.bro == amount })
6970
#expect(!contained)
71+
return TestCopyable(bro: 0)
7072
}
7173
VLArray<TestNonCopyable>.create(amount: amount, initialize: ({ .init(bro: $0) })) { array in
7274
for i in 0..<amount {
@@ -79,6 +81,7 @@ struct VariableLengthArrayTests {
7981
}
8082
}
8183

84+
#if compiler(>=6.2)
8285
@Test
8386
func joinedVLArrayVL() {
8487
VLArray<UInt8>.create(amount: 5, default: 0) { first in
@@ -97,6 +100,7 @@ struct VariableLengthArrayTests {
97100
}
98101
}
99102
}
103+
#endif
100104
}
101105

102106
struct TestCopyable {

0 commit comments

Comments
 (0)