Skip to content

Commit 5681d55

Browse files
committed
Make UDL generation implement FFI traits for all tags (#1865)
This means it works exactly like the proc-macro code which simplifies things significantly. Removed the `use_udl_*` macros, they're not needed anymore. Added the `remote_type!` macro, it's now needed for using remote types who's FFI trait impls are defined in another crate (which is now possible from using UDL and proc-macros)
1 parent bacdfc3 commit 5681d55

File tree

16 files changed

+102
-203
lines changed

16 files changed

+102
-203
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fixtures/ext-types/lib/src/ext-types-lib.udl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,20 @@ typedef extern Url;
5555
[External="custom_types"]
5656
typedef extern Handle;
5757

58-
// Here are some different kinds of "external" types - the types are described
58+
// Here are some different kinds of remote types - the types are described
5959
// in this UDL, but the types themselves are defined in a different crate.
60+
61+
[Remote]
6062
dictionary ExternalCrateDictionary {
6163
string sval;
6264
};
6365

66+
[Remote]
6467
interface ExternalCrateInterface {
6568
string value();
6669
};
6770

68-
[NonExhaustive]
71+
[Remote, NonExhaustive]
6972
enum ExternalCrateNonExhaustiveEnum {
7073
"One",
7174
"Two",

fixtures/ext-types/lib/src/lib.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,9 @@ use uniffi_one::{
1111
use uniffi_sublib::SubLibType;
1212
use url::Url;
1313

14-
// #1988
15-
uniffi::ffi_converter_forward!(
16-
ext_types_custom::Ouid,
17-
ext_types_custom::UniFfiTag,
18-
crate::UniFfiTag
19-
);
20-
uniffi::ffi_converter_forward!(
21-
ext_types_custom::ANestedGuid,
22-
ext_types_custom::UniFfiTag,
23-
crate::UniFfiTag
24-
);
14+
// Remote types require a macro call in the Rust source
15+
16+
uniffi::remote_type!(Url, custom_types);
2517

2618
pub struct CombinedType {
2719
pub uoe: UniffiOneEnum,

fixtures/ext-types/proc-macro-lib/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ use uniffi_one::{
66
};
77
use url::Url;
88

9-
uniffi::use_udl_record!(uniffi_one, UniffiOneType);
10-
uniffi::use_udl_enum!(uniffi_one, UniffiOneEnum);
11-
uniffi::use_udl_object!(uniffi_one, UniffiOneInterface);
12-
uniffi::use_udl_record!(ext_types_custom, Guid);
13-
uniffi::use_udl_record!(custom_types, Url);
14-
uniffi::use_udl_record!(custom_types, Handle);
9+
uniffi::remote_type!(Url, custom_types);
1510

1611
#[derive(uniffi::Record)]
1712
pub struct CombinedType {

fixtures/ext-types/sub-lib/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use std::sync::Arc;
22
use uniffi_one::{UniffiOneEnum, UniffiOneInterface, UniffiOneTrait};
33

4-
uniffi::use_udl_object!(uniffi_one, UniffiOneInterface);
5-
uniffi::use_udl_enum!(uniffi_one, UniffiOneEnum);
6-
74
#[derive(Default, uniffi::Record)]
85
pub struct SubLibType {
96
pub maybe_enum: Option<UniffiOneEnum>,

uniffi_bindgen/src/scaffolding/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use askama::Template;
77
use std::borrow::Borrow;
88

99
use super::interface::*;
10-
use heck::{ToShoutySnakeCase, ToSnakeCase};
10+
use heck::ToShoutySnakeCase;
1111

1212
#[derive(Template)]
1313
#[template(syntax = "rs", escape = "none", path = "scaffolding_template.rs")]
@@ -71,9 +71,4 @@ mod filters {
7171
Type::External { name, .. } => format!("r#{name}"),
7272
})
7373
}
74-
75-
// Turns a `crate-name` into the `crate_name` the .rs code needs to specify.
76-
pub fn crate_name_rs(nm: &str) -> Result<String, askama::Error> {
77-
Ok(format!("r#{}", nm.to_string().to_snake_case()))
78-
}
7974
}

uniffi_bindgen/src/scaffolding/templates/ExternalTypesTemplate.rs

Lines changed: 0 additions & 17 deletions
This file was deleted.

uniffi_bindgen/src/scaffolding/templates/scaffolding_template.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,5 @@ uniffi::deps::static_assertions::assert_impl_all!({{ k|type_rs }}: ::std::cmp::E
4545
{% include "CallbackInterfaceTemplate.rs" %}
4646
{% endfor %}
4747

48-
// External and Wrapped types
49-
{% include "ExternalTypesTemplate.rs" %}
50-
5148
// Export scaffolding checksums for UDL items
5249
{% include "Checksums.rs" %}

uniffi_core/src/lib.rs

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -183,73 +183,6 @@ macro_rules! ffi_converter_rust_buffer_lift_and_lower {
183183
};
184184
}
185185

186-
/// Macro to implement `FfiConverter<T>` for a UniFfiTag using a different UniFfiTag
187-
///
188-
/// This is used for external types
189-
#[macro_export]
190-
macro_rules! ffi_converter_forward {
191-
// Forward a `FfiConverter` implementation
192-
($T:ty, $existing_impl_tag:ty, $new_impl_tag:ty) => {
193-
::uniffi::do_ffi_converter_forward!(
194-
FfiConverter,
195-
$T,
196-
$T,
197-
$existing_impl_tag,
198-
$new_impl_tag
199-
);
200-
201-
$crate::derive_ffi_traits!(local $T);
202-
};
203-
}
204-
205-
/// Macro to implement `FfiConverterArc<T>` for a UniFfiTag using a different UniFfiTag
206-
///
207-
/// This is used for external types
208-
#[macro_export]
209-
macro_rules! ffi_converter_arc_forward {
210-
($T:ty, $existing_impl_tag:ty, $new_impl_tag:ty) => {
211-
::uniffi::do_ffi_converter_forward!(
212-
FfiConverterArc,
213-
::std::sync::Arc<$T>,
214-
$T,
215-
$existing_impl_tag,
216-
$new_impl_tag
217-
);
218-
219-
// Note: no need to call derive_ffi_traits! because there is a blanket impl for all Arc<T>
220-
};
221-
}
222-
223-
// Generic code between the two macros above
224-
#[doc(hidden)]
225-
#[macro_export]
226-
macro_rules! do_ffi_converter_forward {
227-
($trait:ident, $rust_type:ty, $T:ty, $existing_impl_tag:ty, $new_impl_tag:ty) => {
228-
unsafe impl $crate::$trait<$new_impl_tag> for $T {
229-
type FfiType = <$T as $crate::$trait<$existing_impl_tag>>::FfiType;
230-
231-
fn lower(obj: $rust_type) -> Self::FfiType {
232-
<$T as $crate::$trait<$existing_impl_tag>>::lower(obj)
233-
}
234-
235-
fn try_lift(v: Self::FfiType) -> $crate::Result<$rust_type> {
236-
<$T as $crate::$trait<$existing_impl_tag>>::try_lift(v)
237-
}
238-
239-
fn write(obj: $rust_type, buf: &mut Vec<u8>) {
240-
<$T as $crate::$trait<$existing_impl_tag>>::write(obj, buf)
241-
}
242-
243-
fn try_read(buf: &mut &[u8]) -> $crate::Result<$rust_type> {
244-
<$T as $crate::$trait<$existing_impl_tag>>::try_read(buf)
245-
}
246-
247-
const TYPE_ID_META: ::uniffi::MetadataBuffer =
248-
<$T as $crate::$trait<$existing_impl_tag>>::TYPE_ID_META;
249-
}
250-
};
251-
}
252-
253186
#[cfg(test)]
254187
mod test {
255188
use super::{FfiConverter, UniFfiTag};

uniffi_macros/src/derive.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ impl DeriveOptions {
7070
/// Construct DeriveOptions for `udl_derive`
7171
pub fn udl_derive() -> Self {
7272
Self {
73-
// TODO: change this to false
74-
local_tag: true,
73+
local_tag: false,
7574
generate_metadata: false,
7675
}
7776
}

0 commit comments

Comments
 (0)