Skip to content

Commit 3cdb734

Browse files
committed
add option to skip Default impl on struct
1 parent 8b04272 commit 3cdb734

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

crates/anchor-idl/src/program.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub struct GeneratorOptions {
2020
pub zero_copy: Option<PathList>,
2121
/// List of `repr(packed)` structs.
2222
pub packed: Option<PathList>,
23+
/// List of structs that should have `Default` impl skipped.
24+
pub skip_default: Option<PathList>,
2325
}
2426

2527
fn path_list_to_string(list: Option<&PathList>) -> HashSet<String> {
@@ -40,15 +42,23 @@ impl GeneratorOptions {
4042

4143
let zero_copy = path_list_to_string(self.zero_copy.as_ref());
4244
let packed = path_list_to_string(self.packed.as_ref());
45+
let skip_default = path_list_to_string(self.skip_default.as_ref());
46+
47+
let mut all_structs: HashSet<String> = HashSet::new();
48+
for struct_item in [&zero_copy, &packed, &skip_default].into_iter().flatten() {
49+
if !all_structs.contains(struct_item) {
50+
all_structs.insert(struct_item.clone());
51+
}
52+
}
4353

4454
let mut struct_opts: BTreeMap<String, StructOpts> = BTreeMap::new();
45-
let all_structs: HashSet<&String> = zero_copy.union(&packed).collect::<HashSet<_>>();
46-
all_structs.into_iter().for_each(|name| {
55+
all_structs.iter().for_each(|name| {
4756
struct_opts.insert(
4857
name.to_string(),
4958
StructOpts {
5059
zero_copy: zero_copy.contains(name),
5160
packed: packed.contains(name),
61+
skip_default: skip_default.contains(name),
5262
},
5363
);
5464
});
@@ -61,6 +71,7 @@ impl GeneratorOptions {
6171
pub struct StructOpts {
6272
pub packed: bool,
6373
pub zero_copy: bool,
74+
pub skip_default: bool,
6475
}
6576

6677
pub struct Generator {

crates/anchor-idl/src/typedef.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub fn generate_struct(
142142
let fields_rendered = generate_fields(fields);
143143
let props = get_field_list_properties(defs, fields);
144144

145-
let derive_default = if props.can_derive_default {
145+
let derive_default = if props.can_derive_default && !opts.skip_default {
146146
quote! {
147147
#[derive(Default)]
148148
}

examples/whirlpools/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
anchor_gen::generate_cpi_interface!(
1111
idl_path = "idl.json",
1212
zero_copy(TickArray, Tick),
13-
packed(TickArray, Tick)
13+
packed(TickArray, Tick),
14+
skip_default(WhirlpoolBumps)
1415
);
1516

1617
impl Default for state::TickArray {
@@ -23,4 +24,12 @@ impl Default for state::TickArray {
2324
}
2425
}
2526

27+
impl Default for WhirlpoolBumps {
28+
fn default() -> Self {
29+
Self {
30+
whirlpool_bump: Default::default(),
31+
}
32+
}
33+
}
34+
2635
declare_id!("whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc");

0 commit comments

Comments
 (0)