Skip to content

Commit e2415f7

Browse files
committed
[stdlib] _UnsafeBitset.withTemporaryBitset: New internal function
1 parent b294fe9 commit e2415f7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

stdlib/public/core/Bitset.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,35 @@ extension _UnsafeBitset.Word: Sequence, IteratorProtocol {
337337
return bit
338338
}
339339
}
340+
341+
extension _UnsafeBitset {
342+
@_alwaysEmitIntoClient
343+
@inline(__always)
344+
internal static func _withTemporaryUninitializedBitset<R>(
345+
wordCount: Int,
346+
body: (_UnsafeBitset) throws -> R
347+
) rethrows -> R {
348+
try withUnsafeTemporaryAllocation(
349+
of: _UnsafeBitset.Word.self, capacity: wordCount
350+
) { buffer in
351+
let bitset = _UnsafeBitset(
352+
words: buffer.baseAddress!, wordCount: buffer.count)
353+
return try body(bitset)
354+
}
355+
}
356+
357+
@_alwaysEmitIntoClient
358+
@inline(__always)
359+
internal static func withTemporaryBitset<R>(
360+
capacity: Int,
361+
body: (_UnsafeBitset) throws -> R
362+
) rethrows -> R {
363+
let wordCount = Swift.max(1, Self.wordCount(forCapacity: capacity))
364+
return try _withTemporaryUninitializedBitset(
365+
wordCount: wordCount
366+
) { bitset in
367+
bitset.clear()
368+
return try body(bitset)
369+
}
370+
}
371+
}

0 commit comments

Comments
 (0)