Skip to content

Commit 7a12003

Browse files
authored
Merge pull request swiftlang#63570 from apple/egorzhdan/nfc-cxx-iterators
[cxx-interop] NFC: move unsafe iterator types to a separate file
2 parents 06bfef2 + 71cc1bb commit 7a12003

File tree

4 files changed

+77
-65
lines changed

4 files changed

+77
-65
lines changed

stdlib/public/Cxx/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_swift_target_library(swiftCxx ${SWIFT_CXX_LIBRARY_KIND} NO_LINK_NAME IS_STDL
1010
CxxSet.swift
1111
CxxRandomAccessCollection.swift
1212
CxxSequence.swift
13+
UnsafeCxxIterators.swift
1314

1415
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
1516
-Xfrontend -enable-experimental-cxx-interop

stdlib/public/Cxx/CxxRandomAccessCollection.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// Bridged C++ iterator that allows computing the distance between two of its
14-
/// instances, and advancing an instance by a given number of elements.
15-
///
16-
/// Mostly useful for conforming a type to the `CxxRandomAccessCollection`
17-
/// protocol and should not generally be used directly.
18-
///
19-
/// - SeeAlso: https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator
20-
public protocol UnsafeCxxRandomAccessIterator: UnsafeCxxInputIterator {
21-
associatedtype Distance: BinaryInteger
22-
23-
static func -(lhs: Self, rhs: Self) -> Distance
24-
static func +=(lhs: inout Self, rhs: Distance)
25-
}
26-
27-
extension UnsafePointer: UnsafeCxxRandomAccessIterator {}
28-
29-
extension UnsafeMutablePointer: UnsafeCxxRandomAccessIterator {}
30-
3113
public protocol CxxRandomAccessCollection<Element>: CxxSequence, RandomAccessCollection {
3214
override associatedtype Element
3315
override associatedtype RawIterator: UnsafeCxxRandomAccessIterator

stdlib/public/Cxx/CxxSequence.swift

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// Bridged C++ iterator that allows to traverse the elements of a sequence
14-
/// using a for-in loop.
15-
///
16-
/// Mostly useful for conforming a type to the `CxxSequence` protocol and should
17-
/// not generally be used directly.
18-
///
19-
/// - SeeAlso: https://en.cppreference.com/w/cpp/named_req/InputIterator
20-
public protocol UnsafeCxxInputIterator: Equatable {
21-
associatedtype Pointee
22-
23-
/// Returns the unwrapped result of C++ `operator*()`.
24-
///
25-
/// Generally, Swift creates this property automatically for C++ types that
26-
/// define `operator*()`.
27-
var pointee: Pointee { get }
28-
29-
/// Returns an iterator pointing to the next item in the sequence.
30-
///
31-
/// Generally, Swift creates this property automatically for C++ types that
32-
/// define pre-increment `operator++()`.
33-
func successor() -> Self
34-
}
35-
36-
extension UnsafePointer: UnsafeCxxInputIterator {}
37-
38-
extension UnsafeMutablePointer: UnsafeCxxInputIterator {}
39-
40-
extension Optional: UnsafeCxxInputIterator where Wrapped: UnsafeCxxInputIterator {
41-
public typealias Pointee = Wrapped.Pointee
42-
43-
@inlinable
44-
public var pointee: Pointee {
45-
if let value = self {
46-
return value.pointee
47-
}
48-
fatalError("Could not dereference nullptr")
49-
}
50-
51-
@inlinable
52-
public func successor() -> Self {
53-
if let value = self {
54-
return value.successor()
55-
}
56-
fatalError("Could not increment nullptr")
57-
}
58-
}
59-
6013
/// Use this protocol to conform custom C++ sequence types to Swift's `Sequence`
6114
/// protocol like this:
6215
///
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// Bridged C++ iterator that allows to traverse the elements of a sequence
14+
/// using a for-in loop.
15+
///
16+
/// Mostly useful for conforming a type to the `CxxSequence` protocol and should
17+
/// not generally be used directly.
18+
///
19+
/// - SeeAlso: https://en.cppreference.com/w/cpp/named_req/InputIterator
20+
public protocol UnsafeCxxInputIterator: Equatable {
21+
associatedtype Pointee
22+
23+
/// Returns the unwrapped result of C++ `operator*()`.
24+
///
25+
/// Generally, Swift creates this property automatically for C++ types that
26+
/// define `operator*()`.
27+
var pointee: Pointee { get }
28+
29+
/// Returns an iterator pointing to the next item in the sequence.
30+
///
31+
/// Generally, Swift creates this property automatically for C++ types that
32+
/// define pre-increment `operator++()`.
33+
func successor() -> Self
34+
}
35+
36+
extension UnsafePointer: UnsafeCxxInputIterator {}
37+
38+
extension UnsafeMutablePointer: UnsafeCxxInputIterator {}
39+
40+
extension Optional: UnsafeCxxInputIterator where Wrapped: UnsafeCxxInputIterator {
41+
public typealias Pointee = Wrapped.Pointee
42+
43+
@inlinable
44+
public var pointee: Pointee {
45+
if let value = self {
46+
return value.pointee
47+
}
48+
fatalError("Could not dereference nullptr")
49+
}
50+
51+
@inlinable
52+
public func successor() -> Self {
53+
if let value = self {
54+
return value.successor()
55+
}
56+
fatalError("Could not increment nullptr")
57+
}
58+
}
59+
60+
/// Bridged C++ iterator that allows computing the distance between two of its
61+
/// instances, and advancing an instance by a given number of elements.
62+
///
63+
/// Mostly useful for conforming a type to the `CxxRandomAccessCollection`
64+
/// protocol and should not generally be used directly.
65+
///
66+
/// - SeeAlso: https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator
67+
public protocol UnsafeCxxRandomAccessIterator: UnsafeCxxInputIterator {
68+
associatedtype Distance: BinaryInteger
69+
70+
static func -(lhs: Self, rhs: Self) -> Distance
71+
static func +=(lhs: inout Self, rhs: Distance)
72+
}
73+
74+
extension UnsafePointer: UnsafeCxxRandomAccessIterator {}
75+
76+
extension UnsafeMutablePointer: UnsafeCxxRandomAccessIterator {}

0 commit comments

Comments
 (0)