Skip to content

[cxx-interop] Allow inserting elements into std::set from Swift #66764

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 1 commit into from
Jun 22, 2023

Conversation

egorzhdan
Copy link
Contributor

std::set::insert isn't exposed into Swift, because it returns an instance of an unsafe type.

This change adds a Swift overload of insert for std::set and std::unordered_set that has the return type identical to Swift.Set.insert: a tuple of (inserted: Bool, memberAfterInsert: Element).

rdar://111036912

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Jun 20, 2023
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

`std::set::insert` isn't exposed into Swift, because it returns an instance of an unsafe type.

This change adds a Swift overload of `insert` for `std::set` and `std::unordered_set` that has the return type identical to `Swift.Set.insert`: a tuple of `(inserted: Bool, memberAfterInsert: Element)`.

rdar://111036912
@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-set-insert branch from 3c71015 to b79b65c Compare June 20, 2023 15:57
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

public mutating func insert(
_ element: Element
) -> (inserted: Bool, memberAfterInsert: Element) {
let insertionResult = self.__insertUnsafe(element)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worried this could be destroyed before L68 when it's accessed.

Copy link
Contributor Author

@egorzhdan egorzhdan Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

insertionResult is a std::pair<iterator, bool>. The iterator itself won't be destroyed since we store it in let rawIterator. It would be safe if insertionResult is destroyed before L68.

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great. Just want to make sure there aren't any issues in func insert.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants