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

Support "local" types which know how to convert to and from primitive types - eg, Guid, Url #475

Closed
mhammond opened this issue Jun 3, 2021 · 9 comments
Assignees
Labels

Comments

@mhammond
Copy link
Member

mhammond commented Jun 3, 2021

In app-services, we've had to compromise on the interfaces we expose, particularly around Guid and Url. Here's my latest take on this:

How about we "just" provide a way for consumers to provide a list of "external types" - a list of [(name, raw_type)] - eg, ('Guid', String)

Doing this allows us to write in the .udl:

  Guid get_guid(optional Guid? s);

and then it's fairly easy to have:

  1. the Rust side of the generation just calls fn get_guid(guid: Option<Guid>) -> Guid (assuming that type exists and implements the uniffi traits)
  2. the "other" side of the generation world to just use strings - in Python, get_guid(None)) works and returns a string.

and you can imagine (2) being optionally smarter - eg, we can probably arrange for projects to also arrange for a suitable Guid type to be used in the bindings.

I haven't thought too much about how to spell these types, but for the sake of example, let's assume in uniffi.toml with something like:

[local_types]

[local_types.Guid]
raw = "String"
# Python gets special support, other languages get a string.
python = { lower = "some_package.GuidType.to_string", lift = "some_package.GuidType.from_string" }

[local_types.JSONObject]
raw = "String"
python = {...}
kotlin = {...}
# etc!

This would probably allow a consumer to fully implement #440 without any help from us and probably even backout timestamp/duration support.

(To be clear, I'm not saying we should exclude JSON support or back timestamps out, but we could)

FWIW, I've got my get_guid() example above working in a very very hacky way, including painfully hand-written implemention of ViaFfi for Guid - but close enough to convince me it's doable.

WDYT?

┆Issue is synchronized with this Jira Task
┆Issue Number: UNIFFI-65

@rfk

This comment has been minimized.

@mhammond

This comment has been minimized.

@mhammond

This comment has been minimized.

@mhammond mhammond changed the title Support any types which into and try_into into primitive types - eg, Guid, Url Support "local" types which know how to convert to and from primitive types - eg, Guid, Url Jun 10, 2021
@mhammond
Copy link
Member Author

mhammond commented Jun 10, 2021

FWIW, I've edited the first comment to describe a hand-wavey proposal which I'd love thoughts on and hidden earlier comments)

@rfk
Copy link
Collaborator

rfk commented Jun 11, 2021

So, I think this is headed in broadly a good direction, and it's a direction we should explore a bit in an iterative fashion. It seems to intersect really strongly with James' Transform Towers proposal.

As a first attempt, I feel like I'd rather see the types named explicitly in the interface definition somehow, rather than being squirreled away in a config file. Maybe we could use the typedef syntax as a starting point? The details of exactly how to codegen for them in a foreign language might still need to be in a config file, but it sounds like you're not necessarily interested in pushing on the foreign-language side right away, so much as allowing richer types on the Rust side.

I wonder if something like typedef [Extern] String Guid; in the .udl and a hand-written impl ViaFfi for Guid would be enough to take a first step here?

@rfk
Copy link
Collaborator

rfk commented Jun 11, 2021

FWIW, I can imagine a similar annotation being used to access types from another component, which in theory would already come with their own ViaFfi implementation.

@mhammond
Copy link
Member Author

FWIW, I can imagine a similar annotation being used to access types from another component

Yeah, I've been pondering this too, but then I started having images of uniffizords and had to seek therapy.

@rfk
Copy link
Collaborator

rfk commented Jun 11, 2021

I started having images of uniffizords and had to seek therapy.

You are going to love the next Google Doc that I'm working on...

@mhammond
Copy link
Member Author

mhammond commented Jan 6, 2025

custom types ftw 🎉

@mhammond mhammond closed this as completed Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants