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

Attributes for Param, SelfParam, and StructField types #521

Closed
Tracked by #604
ambiguousname opened this issue Jul 3, 2024 · 4 comments · Fixed by #626
Closed
Tracked by #604

Attributes for Param, SelfParam, and StructField types #521

ambiguousname opened this issue Jul 3, 2024 · 4 comments · Fixed by #626
Labels
B-demo_gen Demo_Gen backend

Comments

@ambiguousname
Copy link
Member

Per rust-lang/rust#60406 and https://doc.rust-lang.org/reference/attributes.html, attributes can be positioned in struct fields or parameters positions. Diplomat currently does not have an attrs field for the Param, ParamSelf, or StructField types.

This is something that would be especially nice to have for #500.

So for instance, you'd be able to write something like

struct SomeStruct {
  #[diplomat::attr(not(dart), disable)]
  pub dart_specific_feature,
}

Or

fn foo(
  #[diplomat::attr(kotlin, rename="someNewNameHere")]
  in : i32
) {}
@Manishearth
Copy link
Contributor

How would disabling a struct field work? Renaming, sure, but I can't see how disabling would work.

But yes, we should support this. Figuring out the inheritance might be tricky.

@ambiguousname
Copy link
Member Author

Disable was something of a random example, for a more specific use case for #500, the current attribute syntax looks something like this:

impl ICU4XLocale {
        /// Construct an [`ICU4XLocale`] from a locale identifier represented as a string.
        #[diplomat::attr(supports = constructors, constructor)]
        #[diplomat::demo(input(name(label = "Locale Name")))]
        pub fn new(name: &DiplomatStr) -> Box<ICU4XLocale> {
            Box::new(ICU4XLocale(Locale::try_from_bytes(name).unwrap()))
        }
    }

Could avoid a lot more boilerplate with this:

impl ICU4XLocale {
        /// Construct an [`ICU4XLocale`] from a locale identifier represented as a string.
        #[diplomat::attr(supports = constructors, constructor)]
        pub fn new(
        #[diplomat::demo(label = "Locale Name")]
        name: &DiplomatStr) -> Box<ICU4XLocale> {
            Box::new(ICU4XLocale(Locale::try_from_bytes(name).unwrap()))
        }
    }

@Manishearth
Copy link
Contributor

Ah, yes, that would be fine. Some thought needs to be given to inheritance, I'd assume that most attributes do not inherit past this boundary

@Manishearth
Copy link
Contributor

Shouldn't be hard to add support for if you're thinking of doing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-demo_gen Demo_Gen backend
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants