| 1 |
{%- let obj = ci.get_object_definition(name).unwrap() %} |
| 2 |
{%- let (protocol_name, impl_class_name) = obj|object_names %} |
| 3 |
{%- let methods = obj.methods() %} |
| 4 |
{%- let protocol_docstring = obj.docstring() %} |
| 5 |
|
| 6 |
{%- let is_error = ci.is_name_used_as_error(name) %} |
| 7 |
|
| 8 |
{% include "Protocol.swift" %} |
| 9 |
|
| 10 |
{%- call swift::docstring(obj, 0) %} |
| 11 |
open class {{ impl_class_name }}: {{ protocol_name }}, @unchecked Sendable { |
| 12 |
fileprivate let pointer: UnsafeMutableRawPointer! |
| 13 |
|
| 14 |
/// Used to instantiate a [FFIObject] without an actual pointer, for fakes in tests, mostly. |
| 15 |
#if swift(>=5.8) |
| 16 |
@_documentation(visibility: private) |
| 17 |
#endif |
| 18 |
public struct NoPointer { |
| 19 |
public init() {} |
| 20 |
} |
| 21 |
|
| 22 |
// TODO: We'd like this to be `private` but for Swifty reasons, |
| 23 |
// we can't implement `FfiConverter` without making this `required` and we can't |
| 24 |
// make it `required` without making it `public`. |
| 25 |
#if swift(>=5.8) |
| 26 |
@_documentation(visibility: private) |
| 27 |
#endif |
| 28 |
required public init(unsafeFromRawPointer pointer: UnsafeMutableRawPointer) { |
| 29 |
self.pointer = pointer |
| 30 |
} |
| 31 |
|
| 32 |
// This constructor can be used to instantiate a fake object. |
| 33 |
// - Parameter noPointer: Placeholder value so we can have a constructor separate from the default empty one that may be implemented for classes extending [FFIObject]. |
| 34 |
// |
| 35 |
// - Warning: |
| 36 |
// 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. |
| 37 |
#if swift(>=5.8) |
| 38 |
@_documentation(visibility: private) |
| 39 |
#endif |
| 40 |
public init(noPointer: NoPointer) { |
| 41 |
self.pointer = nil |
| 42 |
} |
| 43 |
|
| 44 |
#if swift(>=5.8) |
| 45 |
@_documentation(visibility: private) |
| 46 |
#endif |
| 47 |
public func uniffiClonePointer() -> UnsafeMutableRawPointer { |
| 48 |
return try! rustCall { {{ obj.ffi_object_clone().name() }}(self.pointer, $0) } |
| 49 |
} |
| 50 |
|
| 51 |
{%- match obj.primary_constructor() %} |
| 52 |
{%- when Some(cons) %} |
| 53 |
{%- call swift::ctor_decl(cons, 4) %} |
| 54 |
{%- when None %} |
| 55 |
// No primary constructor declared for this class. |
| 56 |
{%- endmatch %} |
| 57 |
|
| 58 |
deinit { |
| 59 |
guard let pointer = pointer else { |
| 60 |
return |
| 61 |
} |
| 62 |
|
| 63 |
try! rustCall { {{ obj.ffi_object_free().name() }}(pointer, $0) } |
| 64 |
} |
| 65 |
|
| 66 |
{% for cons in obj.alternate_constructors() %} |
| 67 |
{%- call swift::func_decl("public static func", cons, 4) %} |
| 68 |
{% endfor %} |
| 69 |
|
| 70 |
{% for meth in obj.methods() -%} |
| 71 |
{%- call swift::func_decl("open func", meth, 4) %} |
| 72 |
{% endfor %} |
| 73 |
|
| 74 |
{%- for tm in obj.uniffi_traits() %} |
| 75 |
{%- match tm %} |
| 76 |
{%- when UniffiTrait::Display { fmt } %} |
| 77 |
open var description: String { |
| 78 |
return {% call swift::is_try(fmt) %} {{ fmt.return_type().unwrap()|lift_fn }}( |
| 79 |
{% call swift::to_ffi_call(fmt) %} |
| 80 |
) |
| 81 |
} |
| 82 |
{%- when UniffiTrait::Debug { fmt } %} |
| 83 |
open var debugDescription: String { |
| 84 |
return {% call swift::is_try(fmt) %} {{ fmt.return_type().unwrap()|lift_fn }}( |
| 85 |
{% call swift::to_ffi_call(fmt) %} |
| 86 |
) |
| 87 |
} |
| 88 |
{%- when UniffiTrait::Eq { eq, ne } %} |
| 89 |
public static func == (self: {{ impl_class_name }}, other: {{ impl_class_name }}) -> Bool { |
| 90 |
return {% call swift::is_try(eq) %} {{ eq.return_type().unwrap()|lift_fn }}( |
| 91 |
{% call swift::to_ffi_call(eq) %} |
| 92 |
) |
| 93 |
} |
| 94 |
{%- when UniffiTrait::Hash { hash } %} |
| 95 |
open func hash(into hasher: inout Hasher) { |
| 96 |
let val = {% call swift::is_try(hash) %} {{ hash.return_type().unwrap()|lift_fn }}( |
| 97 |
{% call swift::to_ffi_call(hash) %} |
| 98 |
) |
| 99 |
hasher.combine(val) |
| 100 |
} |
| 101 |
{%- else %} |
| 102 |
{%- endmatch %} |
| 103 |
{%- endfor %} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
{%- if obj.has_callback_interface() %} |
| 108 |
{%- let callback_handler = format!("uniffiCallbackInterface{}", name) %} |
| 109 |
{%- let callback_init = format!("uniffiCallbackInit{}", name) %} |
| 110 |
{%- let vtable = obj.vtable().expect("trait interface should have a vtable") %} |
| 111 |
{%- let vtable_methods = obj.vtable_methods() %} |
| 112 |
{%- let ffi_init_callback = obj.ffi_init_callback() %} |
| 113 |
{% include "CallbackInterfaceImpl.swift" %} |
| 114 |
{%- endif %} |
| 115 |
|
| 116 |
{%- for tm in obj.uniffi_traits() %} |
| 117 |
{%- match tm %} |
| 118 |
{%- when UniffiTrait::Display { fmt } %} |
| 119 |
extension {{ impl_class_name }}: CustomStringConvertible {} |
| 120 |
{%- when UniffiTrait::Debug { fmt } %} |
| 121 |
extension {{ impl_class_name }}: CustomDebugStringConvertible {} |
| 122 |
{%- when UniffiTrait::Eq { eq, ne } %} |
| 123 |
extension {{ impl_class_name }}: Equatable {} |
| 124 |
{%- when UniffiTrait::Hash { hash } %} |
| 125 |
extension {{ impl_class_name }}: Hashable {} |
| 126 |
{%- else %} |
| 127 |
{%- endmatch %} |
| 128 |
{%- endfor %} |
| 129 |
|
| 130 |
{%- if is_error %} |
| 131 |
extension {{ impl_class_name }}: Swift.Error {} |
| 132 |
{% endif %} |
| 133 |
|
| 134 |
{%- for t in obj.trait_impls() %} |
| 135 |
extension {{impl_class_name}}: {{ self::trait_protocol_name(ci, t.trait_name)? }} {} |
| 136 |
{% endfor %} |
| 137 |
|
| 138 |
|
| 139 |
#if swift(>=5.8) |
| 140 |
@_documentation(visibility: private) |
| 141 |
#endif |
| 142 |
public struct {{ ffi_converter_name }}: FfiConverter { |
| 143 |
{%- if obj.has_callback_interface() %} |
| 144 |
fileprivate static let handleMap = UniffiHandleMap<{{ type_name }}>() |
| 145 |
{%- endif %} |
| 146 |
|
| 147 |
typealias FfiType = UnsafeMutableRawPointer |
| 148 |
typealias SwiftType = {{ type_name }} |
| 149 |
|
| 150 |
public static func lift(_ pointer: UnsafeMutableRawPointer) throws -> {{ type_name }} { |
| 151 |
return {{ impl_class_name }}(unsafeFromRawPointer: pointer) |
| 152 |
} |
| 153 |
|
| 154 |
public static func lower(_ value: {{ type_name }}) -> UnsafeMutableRawPointer { |
| 155 |
{%- if obj.has_callback_interface() %} |
| 156 |
guard let ptr = UnsafeMutableRawPointer(bitPattern: UInt(truncatingIfNeeded: handleMap.insert(obj: value))) else { |
| 157 |
fatalError("Cast to UnsafeMutableRawPointer failed") |
| 158 |
} |
| 159 |
return ptr |
| 160 |
{%- else %} |
| 161 |
return value.uniffiClonePointer() |
| 162 |
{%- endif %} |
| 163 |
} |
| 164 |
|
| 165 |
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> {{ type_name }} { |
| 166 |
let v: UInt64 = try readInt(&buf) |
| 167 |
// The Rust code won't compile if a pointer won't fit in a UInt64. |
| 168 |
// We have to go via `UInt` because that's the thing that's the size of a pointer. |
| 169 |
let ptr = UnsafeMutableRawPointer(bitPattern: UInt(truncatingIfNeeded: v)) |
| 170 |
if (ptr == nil) { |
| 171 |
throw UniffiInternalError.unexpectedNullPointer |
| 172 |
} |
| 173 |
return try lift(ptr!) |
| 174 |
} |
| 175 |
|
| 176 |
public static func write(_ value: {{ type_name }}, into buf: inout [UInt8]) { |
| 177 |
// This fiddling is because `Int` is the thing that's the same size as a pointer. |
| 178 |
// The Rust code won't compile if a pointer won't fit in a `UInt64`. |
| 179 |
writeInt(&buf, UInt64(bitPattern: Int64(Int(bitPattern: lower(value))))) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
{# |
| 184 |
We always write these public functions just in case the object is used as |
| 185 |
an external type by another crate. |
| 186 |
#} |
| 187 |
#if swift(>=5.8) |
| 188 |
@_documentation(visibility: private) |
| 189 |
#endif |
| 190 |
public func {{ ffi_converter_name }}_lift(_ pointer: UnsafeMutableRawPointer) throws -> {{ type_name }} { |
| 191 |
return try {{ ffi_converter_name }}.lift(pointer) |
| 192 |
} |
| 193 |
|
| 194 |
#if swift(>=5.8) |
| 195 |
@_documentation(visibility: private) |
| 196 |
#endif |
| 197 |
public func {{ ffi_converter_name }}_lower(_ value: {{ type_name }}) -> UnsafeMutableRawPointer { |
| 198 |
return {{ ffi_converter_name }}.lower(value) |
| 199 |
} |
| 200 |
|
| 201 |
{# Objects as error #} |
| 202 |
{%- if is_error %} |
| 203 |
|
| 204 |
{% if !config.omit_localized_error_conformance() %} |
| 205 |
extension {{ type_name }}: Foundation.LocalizedError { |
| 206 |
public var errorDescription: String? { |
| 207 |
String(reflecting: self) |
| 208 |
} |
| 209 |
} |
| 210 |
{% endif %} |
| 211 |
|
| 212 |
{# Due to some mismatches in the ffi converter mechanisms, errors are a RustBuffer holding a pointer #} |
| 213 |
#if swift(>=5.8) |
| 214 |
@_documentation(visibility: private) |
| 215 |
#endif |
| 216 |
public struct {{ ffi_converter_name }}__as_error: FfiConverterRustBuffer { |
| 217 |
public static func lift(_ buf: RustBuffer) throws -> {{ type_name }} { |
| 218 |
var reader = createReader(data: Data(rustBuffer: buf)) |
| 219 |
return try {{ ffi_converter_name }}.read(from: &reader) |
| 220 |
} |
| 221 |
|
| 222 |
public static func lower(_ value: {{ type_name }}) -> RustBuffer { |
| 223 |
fatalError("not implemented") |
| 224 |
} |
| 225 |
|
| 226 |
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> {{ type_name }} { |
| 227 |
fatalError("not implemented") |
| 228 |
} |
| 229 |
|
| 230 |
public static func write(_ value: {{ type_name }}, into buf: inout [UInt8]) { |
| 231 |
fatalError("not implemented") |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
{# Error FFI converters also need these public functions. #} |
| 236 |
#if swift(>=5.8) |
| 237 |
@_documentation(visibility: private) |
| 238 |
#endif |
| 239 |
public func {{ ffi_converter_name }}__as_error_lift(_ buf: RustBuffer) throws -> {{ type_name }} { |
| 240 |
return try {{ ffi_converter_name }}__as_error.lift(buf) |
| 241 |
} |
| 242 |
|
| 243 |
#if swift(>=5.8) |
| 244 |
@_documentation(visibility: private) |
| 245 |
#endif |
| 246 |
public func {{ ffi_converter_name }}__as_error_lower(_ value: {{ type_name }}) -> RustBuffer { |
| 247 |
return {{ ffi_converter_name }}__as_error.lower(value) |
| 248 |
} |
| 249 |
|
| 250 |
{%- endif %} |