-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Add CxxRandomAccessCollection
protocol
#61554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2022 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 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 CxxRandomAccessCollection: CxxSequence, RandomAccessCollection { | ||
override associatedtype RawIterator: UnsafeCxxRandomAccessIterator | ||
override associatedtype Element = RawIterator.Pointee | ||
|
||
/// Do not implement this function manually in Swift. | ||
func __beginUnsafe() -> RawIterator | ||
|
||
/// Do not implement this function manually in Swift. | ||
func __endUnsafe() -> RawIterator | ||
} | ||
|
||
extension CxxRandomAccessCollection { | ||
@inlinable | ||
public var startIndex: Int { | ||
return 0 | ||
} | ||
|
||
@inlinable | ||
public var endIndex: Int { | ||
egorzhdan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return count | ||
} | ||
|
||
@inlinable | ||
public var count: Int { | ||
egorzhdan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Int(__endUnsafe() - __beginUnsafe()) | ||
} | ||
|
||
/// A C++ implementation of the subscript might be more performant. This | ||
/// overload should only be used if the C++ type does not define `operator[]`. | ||
@inlinable | ||
public subscript(_ index: Int) -> Element { | ||
egorzhdan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_read { | ||
// Not using CxxIterator here to avoid making a copy of the collection. | ||
var rawIterator = __beginUnsafe() | ||
rawIterator += RawIterator.Distance(index) | ||
yield rawIterator.pointee as! Element | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
test/Interop/Cxx/stdlib/overlay/Inputs/custom-collection.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef TEST_INTEROP_CXX_STDLIB_INPUTS_CUSTOM_COLLECTION_H | ||
#define TEST_INTEROP_CXX_STDLIB_INPUTS_CUSTOM_COLLECTION_H | ||
|
||
#include "custom-iterator.h" | ||
#include <iterator> | ||
|
||
struct SimpleCollectionNoSubscript { | ||
private: | ||
int x[5] = {1, 2, 3, 4, 5}; | ||
|
||
public: | ||
using iterator = ConstRACIterator; | ||
|
||
iterator begin() const { return iterator(*x); } | ||
iterator end() const { return iterator(*x + 5); } | ||
}; | ||
|
||
struct SimpleCollectionReadOnly { | ||
private: | ||
int x[5] = {1, 2, 3, 4, 5}; | ||
|
||
public: | ||
using iterator = ConstRACIteratorRefPlusEq; | ||
|
||
iterator begin() const { return iterator(*x); } | ||
iterator end() const { return iterator(*x + 5); } | ||
|
||
const int& operator[](int index) const { return x[index]; } | ||
}; | ||
|
||
#endif // TEST_INTEROP_CXX_STDLIB_INPUTS_CUSTOM_COLLECTION_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module CustomSequence { | ||
header "custom-iterator.h" // TODO: extract into another module | ||
header "custom-sequence.h" | ||
header "custom-collection.h" | ||
requires cplusplus | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop) | ||
// | ||
// REQUIRES: executable_test | ||
// REQUIRES: OS=macosx || OS=linux-gnu | ||
|
||
import StdlibUnittest | ||
import CustomSequence | ||
import Cxx | ||
|
||
var CxxCollectionTestSuite = TestSuite("CxxCollection") | ||
|
||
// === SimpleCollectionNoSubscript === | ||
|
||
extension SimpleCollectionNoSubscript.iterator : UnsafeCxxRandomAccessIterator { | ||
public typealias Distance = difference_type | ||
} | ||
extension SimpleCollectionNoSubscript : CxxRandomAccessCollection { | ||
} | ||
|
||
CxxCollectionTestSuite.test("SimpleCollectionNoSubscript as Swift.Collection") { | ||
let c = SimpleCollectionNoSubscript() | ||
expectEqual(c.first, 1) | ||
expectEqual(c.last, 5) | ||
} | ||
|
||
// === SimpleCollectionReadOnly === | ||
|
||
extension SimpleCollectionReadOnly.iterator : UnsafeCxxRandomAccessIterator { | ||
public typealias Distance = difference_type | ||
} | ||
extension SimpleCollectionReadOnly : CxxRandomAccessCollection { | ||
} | ||
|
||
CxxCollectionTestSuite.test("SimpleCollectionReadOnly as Swift.Collection") { | ||
let c = SimpleCollectionReadOnly() | ||
expectEqual(c.first, 1) | ||
expectEqual(c.last, 5) | ||
} | ||
|
||
// === SimpleArrayWrapper === | ||
|
||
extension SimpleArrayWrapper : CxxRandomAccessCollection { | ||
} | ||
|
||
CxxCollectionTestSuite.test("SimpleArrayWrapper as Swift.Collection") { | ||
let c = SimpleArrayWrapper() | ||
expectEqual(c.first, 10) | ||
expectEqual(c.last, 50) | ||
} | ||
|
||
runAllTests() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.