Skip to content

Commit

Permalink
Switch Python to BindingsIr
Browse files Browse the repository at this point in the history
The new system is to do a `BindingsIr` specialization pass, which means
implementing `VisitMut` and using it to:
  - Rewrite names, docstrings, etc.
  - Derive things like FFI converter names and store them in `lang_data`
  - Generate language-specific things like imports as we walk the tree

Along the way I made a couple changes to the Python bindings:

Changed `_uniffi_rust_call_async` to not lift the return value.  This
makes it fit in with the sync logic better and also enables async
constructors (which store the pointer in `self` rather than lift it).

Added the `CustomTypeConfig::type_name`.  While updating the code I
noticed the current templates handle this in a slightly buggy way where
they assume the type name is the builtin type, even if there's a
`from_custom`/`into_custom` that converts to a different type.

Removed almost all of the macros, `define_callable` is the only one left.
  • Loading branch information
bendk committed Dec 23, 2024
1 parent e61c88c commit 13285df
Show file tree
Hide file tree
Showing 59 changed files with 2,090 additions and 1,700 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ We have [detailed upgrade notes](https://mozilla.github.io/uniffi-rs/next/Upgrad
The `UniffiCustomTypeConverter` trait is no longer used, use the
[`custom_type!` macro](https://mozilla.github.io/uniffi-rs/next/types/custom_types.html) instead.

- Python: Added the `type_name` field for custom type configs. This is required when the name of
the custom type differs from the Python type name.

- The [UDL syntax for external types](https://mozilla.github.io/uniffi-rs/next/udl/external_types.html) has changed.
`typedef extern MyEnum;` has been replaced
with `typedef enum MyEnum;`. Attributes other than `[External = "crate_name"]` have been removed.
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions docs/manual/src/types/custom_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,9 @@ lower = "{}.toString()"
Here's how the configuration works in `uniffi.toml`.

* Create a `[bindings.{language}.custom_types.{CustomTypeName}]` table to enable a custom type on a bindings side. This has several subkeys:
* `type_name` (Optional, Typed languages only): Type/class name for the
custom type. Defaults to the type name used in the UDL. Note: The UDL
type name will still be used in generated function signatures, however it
will be defined as a typealias to this type.
* `type_name` (Optional): Type/class name for the custom type.
Defaults to the type name used in the proc macro.
Note: The UDL type name will still be used in generated function signatures for the foreign bindings, however it will be defined as a typealias to this type.
* `lift`: Expression to convert the UDL type to the custom type. `{}` will be replaced with the value of the UDL type.
* `lower`: Expression to convert the custom type to the UDL type. `{}` will be replaced with the value of the custom type.
* `imports` (Optional) list of modules to import for your `lift`/`lower` functions.
4 changes: 2 additions & 2 deletions examples/custom-types/uniffi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ from_custom = "{}.toString()"

[bindings.python.custom_types.Url]
# We're going to be the urllib.parse.ParseResult class, which is the closest
# thing Python has to a Url class. No need to specify `type_name` though,
# since Python is loosely typed.
# thing Python has to a Url class.
type_name = "urllib.parse.ParseResult"
# modules to import
imports = ["urllib.parse"]
# Functions to convert between strings and the ParsedUrl class
Expand Down
6 changes: 3 additions & 3 deletions fixtures/futures/tests/bindings/test_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ async def test():
asyncio.run(test())

def test_async_constructors(self):
# Check the default constructor has been disabled.
# Async constructor's can't be called directly
with self.assertRaises(ValueError) as e:
Megaphone()
self.assertTrue(str(e.exception).startswith("async constructors not supported"))

async def test():
megaphone = await Megaphone.secondary()
# Instead, users should use the `new` classmethod.
megaphone = await Megaphone.new()
result_alice = await megaphone.say_after(0, 'Alice')
self.assertEqual(result_alice, 'HELLO, ALICE!')

Expand Down
1 change: 1 addition & 0 deletions uniffi_bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ once_cell = "1.12"
paste = "1.0"
serde = { version = "1", features = ["derive"] }
toml = "0.5"
uniffi_internal_macros = { path = "../uniffi_internal_macros", version = "=0.28.3" }
uniffi_meta = { path = "../uniffi_meta", version = "=0.28.3" }
uniffi_testing = { path = "../uniffi_testing", version = "=0.28.3", optional = true }
uniffi_udl = { path = "../uniffi_udl", version = "=0.28.3" }
Expand Down

This file was deleted.

118 changes: 0 additions & 118 deletions uniffi_bindgen/src/bindings/python/gen_python/compounds.rs

This file was deleted.

Loading

0 comments on commit 13285df

Please sign in to comment.