Add account(zero_copy(safe_bytemuck_derives)) feature.
The default account(zero_copy) feature unsafe impls the bytemuck traits The new one derives it instead, which runs desired sanity checks like "struct has no padding".
This commit is contained in:
parent
fbd238fb7c
commit
3f8373770c
|
@ -66,6 +66,7 @@ pub fn account(
|
||||||
) -> proc_macro::TokenStream {
|
) -> proc_macro::TokenStream {
|
||||||
let mut namespace = "".to_string();
|
let mut namespace = "".to_string();
|
||||||
let mut is_zero_copy = false;
|
let mut is_zero_copy = false;
|
||||||
|
let mut safe_bytemuck = false;
|
||||||
let args_str = args.to_string();
|
let args_str = args.to_string();
|
||||||
let args: Vec<&str> = args_str.split(',').collect();
|
let args: Vec<&str> = args_str.split(',').collect();
|
||||||
if args.len() > 2 {
|
if args.len() > 2 {
|
||||||
|
@ -80,6 +81,10 @@ pub fn account(
|
||||||
.collect();
|
.collect();
|
||||||
if ns == "zero_copy" {
|
if ns == "zero_copy" {
|
||||||
is_zero_copy = true;
|
is_zero_copy = true;
|
||||||
|
safe_bytemuck = false;
|
||||||
|
} else if ns == "zero_copy(safe_bytemuck_derives)" {
|
||||||
|
is_zero_copy = true;
|
||||||
|
safe_bytemuck = true;
|
||||||
} else {
|
} else {
|
||||||
namespace = ns;
|
namespace = ns;
|
||||||
}
|
}
|
||||||
|
@ -123,16 +128,37 @@ pub fn account(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
proc_macro::TokenStream::from({
|
let unsafe_bytemuck_impl = {
|
||||||
if is_zero_copy {
|
if !safe_bytemuck {
|
||||||
quote! {
|
quote! {
|
||||||
#[zero_copy]
|
|
||||||
#account_strct
|
|
||||||
|
|
||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
unsafe impl #impl_gen anchor_lang::__private::bytemuck::Pod for #account_name #type_gen #where_clause {}
|
unsafe impl #impl_gen anchor_lang::__private::bytemuck::Pod for #account_name #type_gen #where_clause {}
|
||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
unsafe impl #impl_gen anchor_lang::__private::bytemuck::Zeroable for #account_name #type_gen #where_clause {}
|
unsafe impl #impl_gen anchor_lang::__private::bytemuck::Zeroable for #account_name #type_gen #where_clause {}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
quote! {}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let safe_bytemuck_derives = {
|
||||||
|
if safe_bytemuck {
|
||||||
|
quote! {
|
||||||
|
#[derive(bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
quote! {}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
proc_macro::TokenStream::from({
|
||||||
|
if is_zero_copy {
|
||||||
|
quote! {
|
||||||
|
#[zero_copy]
|
||||||
|
#safe_bytemuck_derives
|
||||||
|
#account_strct
|
||||||
|
|
||||||
|
#unsafe_bytemuck_impl
|
||||||
|
|
||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
impl #impl_gen anchor_lang::ZeroCopy for #account_name #type_gen #where_clause {}
|
impl #impl_gen anchor_lang::ZeroCopy for #account_name #type_gen #where_clause {}
|
||||||
|
|
Loading…
Reference in New Issue