jerel/membrane

support type alias

huang12zheng opened this issue · 1 comments

type IS = String;

#[async_dart(namespace = "accounts")]
pub async fn contact(user_id: IS) -> Result<data::Contact, data::Error> {
  println!("async {:?}", thread::current().id());
  Ok(data::Contact {
    id: user_id.parse().unwrap(),
    ..data::Contact::default()
  })
}

would get error in dart

Future<Contact> contact({required String userId}) async {
Future<Contact> contact({required IS userId}) async {

final ptr = userId.toNativeUtf8().cast<Char>();

ffi.Pointer<ffi.Char> user_id,
ffi.Pointer<ffi.Uint8> user_id,
```·
jerel commented

There's no way for the async_dart macro to handle aliases in a fully automatic fashion. This is because at compile time the only thing we see inside the async_dart macro is the function param user_id: IS with no way to trace where IS comes from. In other words we can't call IS to figure out that it points to String. The developer may have even done use some_other_module::IS; to bring the alias into the current scope.

I will leave this issue open for now, I've been doing some unrelated work (not public yet) which may result in a separate macro that could potentially be applied directly to a type and generate a matching Dart type.