Skip to content

Commit 16d02db

Browse files
committed
[cxx-interop] Add internal function CxxConvertibleToCollection.forEach
1 parent e3effcc commit 16d02db

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

stdlib/public/Cxx/CxxConvertibleToCollection.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,17 @@ public protocol CxxConvertibleToCollection {
2121
mutating func __endUnsafe() -> RawIterator
2222
}
2323

24-
@inlinable @inline(__always)
25-
internal func forEachElement<C: CxxConvertibleToCollection>(
26-
of c: C,
27-
body: (C.RawIterator.Pointee) -> Void
28-
) {
29-
var mutableC = c
30-
withExtendedLifetime(mutableC) {
31-
var rawIterator = mutableC.__beginUnsafe()
32-
let endIterator = mutableC.__endUnsafe()
24+
extension CxxConvertibleToCollection {
25+
@inlinable
26+
internal func forEach(_ body: (RawIterator.Pointee) -> Void) {
27+
var mutableSelf = self
28+
var rawIterator = mutableSelf.__beginUnsafe()
29+
let endIterator = mutableSelf.__endUnsafe()
3330
while rawIterator != endIterator {
3431
body(rawIterator.pointee)
3532
rawIterator = rawIterator.successor()
3633
}
34+
withExtendedLifetime(mutableSelf) {}
3735
}
3836
}
3937

@@ -51,7 +49,7 @@ extension RangeReplaceableCollection {
5149
where C.RawIterator.Pointee == Element {
5250

5351
self.init()
54-
forEachElement(of: c) { self.append($0) }
52+
c.forEach { self.append($0) }
5553
}
5654
}
5755

@@ -68,6 +66,6 @@ extension Set {
6866
where C.RawIterator.Pointee == Element {
6967

7068
self.init()
71-
forEachElement(of: c) { self.insert($0) }
69+
c.forEach { self.insert($0) }
7270
}
7371
}

0 commit comments

Comments
 (0)