1111//===----------------------------------------------------------------------===//
1212
1313/// A C++ type that can be converted to a Swift collection.
14- public protocol CxxConvertibleToCollection {
14+ public protocol CxxConvertibleToCollection < Element> {
15+ associatedtype Element
1516 associatedtype RawIterator : UnsafeCxxInputIterator
17+ where RawIterator. Pointee == Element
1618
1719 /// Do not implement this function manually in Swift.
1820 mutating func __beginUnsafe( ) -> RawIterator
@@ -21,52 +23,51 @@ public protocol CxxConvertibleToCollection {
2123 mutating func __endUnsafe( ) -> RawIterator
2224}
2325
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 ( )
26+ extension CxxConvertibleToCollection {
27+ @inlinable
28+ internal func forEach( _ body: ( RawIterator . Pointee ) -> Void ) {
29+ var mutableSelf = self
30+ var rawIterator = mutableSelf. __beginUnsafe ( )
31+ let endIterator = mutableSelf. __endUnsafe ( )
3332 while rawIterator != endIterator {
3433 body ( rawIterator. pointee)
3534 rawIterator = rawIterator. successor ( )
3635 }
36+ withExtendedLifetime ( mutableSelf) { }
3737 }
3838}
3939
40- extension Array {
41- /// Creates an array containing the elements of a C++ container.
40+ extension RangeReplaceableCollection {
41+ /// Creates a collection containing the elements of a C++ container.
4242 ///
43- /// This initializes the array by copying every element of the C++ container.
43+ /// This initializes the collection by copying every element of the C++
44+ /// container.
4445 ///
4546 /// - Complexity: O(*n*), where *n* is the number of elements in the C++
4647 /// container when each element is copied in O(1). Note that this might not
4748 /// be true for certain C++ types, e.g. those with a custom copy
4849 /// constructor that performs additional logic.
49- public init < C: CxxConvertibleToCollection > ( _ c : C )
50+ public init < C: CxxConvertibleToCollection > ( _ elements : C )
5051 where C. RawIterator. Pointee == Element {
5152
5253 self . init ( )
53- forEachElement ( of : c ) { self . append ( $0) }
54+ elements . forEach { self . append ( $0) }
5455 }
5556}
5657
57- extension Set {
58- /// Creates an set containing the elements of a C++ container.
58+ extension SetAlgebra {
59+ /// Creates a set containing the elements of a C++ container.
5960 ///
6061 /// This initializes the set by copying every element of the C++ container.
6162 ///
6263 /// - Complexity: O(*n*), where *n* is the number of elements in the C++
6364 /// container when each element is copied in O(1). Note that this might not
6465 /// be true for certain C++ types, e.g. those with a custom copy
6566 /// constructor that performs additional logic.
66- public init < C: CxxConvertibleToCollection > ( _ c : C )
67+ public init < C: CxxConvertibleToCollection > ( _ elements : C )
6768 where C. RawIterator. Pointee == Element {
6869
6970 self . init ( )
70- forEachElement ( of : c ) { self . insert ( $0) }
71+ elements . forEach { self . insert ( $0) }
7172 }
7273}
0 commit comments