lang: Optimize `trait Key` implementation (#652)

This commit is contained in:
Kirill Fomichev 2021-08-31 10:27:26 +03:00 committed by GitHub
parent 75c20856e4
commit c67dea4379
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 81 additions and 17 deletions

View File

@ -15,6 +15,8 @@ incremented for features.
* lang: Ignore `Unnamed` structs instead of panic ([#605](https://github.com/project-serum/anchor/pull/605)).
* lang: Add constraints for initializing mint accounts as pdas, `#[account(init, seeds = [...], mint::decimals = <expr>, mint::authority = <expr>)]` ([#562](https://github.com/project-serum/anchor/pull/562)).
* lang: Add `AsRef<AccountInfo>` for `AccountInfo` wrappers ([#652](https://github.com/project-serum/anchor/pull/652)).
* lang: Optimize `trait Key` by removing `AccountInfo` cloning ([#652](https://github.com/project-serum/anchor/pull/652)).
### Breaking Changes

View File

@ -1,5 +1,5 @@
use crate::error::ErrorCode;
use crate::{Accounts, AccountsExit, ToAccountInfo, ToAccountInfos, ToAccountMetas};
use crate::{Accounts, AccountsExit, Key, ToAccountInfo, ToAccountInfos, ToAccountMetas};
use solana_program::account_info::AccountInfo;
use solana_program::entrypoint::ProgramResult;
use solana_program::instruction::AccountMeta;
@ -50,3 +50,9 @@ impl<'info> AccountsExit<'info> for AccountInfo<'info> {
Ok(())
}
}
impl<'info> Key for AccountInfo<'info> {
fn key(&self) -> Pubkey {
*self.key
}
}

View File

@ -1,6 +1,6 @@
use crate::error::ErrorCode;
use crate::{
AccountDeserialize, Accounts, AccountsExit, ToAccountInfo, ToAccountInfos, ToAccountMetas,
AccountDeserialize, Accounts, AccountsExit, Key, ToAccountInfo, ToAccountInfos, ToAccountMetas,
};
use solana_program::account_info::AccountInfo;
use solana_program::entrypoint::ProgramResult;
@ -113,3 +113,9 @@ impl<'info, T: AccountDeserialize + Clone> AccountsExit<'info> for CpiAccount<'i
Ok(())
}
}
impl<'info, T: AccountDeserialize + Clone> Key for CpiAccount<'info, T> {
fn key(&self) -> Pubkey {
*self.info.key
}
}

View File

@ -1,7 +1,7 @@
use crate::error::ErrorCode;
use crate::{
AccountDeserialize, AccountSerialize, Accounts, AccountsExit, CpiStateContext, ProgramState,
ToAccountInfo, ToAccountInfos, ToAccountMetas,
AccountDeserialize, AccountSerialize, Accounts, AccountsExit, CpiStateContext, Key,
ProgramState, ToAccountInfo, ToAccountInfos, ToAccountMetas,
};
use solana_program::account_info::AccountInfo;
use solana_program::entrypoint::ProgramResult;
@ -110,6 +110,14 @@ impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountInfo<'inf
}
}
impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AsRef<AccountInfo<'info>>
for CpiState<'info, T>
{
fn as_ref(&self) -> &AccountInfo<'info> {
&self.inner.info
}
}
impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Deref for CpiState<'info, T> {
type Target = T;
@ -132,3 +140,9 @@ impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsExit<'info
Ok(())
}
}
impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Key for CpiState<'info, T> {
fn key(&self) -> Pubkey {
*self.inner.info.key
}
}

View File

