Files
swift-mirror/stdlib/public/Cxx/UnsafeCxxIterators.swift
Egor Zhdan 3a200deee9 [cxx-interop] Add UnsafeCxxContiguousIterator & UnsafeCxxMutableContiguousIterator protocols
This adds a pair of Swift protocols that represents C++ iterator types conforming to `std::contiguous_iterator_tag` requirements. These are random access iterators that guarantee that the values are stored in consequent memory addresses.

This will be used to optimize usage of C++ containers such as `std::vector` from Swift, for instance, by providing an overload of `withContiguousStorageIfAvailable` for contiguous containers.

rdar://137877849
2024-10-15 14:30:09 +01:00

102 lines
3.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// Bridged C++ iterator that allows to traverse the elements of a sequence
/// using a for-in loop.
///
/// Mostly useful for conforming a type to the `CxxSequence` protocol and should
/// not generally be used directly.
///
/// - SeeAlso: https://en.cppreference.com/w/cpp/named_req/InputIterator
public protocol UnsafeCxxInputIterator: Equatable {
associatedtype Pointee
/// Returns the unwrapped result of C++ `operator*()`.
///
/// Generally, Swift creates this property automatically for C++ types that
/// define `operator*()`.
@_borrowed
var pointee: Pointee { get }
/// Returns an iterator pointing to the next item in the sequence.
///
/// Generally, Swift creates this property automatically for C++ types that
/// define pre-increment `operator++()`.
func successor() -> Self
}
extension UnsafePointer: UnsafeCxxInputIterator {}
extension UnsafeMutablePointer: UnsafeCxxInputIterator {}
extension Optional: UnsafeCxxInputIterator where Wrapped: UnsafeCxxInputIterator {
public typealias Pointee = Wrapped.Pointee
@inlinable
public var pointee: Pointee {
if let value = self {
return value.pointee
}
fatalError("Could not dereference nullptr")
}
@inlinable
public func successor() -> Self {
if let value = self {
return value.successor()
}
fatalError("Could not increment nullptr")
}
}
public protocol UnsafeCxxMutableInputIterator: UnsafeCxxInputIterator {
@_borrowed
override var pointee: Pointee { get set }
}
extension UnsafeMutablePointer: UnsafeCxxMutableInputIterator {}
/// Bridged C++ iterator that allows computing the distance between two of its
/// instances, and advancing an instance by a given number of elements.
///
/// Mostly useful for conforming a type to the `CxxRandomAccessCollection`
/// protocol and should not generally be used directly.
///
/// - SeeAlso: https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator
public protocol UnsafeCxxRandomAccessIterator: UnsafeCxxInputIterator {
associatedtype Distance: BinaryInteger
static func -(lhs: Self, rhs: Self) -> Distance
static func +=(lhs: inout Self, rhs: Distance)
}
extension UnsafePointer: UnsafeCxxRandomAccessIterator {}
extension UnsafeMutablePointer: UnsafeCxxRandomAccessIterator {}
public protocol UnsafeCxxMutableRandomAccessIterator:
UnsafeCxxRandomAccessIterator, UnsafeCxxMutableInputIterator {}
extension UnsafeMutablePointer: UnsafeCxxMutableRandomAccessIterator {}
/// Bridged C++ iterator that allows traversing elements of a random access
/// collection that are stored in contiguous memory segments.
///
/// Mostly useful for optimizing operations with containers that conform to
/// `CxxRandomAccessCollection` and should not generally be used directly.
///
/// - SeeAlso: https://en.cppreference.com/w/cpp/named_req/ContiguousIterator
public protocol UnsafeCxxContiguousIterator: UnsafeCxxRandomAccessIterator {}
public protocol UnsafeCxxMutableContiguousIterator:
UnsafeCxxContiguousIterator, UnsafeCxxMutableRandomAccessIterator {}