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: Optimize trait Key implementation #652

Merged
merged 3 commits into from
Aug 31, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion lang/src/account_info.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -50,3 +50,9 @@ impl<'info> AccountsExit<'info> for AccountInfo<'info> {
Ok(())
}
}

impl<'info> Key for AccountInfo<'info> {
fn key(&self) -> Pubkey {
*self.key
}
}
8 changes: 7 additions & 1 deletion lang/src/cpi_account.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
}
}
18 changes: 16 additions & 2 deletions lang/src/cpi_state.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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
}
}
9 changes: 0 additions & 9 deletions lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion lang/src/loader.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()]
Expand All @@ -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
}
}
8 changes: 7 additions & 1 deletion lang/src/program_account.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
}
}
16 changes: 15 additions & 1 deletion lang/src/state.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
}
}
14 changes: 13 additions & 1 deletion lang/src/sysvar.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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
}
}