@ -203,15 +203,6 @@ pub trait Key {
fn key(&self) -> Pubkey;
}
impl<'info, T> Key for T
where
T: ToAccountInfo<'info>,
{
fn key(&self) -> Pubkey {
*self.to_account_info().key
}
}
impl Key for Pubkey {
fn key(&self) -> Pubkey {
*self

View File

@ -1,6 +1,7 @@
use crate::error::ErrorCode;
use crate::{
Accounts, AccountsClose, AccountsExit, ToAccountInfo, ToAccountInfos, ToAccountMetas, ZeroCopy,
Accounts, AccountsClose, AccountsExit, Key, ToAccountInfo, ToAccountInfos, ToAccountMetas,
ZeroCopy,
};
use solana_program::account_info::AccountInfo;
use solana_program::entrypoint::ProgramResult;
@ -166,6 +167,12 @@ impl<'info, T: ZeroCopy> ToAccountMetas for Loader<'info, T> {
}
}
impl<'info, T: ZeroCopy> AsRef<AccountInfo<'info>> for Loader<'info, T> {
fn as_ref(&self) -> &AccountInfo<'info> {
&self.acc_info
}
}
impl<'info, T: ZeroCopy> ToAccountInfos<'info> for Loader<'info, T> {
fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
vec![self.acc_info.clone()]
@ -177,3 +184,9 @@ impl<'info, T: ZeroCopy> ToAccountInfo<'info> for Loader<'info, T> {
self.acc_info.clone()
}
}
impl<'info, T: ZeroCopy> Key for Loader<'info, T> {
fn key(&self) -> Pubkey {
*self.acc_info.key
}
}

View File

@ -1,6 +1,6 @@
use crate::error::ErrorCode;
use crate::{
AccountDeserialize, AccountSerialize, Accounts, AccountsClose, AccountsExit, CpiAccount,
AccountDeserialize, AccountSerialize, Accounts, AccountsClose, AccountsExit, CpiAccount, Key,
ToAccountInfo, ToAccountInfos, ToAccountMetas,
};
use solana_program::account_info::AccountInfo;
@ -163,3 +163,9 @@ where
Self::new(a.to_account_info(), Deref::deref(&a).clone())
}
}
impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Key for ProgramAccount<'info, T> {
fn key(&self) -> Pubkey {
*self.inner.info.key
}
}

View File

@ -1,6 +1,6 @@
use crate::error::ErrorCode;
use crate::{
AccountDeserialize, AccountSerialize, Accounts, AccountsExit, CpiAccount, ToAccountInfo,
AccountDeserialize, AccountSerialize, Accounts, AccountsExit, CpiAccount, Key, ToAccountInfo,
ToAccountInfos, ToAccountMetas,
};
use solana_program::account_info::AccountInfo;
@ -109,6 +109,14 @@ impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountInfo<'inf
}
}
impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AsRef<AccountInfo<'info>>
for ProgramState<'info, T>
{
fn as_ref(&self) -> &AccountInfo<'info> {
&self.inner.info
}
}
impl<'a, T: AccountSerialize + AccountDeserialize + Clone> Deref for ProgramState<'a, T> {
type Target = T;
@ -151,3 +159,9 @@ pub fn address(program_id: &Pubkey) -> Pubkey {
let owner = program_id;
Pubkey::create_with_seed(&base, seed, owner).unwrap()
}
impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Key for ProgramState<'info, T> {
fn key(&self) -> Pubkey {
*self.inner.info.key
}
}

View File

@ -1,5 +1,5 @@
use crate::error::ErrorCode;
use crate::{Accounts, AccountsExit, ToAccountInfo, ToAccountInfos, ToAccountMetas};
use crate::{Accounts, AccountsExit, Key, ToAccountInfo, ToAccountInfos, ToAccountMetas};
use solana_program::account_info::AccountInfo;
use solana_program::entrypoint::ProgramResult;
use solana_program::instruction::AccountMeta;
@ -60,6 +60,12 @@ impl<'info, T: solana_program::sysvar::Sysvar> ToAccountInfos<'info> for Sysvar<
}
}
impl<'info, T: solana_program::sysvar::Sysvar> AsRef<AccountInfo<'info>> for Sysvar<'info, T> {
fn as_ref(&self) -> &AccountInfo<'info> {
&self.info
}
}
impl<'a, T: solana_program::sysvar::Sysvar> Deref for Sysvar<'a, T> {
type Target = T;
@ -86,3 +92,9 @@ impl<'info, T: solana_program::sysvar::Sysvar> AccountsExit<'info> for Sysvar<'i
Ok(())
}
}
impl<'info, T: solana_program::sysvar::Sysvar> Key for Sysvar<'info, T> {
fn key(&self) -> Pubkey {
*self.info.key
}
}