Unverified Commit 22909572 authored by Martin Mose Hansen's avatar Martin Mose Hansen Committed by GitHub
Browse files

Add `Sendable` to Swift Templates (#2318)

parent 9eb0bae5
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -38,12 +38,16 @@ RUN rustup self update \
  && rustup show \
  && rm rust-toolchain.toml

ARG SWIFT_VERSION=6.0.3
ARG SWIFT_UBUNTU_VERSION=20.04
ARG SWIFT_CHECKSUM=012abe32cd1a9251d80ff1eeefcb3ff3fe2c02b569e5c755058b947f7afc9f56

RUN mkdir -p /tmp/setup-swift \
    && cd /tmp/setup-swift \
    && curl -o swift.tar.gz https://download.swift.org/swift-5.5-release/ubuntu1804/swift-5.5-RELEASE/swift-5.5-RELEASE-ubuntu18.04.tar.gz \
    # XXX TODO: should check a sha256sum or something here...
    && curl -o swift.tar.gz https://download.swift.org/swift-${SWIFT_VERSION}-release/ubuntu${SWIFT_UBUNTU_VERSION}/swift-${SWIFT_VERSION}-RELEASE/swift-${SWIFT_VERSION}-RELEASE-ubuntu${SWIFT_UBUNTU_VERSION}.tar.gz \
    && echo "${SWIFT_CHECKSUM} swift.tar.gz" | sha256sum -c - \
    && tar -xzf swift.tar.gz \
    && sudo mv swift-5.5-RELEASE-ubuntu18.04 /opt/swift \
    && sudo mv swift-${SWIFT_VERSION}-RELEASE-ubuntu${SWIFT_UBUNTU_VERSION} /opt/swift \
    && echo "export PATH=\"\$PATH:/opt/swift/usr/bin\"" >> /home/circleci/.bashrc \
    && echo "export PATH=\"\$PATH:/opt/swift/usr/bin\"" >> /home/circleci/.profile \
    && cd ../ \
+11 −12
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ The configurations prefixed with `experimental_` should be regarded as unstable
more likely to change than other configurations.

| Configuration name                 | Default                  | Description                                                                                                                                                          |
| ----------------------------------- | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| ---------------------------------- | ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `cdylib_name`                      | `uniffi_{namespace}`[^1] | The name of the compiled Rust library containing the FFI implementation (not needed when using `generate --library`).                                                |
| `module_name`                      | `{namespace}`[^1]        | The name of the Swift module containing the high-level foreign-language bindings.                                                                                    |
| `ffi_module_name`                  | `{module_name}FFI`       | The name of the lower-level C module containing the FFI declarations.                                                                                                |
@@ -16,7 +16,6 @@ more likely to change than other configurations.
| `generate_module_map`              | `true`                   | Whether to generate a `.modulemap` file for the lower-level C module with FFI declarations. (ignored by `uniffi-bindgen-swift`)                                      |
| `omit_argument_labels`             | `false`                  | Whether to omit argument labels in Swift function definitions.                                                                                                       |
| `generate_immutable_records`       | `false`                  | Whether to generate records with immutable fields (`let` instead of `var`).                                                                                          |
| `experimental_sendable_value_types` | `false`                  | Whether to mark value types as `Sendable'.                                                                                                                         |
| `custom_types`                     |                          | A map which controls how custom types are exposed to Swift. See the [custom types section of the manual](../types/custom_types.md#custom-types-in-the-bindings-code) |
| `omit_localized_error_conformance` | `false`                  | Whether to make generated error types conform to `LocalizedError`.                                                                                                   |

+0 −6
Original line number Diff line number Diff line
@@ -174,7 +174,6 @@ pub struct Config {
    generate_module_map: Option<bool>,
    omit_argument_labels: Option<bool>,
    generate_immutable_records: Option<bool>,
    experimental_sendable_value_types: Option<bool>,
    omit_localized_error_conformance: Option<bool>,
    #[serde(default)]
    custom_types: HashMap<String, CustomTypeConfig>,
@@ -239,11 +238,6 @@ impl Config {
        self.generate_immutable_records.unwrap_or(false)
    }

    /// Whether to mark value types as 'Sendable'
    pub fn experimental_sendable_value_types(&self) -> bool {
        self.experimental_sendable_value_types.unwrap_or(false)
    }

    // Whether to make generated error types conform to `LocalizedError`. Default: false.
    pub fn omit_localized_error_conformance(&self) -> bool {
        self.omit_localized_error_conformance.unwrap_or(false)
+3 −1
Original line number Diff line number Diff line
@@ -84,6 +84,8 @@ public func {{ ffi_converter_name }}_lower(_ value: {{ type_name }}) -> RustBuff
}

{% if !contains_object_references %}
{% if config.experimental_sendable_value_types() %}extension {{ type_name }}: Sendable {} {% endif %}
extension {{ type_name }}: Equatable, Hashable {}
#if swift(>=6.0)
extension {{ type_name }}: Sendable {}
#endif
{% endif %}
+28 −22
Original line number Diff line number Diff line
@@ -8,28 +8,7 @@
{% include "Protocol.swift" %}

{%- call swift::docstring(obj, 0) %}
open class {{ impl_class_name }}:
    {%- for tm in obj.uniffi_traits() %}
    {%-     match tm %}
    {%-         when UniffiTrait::Display { fmt } %}
    CustomStringConvertible,
    {%-         when UniffiTrait::Debug { fmt } %}
    CustomDebugStringConvertible,
    {%-         when UniffiTrait::Eq { eq, ne } %}
    Equatable,
    {%-         when UniffiTrait::Hash { hash } %}
    Hashable,
    {%-         else %}
    {%-    endmatch %}
    {%- endfor %}
    {%- if is_error %}
    Swift.Error,
    {% endif %}
    {%- for t in obj.trait_impls() %}
    {{ self::trait_protocol_name(ci, t.trait_name)? }},
    {% endfor %}
    {{ protocol_name }}
    {
open class {{ impl_class_name }}: {{ protocol_name }} {
    fileprivate let pointer: UnsafeMutableRawPointer!

    /// Used to instantiate a [FFIObject] without an actual pointer, for fakes in tests, mostly.
@@ -131,6 +110,33 @@ open class {{ impl_class_name }}:
{% include "CallbackInterfaceImpl.swift" %}
{%- endif %}

{%- for tm in obj.uniffi_traits() %}
{%-     match tm %}
{%-         when UniffiTrait::Display { fmt } %}
extension {{ impl_class_name }}: CustomStringConvertible {}
{%-         when UniffiTrait::Debug { fmt } %}
extension {{ impl_class_name }}: CustomDebugStringConvertible {}
{%-         when UniffiTrait::Eq { eq, ne } %}
extension {{ impl_class_name }}: Equatable {}
{%-         when UniffiTrait::Hash { hash } %}
extension {{ impl_class_name }}: Hashable {}
{%-         else %}
{%-    endmatch %}
{%- endfor %}

{%- if is_error %}
extension {{ impl_class_name }}: Swift.Error {}
{% endif %}

{%- for t in obj.trait_impls() %}
extension {{impl_class_name}}: {{ self::trait_protocol_name(ci, t.trait_name)? }} {}
{% endfor %}

#if swift(>=6.0)
extension {{ impl_class_name }}: Sendable {}
#endif


#if swift(>=5.8)
@_documentation(visibility: private)
#endif
Loading