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

Update/anchor v0.27 #42

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add packed repr
  • Loading branch information
datben committed Mar 16, 2023
commit d5b158b8add27ad66ff9bd69f9794ec4b83e9f4a
22 changes: 16 additions & 6 deletions crates/anchor-idl/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct GeneratorOptions {
pub c_representation: Option<PathList>,
/// List of `repr(transparent)` structs.
pub transparent_representation: Option<PathList>,
/// List of `repr(packed)` structs.
pub packed_representation: Option<PathList>,
}

fn path_list_to_string(list: Option<&PathList>) -> HashSet<String> {
Expand All @@ -50,9 +52,14 @@ impl GeneratorOptions {

let transparent_repr = path_list_to_string(self.transparent_representation.as_ref());

let packed_repr = path_list_to_string(self.packed_representation.as_ref());

let repr = c_repr
.union(&transparent_repr)
.cloned()
.collect::<HashSet<_>>()
.union(&packed_repr)
.cloned()
.collect::<HashSet<_>>();

let zero_copy = zero_copy_safe
Expand All @@ -65,12 +72,14 @@ impl GeneratorOptions {
all_structs.into_iter().for_each(|name| {
let is_c_repr = c_repr.contains(name);
let is_transparent_repr = transparent_repr.contains(name);

let representation = match (is_c_repr, is_transparent_repr) {
(true, true) => panic!("cant be c and transparent representation at the same time"),
(true, false) => Some(Representation::C),
(false, true) => Some(Representation::Transparent),
(false, false) => None,
let is_packed_repr = packed_repr.contains(name);

let representation = match (is_c_repr, is_transparent_repr, is_packed_repr) {
(true, false, false) => Some(Representation::C),
(false, true, false) => Some(Representation::Transparent),
(false, false, true) => Some(Representation::Packed),
(false, false, false) => None,
_ => panic!("a type cannot have many representation"),
};

let is_zero_copy_safe = zero_copy_safe.contains(name);
Expand Down Expand Up @@ -112,6 +121,7 @@ pub enum ZeroCopy {
pub enum Representation {
C,
Transparent,
Packed,
}
pub struct Generator {
pub idl: anchor_syn::idl::Idl,
Expand Down
11 changes: 8 additions & 3 deletions crates/anchor-idl/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,20 @@ pub fn generate_account(
},
};
if let Some(repr) = opts.representation {
match repr {
let repr_quote = match repr {
crate::Representation::C => quote! {
#zero_copy_quote
#[repr(C)]
},
crate::Representation::Transparent => quote! {
#zero_copy_quote
#[repr(transparent)]
},
crate::Representation::Packed => quote! {
#[repr(packed)]
},
};
quote! {
#zero_copy_quote
#repr_quote
}
} else {
zero_copy_quote
Expand Down
12 changes: 9 additions & 3 deletions crates/anchor-idl/src/typedef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,21 @@ pub fn generate_struct(
},
};
if let Some(repr) = opts.representation {
match repr {
let repr_quote = match repr {
crate::Representation::C => quote! {
#zero_copy_quote
#[repr(C)]
},
crate::Representation::Transparent => quote! {
#zero_copy_quote

#[repr(transparent)]
},
crate::Representation::Packed => quote! {
#[repr(packed)]
},
};
quote! {
#zero_copy_quote
#repr_quote
}
} else {
zero_copy_quote
Expand Down
2 changes: 1 addition & 1 deletion examples/whirlpools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
anchor_gen::generate_cpi_interface!(
idl_path = "idl.json",
zero_copy_unsafe(TickArray, Tick),
c_representation(TickArray, Tick),
packed_representation(TickArray, Tick),
);

impl Default for state::TickArray {
Expand Down