saber-hq / anchor-gen

Generates an Anchor CPI crate from a JSON IDL.
https://crates.io/crates/anchor-gen
Apache License 2.0
108 stars 63 forks source link

Custom Default trait on user-defined IDL structs #6

Open losman0s opened 2 years ago

losman0s commented 2 years ago

Is there a way to handle custom Default trait definition for some of the Defined structs, e.g.:

{
  "name": "MyStruct",
  "type": {
    "kind": "struct",
    "fields": [
      {
        "name": "authority",
        "type": "publicKey"
      },
      {
        "name": "reservedSpace",
        "type": {
          "array": ["u128", 256]
        }
      }
    ]
  }
}

atm the explicit sizes (0-32) in the standard implementation prevents generation: the trait 'Default' is not implemented for '[u128; 256]'

losman0s commented 2 years ago

Not sure if notifications go through so will try pinging @macalinao

macalinao commented 2 years ago

This specific case is fixed. We should probably add a way to explicitly specify that a Default should not be generated for a type though

losman0s commented 2 years ago

This specific case is fixed. We should probably add a way to explicitly specify that a Default should not be generated for a type though

Great thanks! One last clarification though: I don't get the use case for the separation between anchor_gen::generate_cpi_crate and anchor_gen::generate_cpi_interface. The former, shown in the README, seems to only be a "forced defaults" of the latter.

  1. Am I missing something
  2. If not, it might be valuable to avoid confusion by either explaining the distinction, or using anchor_gen::generate_cpi_interface in teh README (although I agree examples are pretty helpful in any case)
macalinao commented 2 years ago

Backwards compatibility. Maybe we should mark the other one as deprecated