-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Add CxxSequence
protocol to the stdlib overlay
#59492
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
Conversation
/// Returns the unwrapped result of C++ `operator*()`. | ||
/// | ||
/// Generally, Swift creates this property automatically for C++ types that define `operator*()`. | ||
var pointee: Pointee { get } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name chosen to match UnsafePointer.pointee
/UnsafeMutablePointer.pointee
and the synthesized properties for operator*()
.
var pointee: Pointee { get } | ||
|
||
/// Returns an iterator pointing to the next item in the sequence. | ||
func successor() -> Self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name chosen to match UnsafePointer.successor()
/UnsafeMutablePointer.successor()
: I think we should try to avoid adding public methods/properties to Swift stdlib classes like UnsafePointer
as much as possible, and reusing the existing name allows us to define an empty extension with no public methods.
@swift-ci please smoke test |
test/Interop/Cxx/stdlib/overlay/custom-sequence-typechecker.swift
Outdated
Show resolved
Hide resolved
// REQUIRES: OS=macosx || OS=linux-gnu | ||
|
||
import CustomSequence | ||
import std |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test for std.string and/or std.vector? (Maybe in another file.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or better yet: update the existing string/vector tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeap, added a test for std::string
. std::vector
is more tricky because it's a template type.
|
||
for item in seq { | ||
print(item) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simply executing the code without validating the results doesn't seem like an adequate test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a typechecker test, we're not actually executing any code here. This test makes sure that the typechecker doesn't emit any unexpected warnings or remarks.
58ae64c
to
dd195cd
Compare
@swift-ci please smoke test |
|
||
extension UnsafePointer: UnsafeCxxInputIterator {} | ||
|
||
extension UnsafeMutablePointer: UnsafeCxxInputIterator {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you conform Optional<UnsafePointer>
to UnsafeCxxInputIterator
? That way you can allow nullable pointers returned from begin
/end
, which some empty containers can return.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great idea, thanks!
That currently means that we have to add new public members to Optional<UnsafePointer>
(pointee
& successor()
), which I think would be best to avoid, since users might start relying on them in non-interop code, but I'll think about how to hide those members a bit later. We can probably fix that by introducing a separate private iterator protocol.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm: if c++ interop is not enabled, people won't get the overlay, therefore won't see these members/conformances?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is correct, these conformances will only be visible when C++ interop is enabled.
50cb704
to
4203720
Compare
This change adds basic helper protocols and structs that are going to be used for making C++ sequences and collection safe and Swifty by adding conformances to `Swift.Sequence`, `Swift.Collection`, etc. This is not meant to be a final design.
4203720
to
6754c3c
Compare
@swift-ci please smoke test |
This change adds basic helper protocols and structs that are going to be used for making C++ sequences and collection Swifty by adding conformances to
Swift.Sequence
,Swift.Collection
, etc.This is not meant to be a final design.