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

Lightweight generics support #753

Open
Manishearth opened this issue Dec 20, 2024 · 0 comments
Open

Lightweight generics support #753

Manishearth opened this issue Dec 20, 2024 · 0 comments
Labels
A-type-system Area: Additions and improvements to the types Diplomat supports enhancement New feature or request

Comments

@Manishearth
Copy link
Contributor

In theory, Diplomat could support generics, provided each generic can only pull from a closed set of types.

Something like this:

#[diplomat::generictrait]
pub trait SomeTrait {...}

impl SomeTrait for crate::module::ffi::SomeOpaqueType;
impl SomeTrait for crate::module::ffi::SomeOtherOpaqueType;
impl SomeTrait for u8;

pub struct GenericOpaque<T: SomeTrait>(RustType<T>);

impl GenericOpaque {
   fn create(t: &T) -> Box<Self> {...}
}

This would generate C ABI functions create_SomeOpaqueType, create_SomeOpaqueType, etc. In C++ this would produce a specialized, templated GenericOpaque<T> type. In JS it would be a single type with matchers on its methods.

If you want to use traits from other modules you'd need to define some type of #[diplomat::trait_use = src/traits.rs] thing so the macro can use them (see #752 for a similar design issue).

This is likely to be quite complicated, needing major changes to the AST, HIR, and individual backends.

@Manishearth Manishearth added enhancement New feature or request A-type-system Area: Additions and improvements to the types Diplomat supports labels Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Additions and improvements to the types Diplomat supports enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant