From ee7712de1ad9ce658e1a5b5c134d1529c62263a1 Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Thu, 16 Dec 2021 23:43:33 +0100 Subject: [PATCH 1/2] lang: add default impls for AccountSerialize and AccountDeserialize --- lang/src/lib.rs | 8 ++++++-- spl/src/token.rs | 27 ++------------------------- 2 files changed, 8 insertions(+), 27 deletions(-) diff --git a/lang/src/lib.rs b/lang/src/lib.rs index 4cda3d4247..6903667701 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -158,7 +158,9 @@ pub trait ToAccountInfo<'info> { /// [`#[account]`](./attr.account.html) attribute. pub trait AccountSerialize { /// Serializes the account data into `writer`. - fn try_serialize(&self, writer: &mut W) -> Result<(), ProgramError>; + fn try_serialize(&self, _writer: &mut W) -> Result<(), ProgramError> { + Ok(()) + } } /// A data structure that can be deserialized and stored into account storage, @@ -173,7 +175,9 @@ pub trait AccountDeserialize: Sized { /// For example, if the SPL token program were to implement this trait, /// it should be impossible to deserialize a `Mint` account into a token /// `Account`. - fn try_deserialize(buf: &mut &[u8]) -> Result; + fn try_deserialize(buf: &mut &[u8]) -> Result { + Self::try_deserialize_unchecked(buf) + } /// Deserializes account data without checking the account discriminator. /// This should only be used on account initialization, when the bytes of diff --git a/spl/src/token.rs b/spl/src/token.rs index 8655321cf0..be1b075287 100644 --- a/spl/src/token.rs +++ b/spl/src/token.rs @@ -5,7 +5,6 @@ use anchor_lang::solana_program::program_error::ProgramError; use anchor_lang::solana_program::program_pack::Pack; use anchor_lang::solana_program::pubkey::Pubkey; use anchor_lang::{Accounts, CpiContext}; -use std::io::Write; use std::ops::Deref; pub use spl_token::ID; @@ -321,21 +320,12 @@ impl TokenAccount { } impl anchor_lang::AccountDeserialize for TokenAccount { - fn try_deserialize(buf: &mut &[u8]) -> Result { - TokenAccount::try_deserialize_unchecked(buf) - } - fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result { spl_token::state::Account::unpack(buf).map(TokenAccount) } } -impl anchor_lang::AccountSerialize for TokenAccount { - fn try_serialize(&self, _writer: &mut W) -> Result<(), ProgramError> { - // no-op - Ok(()) - } -} +impl anchor_lang::AccountSerialize for TokenAccount {} impl anchor_lang::Owner for TokenAccount { fn owner() -> Pubkey { @@ -359,21 +349,12 @@ impl Mint { } impl anchor_lang::AccountDeserialize for Mint { - fn try_deserialize(buf: &mut &[u8]) -> Result { - Mint::try_deserialize_unchecked(buf) - } - fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result { spl_token::state::Mint::unpack(buf).map(Mint) } } -impl anchor_lang::AccountSerialize for Mint { - fn try_serialize(&self, _writer: &mut W) -> Result<(), ProgramError> { - // no-op - Ok(()) - } -} +impl anchor_lang::AccountSerialize for Mint {} impl anchor_lang::Owner for Mint { fn owner() -> Pubkey { @@ -393,10 +374,6 @@ impl Deref for Mint { pub struct Token; impl anchor_lang::AccountDeserialize for Token { - fn try_deserialize(buf: &mut &[u8]) -> Result { - Token::try_deserialize_unchecked(buf) - } - fn try_deserialize_unchecked(_buf: &mut &[u8]) -> Result { Ok(Token) } From 141a717a2d116e08847be0d89f4f46b6689a82f3 Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Fri, 17 Dec 2021 00:07:38 +0100 Subject: [PATCH 2/2] docs: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 007a68e42f..48f9ab139a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ incremented for features. * lang: Add `programdata_address: Option` field to `Program` account. Will be populated if account is a program owned by the upgradable bpf loader ([#1125](https://github.com/project-serum/anchor/pull/1125)) * lang,ts,ci,cli,docs: update solana toolchain to version 1.8.5([#1133](https://github.com/project-serum/anchor/pull/1133)) +* lang: Account wrappers for non-Anchor programs no longer have to implement the `serialize` function because it has a default impl now. Similarly, they no longer have to implement `try_deserialize` which now delegates to `try_deserialize_unchecked` by default([#1156](https://github.com/project-serum/anchor/pull/1156)). ## [0.19.0] - 2021-12-08