From 33c9a14d1b127eb620156a0591869dc091b25e83 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Mon, 26 Dec 2022 16:32:51 +0100 Subject: [PATCH] Make the more restrictive behavior the default, add changelog --- CHANGELOG.md | 6 ++++++ lang/attribute/account/src/lib.rs | 12 ++++++------ lang/tests/generics_test.rs | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c03d06286..88f76cc0ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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_derives))` for the old behavior. ## [0.26.0] - 2022-12-15 diff --git a/lang/attribute/account/src/lib.rs b/lang/attribute/account/src/lib.rs index 4b3ea0d600..d72f49f5b8 100644 --- a/lang/attribute/account/src/lib.rs +++ b/lang/attribute/account/src/lib.rs @@ -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 { @@ -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_derives)" { is_zero_copy = true; - safe_bytemuck = true; + unsafe_bytemuck = true; } else { namespace = ns; } @@ -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 {} @@ -142,7 +142,7 @@ pub fn account( }; let safe_bytemuck_derives = { - if safe_bytemuck { + if !unsafe_bytemuck { quote! { #[derive(bytemuck::Pod, bytemuck::Zeroable)] } diff --git a/lang/tests/generics_test.rs b/lang/tests/generics_test.rs index 3ecb8f3257..8458e61ec6 100644 --- a/lang/tests/generics_test.rs +++ b/lang/tests/generics_test.rs @@ -22,7 +22,7 @@ where pub associated: Account<'info, Associated>, } -#[account(zero_copy)] +#[account(zero_copy(unsafe_bytemuck_derives))] pub struct FooAccount { pub data: WrappedU8Array, }