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 open classes #1975

Merged
merged 2 commits into from
Feb 10, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% include "Protocol.swift" %}

{%- call swift::docstring(obj, 0) %}
public class {{ impl_class_name }}:
open class {{ impl_class_name }}:
{%- for tm in obj.uniffi_traits() %}
{%- match tm %}
{%- when UniffiTrait::Display { fmt } %}
Expand All @@ -21,15 +21,29 @@ public class {{ impl_class_name }}:
{%- endmatch %}
{%- endfor %}
{{ protocol_name }} {
fileprivate let pointer: UnsafeMutableRawPointer
fileprivate let pointer: UnsafeMutableRawPointer!

/// Used to instantiate a [FFIObject] without an actual pointer, for fakes in tests, mostly.
public struct NoPointer {
public init() {}
}

// TODO: We'd like this to be `private` but for Swifty reasons,
// we can't implement `FfiConverter` without making this `required` and we can't
// make it `required` without making it `public`.
required init(unsafeFromRawPointer pointer: UnsafeMutableRawPointer) {
required public init(unsafeFromRawPointer pointer: UnsafeMutableRawPointer) {
self.pointer = pointer
}

/// This constructor can be used to instantiate a fake object.
/// - Parameter noPointer: Placeholder value so we can have a constructor separate from the default empty one that may be implemented for classes extending [FFIObject].
///
/// - Warning:
/// Any object instantiated with this constructor cannot be passed to an actual Rust-backed object. Since there isn't a backing [Pointer] the FFI lower functions will crash.
public init(noPointer: NoPointer) {
self.pointer = nil
}

public func uniffiClonePointer() -> UnsafeMutableRawPointer {
return try! rustCall { {{ obj.ffi_object_clone().name() }}(self.pointer, $0) }
}
Expand Down Expand Up @@ -59,7 +73,7 @@ public class {{ impl_class_name }}:
{% for meth in obj.methods() -%}
{%- if meth.is_async() %}
{%- call swift::docstring(meth, 4) %}
public func {{ meth.name()|fn_name }}({%- call swift::arg_list_decl(meth) -%}) async {% call swift::throws(meth) %}{% match meth.return_type() %}{% when Some with (return_type) %} -> {{ return_type|type_name }}{% when None %}{% endmatch %} {
open func {{ meth.name()|fn_name }}({%- call swift::arg_list_decl(meth) -%}) async {% call swift::throws(meth) %}{% match meth.return_type() %}{% when Some with (return_type) %} -> {{ return_type|type_name }}{% when None %}{% endmatch %} {
return {% call swift::try(meth) %} await uniffiRustCallAsync(
rustFutureFunc: {
{{ meth.ffi_func().name() }}(
Expand Down Expand Up @@ -94,15 +108,15 @@ public class {{ impl_class_name }}:

{%- when Some with (return_type) %}
{%- call swift::docstring(meth, 4) %}
public func {{ meth.name()|fn_name }}({% call swift::arg_list_decl(meth) %}) {% call swift::throws(meth) %} -> {{ return_type|type_name }} {
open func {{ meth.name()|fn_name }}({% call swift::arg_list_decl(meth) %}) {% call swift::throws(meth) %} -> {{ return_type|type_name }} {
return {% call swift::try(meth) %} {{ return_type|lift_fn }}(
{% call swift::to_ffi_call_with_prefix("self.uniffiClonePointer()", meth) %}
)
}

{%- when None %}
{%- call swift::docstring(meth, 4) %}
public func {{ meth.name()|fn_name }}({% call swift::arg_list_decl(meth) %}) {% call swift::throws(meth) %} {
open func {{ meth.name()|fn_name }}({% call swift::arg_list_decl(meth) %}) {% call swift::throws(meth) %} {
{% call swift::to_ffi_call_with_prefix("self.uniffiClonePointer()", meth) %}
}

Expand All @@ -113,13 +127,13 @@ public class {{ impl_class_name }}:
{%- for tm in obj.uniffi_traits() %}
{%- match tm %}
{%- when UniffiTrait::Display { fmt } %}
public var description: String {
open var description: String {
return {% call swift::try(fmt) %} {{ fmt.return_type().unwrap()|lift_fn }}(
{% call swift::to_ffi_call_with_prefix("self.uniffiClonePointer()", fmt) %}
)
}
{%- when UniffiTrait::Debug { fmt } %}
public var debugDescription: String {
open var debugDescription: String {
return {% call swift::try(fmt) %} {{ fmt.return_type().unwrap()|lift_fn }}(
{% call swift::to_ffi_call_with_prefix("self.uniffiClonePointer()", fmt) %}
)
Expand All @@ -131,7 +145,7 @@ public class {{ impl_class_name }}:
)
}
{%- when UniffiTrait::Hash { hash } %}
public func hash(into hasher: inout Hasher) {
open func hash(into hasher: inout Hasher) {
let val = {% call swift::try(hash) %} {{ hash.return_type().unwrap()|lift_fn }}(
{% call swift::to_ffi_call_with_prefix("self.uniffiClonePointer()", hash) %}
)
Expand Down