瀏覽代碼

lang: add default impls for AccountSerialize and AccountDeserialize (#1156)

Paul 3 年之前
父節點
當前提交
b5827c1b24
共有 3 個文件被更改,包括 13 次插入31 次删除
  1. 5 4
      CHANGELOG.md
  2. 6 2
      lang/src/lib.rs
  3. 2 25
      spl/src/token.rs

+ 5 - 4
CHANGELOG.md

@@ -20,16 +20,17 @@ incremented for features.
 ### Features
 
 * lang: Add `programdata_address: Option<Pubkey>` 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))
-* ts: Add optional commitment argument to `fetch` and `fetchMultiple` ([#1171](https://github.com/project-serum/anchor/pull/1171))
-* lang: Add `set_inner` method to `Account<'a, T>` to enable easy updates ([#1177](https://github.com/project-serum/anchor/pull/1177))
+* 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)).
+* lang: Add `set_inner` method to `Account<'a, T>` to enable easy updates ([#1177](https://github.com/project-serum/anchor/pull/1177)).
 * lang: Handle arrays with const as length ([#968](https://github.com/project-serum/anchor/pull/968)).
+* ts: Add optional commitment argument to `fetch` and `fetchMultiple` ([#1171](https://github.com/project-serum/anchor/pull/1171)).
 
 ### Breaking
 
 * client: Client::new and Client::new_with_options now accept `Rc<dyn Signer>` instead of `Keypair` ([#975](https://github.com/project-serum/anchor/pull/975)).
 * lang, ts: Change error enum name and message for 'wrong program ownership' account validation ([#1154](https://github.com/project-serum/anchor/pull/1154)).
- lang: Change from `#[repr(packed)]` to `#[repr(C)]` for zero copy accounts ([#1106](https://github.com/project-serum/anchor/pull/1106)).
+* lang: Change from `#[repr(packed)]` to `#[repr(C)]` for zero copy accounts ([#1106](https://github.com/project-serum/anchor/pull/1106)).
 
 ## [0.19.0] - 2021-12-08
 

+ 6 - 2
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<W: Write>(&self, writer: &mut W) -> Result<(), ProgramError>;
+    fn try_serialize<W: Write>(&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<Self, ProgramError>;
+    fn try_deserialize(buf: &mut &[u8]) -> Result<Self, ProgramError> {
+        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

+ 2 - 25
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;
@@ -311,21 +310,12 @@ impl TokenAccount {
 }
 
 impl anchor_lang::AccountDeserialize for TokenAccount {
-    fn try_deserialize(buf: &mut &[u8]) -> Result<Self, ProgramError> {
-        TokenAccount::try_deserialize_unchecked(buf)
-    }
-
     fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self, ProgramError> {
         spl_token::state::Account::unpack(buf).map(TokenAccount)
     }
 }
 
-impl anchor_lang::AccountSerialize for TokenAccount {
-    fn try_serialize<W: Write>(&self, _writer: &mut W) -> Result<(), ProgramError> {
-        // no-op
-        Ok(())
-    }
-}
+impl anchor_lang::AccountSerialize for TokenAccount {}
 
 impl anchor_lang::Owner for TokenAccount {
     fn owner() -> Pubkey {
@@ -349,21 +339,12 @@ impl Mint {
 }
 
 impl anchor_lang::AccountDeserialize for Mint {
-    fn try_deserialize(buf: &mut &[u8]) -> Result<Self, ProgramError> {
-        Mint::try_deserialize_unchecked(buf)
-    }
-
     fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self, ProgramError> {
         spl_token::state::Mint::unpack(buf).map(Mint)
     }
 }
 
-impl anchor_lang::AccountSerialize for Mint {
-    fn try_serialize<W: Write>(&self, _writer: &mut W) -> Result<(), ProgramError> {
-        // no-op
-        Ok(())
-    }
-}
+impl anchor_lang::AccountSerialize for Mint {}
 
 impl anchor_lang::Owner for Mint {
     fn owner() -> Pubkey {
@@ -383,10 +364,6 @@ impl Deref for Mint {
 pub struct Token;
 
 impl anchor_lang::AccountDeserialize for Token {
-    fn try_deserialize(buf: &mut &[u8]) -> Result<Self, ProgramError> {
-        Token::try_deserialize_unchecked(buf)
-    }
-
     fn try_deserialize_unchecked(_buf: &mut &[u8]) -> Result<Self, ProgramError> {
         Ok(Token)
     }