Skip to content
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

Swift classes can inherit from traits #2363

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ We have [detailed upgrade notes](https://mozilla.github.io/uniffi-rs/next/Upgrad
[Detailed upgrade notes](https://mozilla.github.io/uniffi-rs/next/Upgrading.html)
### What's new?

- Kotlin: Proc-macros exporting an `impl Trait for Struct` block now has a class inheritance
hierarcy to reflect that. [#2297](https://github.com/mozilla/uniffi-rs/pull/2297)
- Kotlin and Swift: Proc-macros exporting an `impl Trait for Struct` block now has a class inheritance
hierarcy to reflect that.
[#2297](https://github.com/mozilla/uniffi-rs/pull/2297), [#2363](https://github.com/mozilla/uniffi-rs/pull/2363)

- Removed the `log` dependency and logging statements about FFI calls. These were not really useful
to consumers and could have high overhead when lots of FFI calls are made. Instead, the
Expand Down
21 changes: 21 additions & 0 deletions fixtures/coverall/tests/bindings/test_coverall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,27 @@ do {
traits[0].setParent(parent: nil)
}

// A struct which implements the node trait.
do {
let n = Node(name: "node")
assert(String(describing: n).starts(with: "Node { name: Some(\"node\"), parent: Mutex { "))
assert(n.getParent()?.name() == "via node")

n.setParent(parent: n.getParent())
// doubly-wrapped :(
// Get: "Some(UniFFICallbackHandlerNodeTrait { handle: 19 })"
// Want: Like the Rust node above.
// debugPrint("parent \(n.describeParent())")

let rustParent = Node(name: "parent")
n.setParent(parent: rustParent)
assert(n.getParent()?.name() == "parent")

let swiftParent = SwiftNode()
rustParent.setParent(parent: swiftParent)
assert(ancestorNames(node: n) == ["parent", "node-swift"])
}

// Test round tripping
do {
let rustGetters = makeRustGetters()
Expand Down
21 changes: 21 additions & 0 deletions uniffi_bindgen/src/bindings/swift/gen_swift/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,27 @@ impl Config {
}
}

// Given a trait, work out what the protocol name we generate for it.
// This differs based on whether the trait supports foreign impls (ie,
// whether is has a "callback interface".
fn trait_protocol_name(ci: &ComponentInterface, name: &str) -> Result<String> {
let (obj_name, has_callback_interface) = match ci.get_object_definition(name) {
Some(obj) => (obj.name(), obj.has_callback_interface()),
None => (
ci.get_callback_interface_definition(name)
.ok_or_else(|| anyhow::anyhow!("no interface {}", name))?
.name(),
true,
),
};
let class_name = SwiftCodeOracle.class_name(obj_name);
if has_callback_interface {
Ok(class_name)
} else {
Ok(format!("{class_name}Protocol"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a good reason why this has "Protocol" appended? IIUC, this is a Rust trait without callback interface support, so Swift can't implement it. In that case, it seems like protocol should get {class_name} which feels like a nicer name, and the class that implements the protocol should get something like{class_name}Impl. I think that's how the other languages handle it.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is exactly the same as kotlin which has "Interface" appended

Note that this change doesn't change the hierarchy of any existing objects, it just controls which of the 2 generated classes is used in the inheritance chain.

I'm inclined to agree that the existing hierarchy is odd in these cases, I fear that trying to change that here will accidentally introduce new regressions.

Is this also your understanding - ie, that this patch doesn't change the class hierarchy for existing types, and that doing so is something we'd need to be very careful about?

Copy link
Contributor

@bendk bendk Dec 23, 2024

Choose a reason for hiding this comment

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

Is this also your understanding - ie, that this patch doesn't change the class hierarchy for existing types

Yes agreed.

..and that doing so is something we'd need to be very careful about?

Maybe I have a slight difference of opinion here. I think it could be okay to make the change, since most end-users aren't going to see a difference. If they store a Foo and only use it to call methods from the protocol, it shouldn't matter if Foo is a concrete type that implements the protocol or the protocol itself.

I agree we should be careful, but maybe I'd remove the "very" part.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, I think I got confused and believed that we had already made the change for other languages. If Kotlin works this way, then I think Swift should too. If we decide to change them, let's change both at the same time.

}
}

/// Generate UniFFI component bindings for Swift, as strings in memory.
pub fn generate_bindings(config: &Config, ci: &ComponentInterface) -> Result<Bindings> {
let header = BridgingHeader::new(config, ci)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ open class {{ impl_class_name }}:
{%- if is_error %}
Swift.Error,
{% endif %}
{{ protocol_name }} {
{%- for t in obj.trait_impls() %}
{{ self::trait_protocol_name(ci, t.trait_name)? }},
{% endfor %}
{{ protocol_name }}
{
fileprivate let pointer: UnsafeMutableRawPointer!

/// Used to instantiate a [FFIObject] without an actual pointer, for fakes in tests, mostly.
Expand Down