Skip to content

Commit

Permalink
Make the more restrictive behavior the default, add changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
ckamm committed Dec 26, 2022
1 parent 7e2a370 commit d1fe74e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,12 @@ The minor version will be incremented upon a breaking change and the patch versi
### Breaking

- lang: Remove `state` and `interface` attributes ([#2285](https://github.com/coral-xyz/anchor/pull/2285)).
- lang: `account(zero_copy)` now derives the `bytemuck::Pod` and `Zeroable` traits instead of using `unsafe impl` ([#2330](https://github.com/coral-xyz/anchor/pull/2330)).

This imposes useful restrictions on the type, like not having padding bytes and all fields being `Pod` themselves.
See [bytemuck::Pod](https://docs.rs/bytemuck/latest/bytemuck/trait.Pod.html) for details.

Legacy applications can use `account(zero_copy(unsafe_bytemuck_impls))` for the old behavior.

## [0.26.0] - 2022-12-15

Expand Down
12 changes: 6 additions & 6 deletions lang/attribute/account/src/lib.rs
Expand Up @@ -66,7 +66,7 @@ pub fn account(
) -> proc_macro::TokenStream {
let mut namespace = "".to_string();
let mut is_zero_copy = false;
let mut safe_bytemuck = false;
let mut unsafe_bytemuck = false;
let args_str = args.to_string();
let args: Vec<&str> = args_str.split(',').collect();
if args.len() > 2 {
Expand All @@ -81,10 +81,10 @@ pub fn account(
.collect();
if ns == "zero_copy" {
is_zero_copy = true;
safe_bytemuck = false;
} else if ns == "zero_copy(safe_bytemuck_derives)" {
unsafe_bytemuck = false;
} else if ns == "zero_copy(unsafe_bytemuck_impls)" {
is_zero_copy = true;
safe_bytemuck = true;
unsafe_bytemuck = true;
} else {
namespace = ns;
}
Expand Down Expand Up @@ -129,7 +129,7 @@ pub fn account(
};

let unsafe_bytemuck_impl = {
if !safe_bytemuck {
if unsafe_bytemuck {
quote! {
#[automatically_derived]
unsafe impl #impl_gen anchor_lang::__private::bytemuck::Pod for #account_name #type_gen #where_clause {}
Expand All @@ -142,7 +142,7 @@ pub fn account(
};

let safe_bytemuck_derives = {
if safe_bytemuck {
if !unsafe_bytemuck {
quote! {
#[derive(bytemuck::Pod, bytemuck::Zeroable)]
}
Expand Down
2 changes: 1 addition & 1 deletion lang/tests/generics_test.rs
Expand Up @@ -22,7 +22,7 @@ where
pub associated: Account<'info, Associated<U>>,
}

#[account(zero_copy)]
#[account(zero_copy(unsafe_bytemuck_impls))]
pub struct FooAccount<const N: usize> {
pub data: WrappedU8Array<N>,
}
Expand Down

0 comments on commit d1fe74e

Please sign in to comment.