Backed out 5 changesets (bug 1890092, bug 1888683) for causing build bustages & crash...
[gecko.git] / third_party / rust / uniffi_bindgen / src / bindings / swift / templates / wrapper.swift
blobc34d348efb47f1f2022a70f0c117d7444a9adddc
1 // This file was autogenerated by some hot garbage in the `uniffi` crate.
2 // Trust me, you don't want to mess with it!
3 {%- import "macros.swift" as swift %}
4 import Foundation
5 {%- for imported_class in self.imports() %}
6 import {{ imported_class }}
7 {%- endfor %}
9 // Depending on the consumer's build setup, the low-level FFI code
10 // might be in a separate module, or it might be compiled inline into
11 // this module. This is a bit of light hackery to work with both.
12 #if canImport({{ config.ffi_module_name() }})
13 import {{ config.ffi_module_name() }}
14 #endif
16 {% include "RustBufferTemplate.swift" %}
17 {% include "Helpers.swift" %}
19 // Public interface members begin here.
20 {{ type_helper_code }}
22 {%- if ci.has_async_fns() %}
23 {% include "Async.swift" %}
24 {%- endif %}
26 {%- for func in ci.function_definitions() %}
27 {%- include "TopLevelFunctionTemplate.swift" %}
28 {%- endfor %}
30 private enum InitializationResult {
31     case ok
32     case contractVersionMismatch
33     case apiChecksumMismatch
35 // Use a global variables to perform the versioning checks. Swift ensures that
36 // the code inside is only computed once.
37 private var initializationResult: InitializationResult {
38     // Get the bindings contract version from our ComponentInterface
39     let bindings_contract_version = {{ ci.uniffi_contract_version() }}
40     // Get the scaffolding contract version by calling the into the dylib
41     let scaffolding_contract_version = {{ ci.ffi_uniffi_contract_version().name() }}()
42     if bindings_contract_version != scaffolding_contract_version {
43         return InitializationResult.contractVersionMismatch
44     }
46     {%- for (name, expected_checksum) in ci.iter_checksums() %}
47     if ({{ name }}() != {{ expected_checksum }}) {
48         return InitializationResult.apiChecksumMismatch
49     }
50     {%- endfor %}
52     {% for fn in self.initialization_fns() -%}
53     {{ fn }}()
54     {% endfor -%}
56     return InitializationResult.ok
59 private func uniffiEnsureInitialized() {
60     switch initializationResult {
61     case .ok:
62         break
63     case .contractVersionMismatch:
64         fatalError("UniFFI contract version mismatch: try cleaning and rebuilding your project")
65     case .apiChecksumMismatch:
66         fatalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
67     }