Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lang: add default impls for AccountSerialize and AccountDeserialize #1156

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions lang/src/lib.rs
Expand Up @@ -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,
Expand All @@ -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
Expand Down
27 changes: 2 additions & 25 deletions spl/src/token.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down