Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift: Make it possible to mark value types as Sendable with sendable_value_types config #2045

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

- Proc-macro record defaults now support empty vecs and Some values.

- Swift: Records and Enums without object references can now be made `Sendable` Swift,
by opting in to new Configuration `experimental_sendable_value_types` in `uniffi.toml`.

### What's fixed?

- Fixed a memory leak in callback interface handling.
Expand Down
23 changes: 13 additions & 10 deletions docs/manual/src/swift/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ The generated Swift module can be configured using a `uniffi.toml` configuration

## Available options

| 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. |
| `ffi_module_filename` | `{ffi_module_name}` | The filename stem for the lower-level C module containing the FFI declarations. |
| `generate_module_map` | `true` | Whether to generate a `.modulemap` file for the lower-level C module with FFI declarations. |
| `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`). |
| `custom_types` | | A map which controls how custom types are exposed to Swift. See the [custom types section of the manual](../udl/custom_types.md#custom-types-in-the-bindings-code)|
The configurations prefixed with `experimental_` should be regarded as unstable and
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. |
| `ffi_module_filename` | `{ffi_module_name}` | The filename stem for the lower-level C module containing the FFI declarations. |
| `generate_module_map` | `true` | Whether to generate a `.modulemap` file for the lower-level C module with FFI declarations. |
| `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](../udl/custom_types.md#custom-types-in-the-bindings-code) |

[^1]: `namespace` is the top-level namespace from your UDL file.

Expand Down
6 changes: 6 additions & 0 deletions uniffi_bindgen/src/bindings/swift/gen_swift/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ pub struct Config {
generate_module_map: Option<bool>,
omit_argument_labels: Option<bool>,
generate_immutable_records: Option<bool>,
experimental_sendable_value_types: Option<bool>,
#[serde(default)]
custom_types: HashMap<String, CustomTypeConfig>,
}
Expand Down Expand Up @@ -291,6 +292,11 @@ impl Config {
pub fn generate_immutable_records(&self) -> bool {
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)
}
}

impl BindingsConfig for Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ 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 {}
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public struct {{ type_name }} {
}

{% if !contains_object_references %}
{% if config.experimental_sendable_value_types() %}extension {{ type_name }}: Sendable {} {% endif %}
extension {{ type_name }}: Equatable, Hashable {
public static func ==(lhs: {{ type_name }}, rhs: {{ type_name }}) -> Bool {
{%- for field in rec.fields() %}
Expand Down