summaryrefslogtreecommitdiffstats
path: root/third_party/rust/uniffi_bindgen/src/bindings/swift/templates/wrapper.swift
blob: 17fdde74e07a9dea7a2921f55647a4c3313abb0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// This file was autogenerated by some hot garbage in the `uniffi` crate.
// Trust me, you don't want to mess with it!

// swiftlint:disable all

{%- call swift::docstring_value(ci.namespace_docstring(), 0) %}

{%- import "macros.swift" as swift %}
import Foundation
{%- for imported_class in self.imports() %}
import {{ imported_class }}
{%- endfor %}

// Depending on the consumer's build setup, the low-level FFI code
// might be in a separate module, or it might be compiled inline into
// this module. This is a bit of light hackery to work with both.
#if canImport({{ config.ffi_module_name() }})
import {{ config.ffi_module_name() }}
#endif

{% include "RustBufferTemplate.swift" %}
{% include "Helpers.swift" %}
{% include "HandleMap.swift" %}

// Public interface members begin here.
{{ type_helper_code }}

{%- if ci.has_async_fns() %}
{% include "Async.swift" %}
{%- endif %}

{%- for func in ci.function_definitions() %}
{%- include "TopLevelFunctionTemplate.swift" %}
{%- endfor %}

private enum InitializationResult {
    case ok
    case contractVersionMismatch
    case apiChecksumMismatch
}
// Use a global variables to perform the versioning checks. Swift ensures that
// the code inside is only computed once.
private var initializationResult: InitializationResult {
    // Get the bindings contract version from our ComponentInterface
    let bindings_contract_version = {{ ci.uniffi_contract_version() }}
    // Get the scaffolding contract version by calling the into the dylib
    let scaffolding_contract_version = {{ ci.ffi_uniffi_contract_version().name() }}()
    if bindings_contract_version != scaffolding_contract_version {
        return InitializationResult.contractVersionMismatch
    }

    {%- for (name, expected_checksum) in ci.iter_checksums() %}
    if ({{ name }}() != {{ expected_checksum }}) {
        return InitializationResult.apiChecksumMismatch
    }
    {%- endfor %}

    {% for fn in self.initialization_fns() -%}
    {{ fn }}()
    {% endfor -%}

    return InitializationResult.ok
}

private func uniffiEnsureInitialized() {
    switch initializationResult {
    case .ok:
        break
    case .contractVersionMismatch:
        fatalError("UniFFI contract version mismatch: try cleaning and rebuilding your project")
    case .apiChecksumMismatch:
        fatalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
    }
}

// swiftlint:enable all