Skip to content

Commit 601c344

Browse files
stefanceriunimau
andauthored
Swift open classes (#1975)
Co-authored-by: Nicolas Mauri <[email protected]>
1 parent 545bcd7 commit 601c344

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

uniffi_bindgen/src/bindings/swift/templates/ObjectTemplate.swift

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{% include "Protocol.swift" %}
77

88
{%- call swift::docstring(obj, 0) %}
9-
public class {{ impl_class_name }}:
9+
open class {{ impl_class_name }}:
1010
{%- for tm in obj.uniffi_traits() %}
1111
{%- match tm %}
1212
{%- when UniffiTrait::Display { fmt } %}
@@ -21,15 +21,29 @@ public class {{ impl_class_name }}:
2121
{%- endmatch %}
2222
{%- endfor %}
2323
{{ protocol_name }} {
24-
fileprivate let pointer: UnsafeMutableRawPointer
24+
fileprivate let pointer: UnsafeMutableRawPointer!
25+
26+
/// Used to instantiate a [FFIObject] without an actual pointer, for fakes in tests, mostly.
27+
public struct NoPointer {
28+
public init() {}
29+
}
2530

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

38+
/// This constructor can be used to instantiate a fake object.
39+
/// - Parameter noPointer: Placeholder value so we can have a constructor separate from the default empty one that may be implemented for classes extending [FFIObject].
40+
///
41+
/// - Warning:
42+
/// 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.
43+
public init(noPointer: NoPointer) {
44+
self.pointer = nil
45+
}
46+
3347
public func uniffiClonePointer() -> UnsafeMutableRawPointer {
3448
return try! rustCall { {{ obj.ffi_object_clone().name() }}(self.pointer, $0) }
3549
}
@@ -59,7 +73,7 @@ public class {{ impl_class_name }}:
5973
{% for meth in obj.methods() -%}
6074
{%- if meth.is_async() %}
6175
{%- call swift::docstring(meth, 4) %}
62-
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 %} {
76+
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 %} {
6377
return {% call swift::try(meth) %} await uniffiRustCallAsync(
6478
rustFutureFunc: {
6579
{{ meth.ffi_func().name() }}(
@@ -94,15 +108,15 @@ public class {{ impl_class_name }}:
94108

95109
{%- when Some with (return_type) %}
96110
{%- call swift::docstring(meth, 4) %}
97-
public func {{ meth.name()|fn_name }}({% call swift::arg_list_decl(meth) %}) {% call swift::throws(meth) %} -> {{ return_type|type_name }} {
111+
open func {{ meth.name()|fn_name }}({% call swift::arg_list_decl(meth) %}) {% call swift::throws(meth) %} -> {{ return_type|type_name }} {
98112
return {% call swift::try(meth) %} {{ return_type|lift_fn }}(
99113
{% call swift::to_ffi_call_with_prefix("self.uniffiClonePointer()", meth) %}
100114
)
101115
}
102116

103117
{%- when None %}
104118
{%- call swift::docstring(meth, 4) %}
105-
public func {{ meth.name()|fn_name }}({% call swift::arg_list_decl(meth) %}) {% call swift::throws(meth) %} {
119+
open func {{ meth.name()|fn_name }}({% call swift::arg_list_decl(meth) %}) {% call swift::throws(meth) %} {
106120
{% call swift::to_ffi_call_with_prefix("self.uniffiClonePointer()", meth) %}
107121
}
108122

@@ -113,13 +127,13 @@ public class {{ impl_class_name }}:
113127
{%- for tm in obj.uniffi_traits() %}
114128
{%- match tm %}
115129
{%- when UniffiTrait::Display { fmt } %}
116-
public var description: String {
130+
open var description: String {
117131
return {% call swift::try(fmt) %} {{ fmt.return_type().unwrap()|lift_fn }}(
118132
{% call swift::to_ffi_call_with_prefix("self.uniffiClonePointer()", fmt) %}
119133
)
120134
}
121135
{%- when UniffiTrait::Debug { fmt } %}
122-
public var debugDescription: String {
136+
open var debugDescription: String {
123137
return {% call swift::try(fmt) %} {{ fmt.return_type().unwrap()|lift_fn }}(
124138
{% call swift::to_ffi_call_with_prefix("self.uniffiClonePointer()", fmt) %}
125139
)
@@ -131,7 +145,7 @@ public class {{ impl_class_name }}:
131145
)
132146
}
133147
{%- when UniffiTrait::Hash { hash } %}
134-
public func hash(into hasher: inout Hasher) {
148+
open func hash(into hasher: inout Hasher) {
135149
let val = {% call swift::try(hash) %} {{ hash.return_type().unwrap()|lift_fn }}(
136150
{% call swift::to_ffi_call_with_prefix("self.uniffiClonePointer()", hash) %}
137151
)

0 commit comments

Comments
 (0)