Skip to content

Commit

Permalink
lang: impl Key for Pubkey (#1601)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-schaaf authored Mar 12, 2022
1 parent ad8aec2 commit 54c07be
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ incremented for features.
* ts: Fix the loss of strict typing using the `methods` namespace on builder functions ([#1539](https://github.com/project-serum/anchor/pull/1539)).
* spl: Update `spl/governance` to use new errors ([#1582](https://github.com/project-serum/anchor/pull/1582)).
* client: Fix `Cluster`'s `FromStr` implementation ([#1362](https://github.com/project-serum/anchor/pull/1362)).
* lang: implement `Key` for `Pubkey` again, so `associated_token::*` constraints can use pubkey targets again ([#1601](https://github.com/project-serum/anchor/pull/1601)).

### Breaking

Expand Down
14 changes: 12 additions & 2 deletions lang/src/accounts/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
use crate::bpf_writer::BpfWriter;
use crate::error::ErrorCode;
use crate::*;
use crate::{
AccountDeserialize, AccountSerialize, Accounts, AccountsClose, AccountsExit, Key, Owner,
Result, ToAccountInfo, ToAccountInfos, ToAccountMetas,
};
use solana_program::account_info::AccountInfo;
use solana_program::instruction::AccountMeta;
use solana_program::pubkey::Pubkey;
use solana_program::system_program;
use std::collections::BTreeMap;
use std::fmt;
use std::ops::{Deref, DerefMut};
Expand Down Expand Up @@ -235,7 +239,7 @@ impl<'info, T: AccountSerialize + AccountDeserialize + Owner + Clone + fmt::Debu
}
}

impl<'a, T: AccountSerialize + AccountDeserialize + Owner + Clone> Account<'a, T> {
impl<'a, T: AccountSerialize + AccountDeserialize + crate::Owner + Clone> Account<'a, T> {
fn new(info: AccountInfo<'a>, account: T) -> Account<'a, T> {
Self { info, account }
}
Expand Down Expand Up @@ -411,3 +415,9 @@ impl<'a, T: AccountSerialize + AccountDeserialize + Owner + Clone> DerefMut for
&mut self.account
}
}

impl<'info, T: AccountSerialize + AccountDeserialize + Owner + Clone> Key for Account<'info, T> {
fn key(&self) -> Pubkey {
*self.info.key
}
}
8 changes: 7 additions & 1 deletion lang/src/accounts/account_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! should be used instead.
use crate::error::ErrorCode;
use crate::{Accounts, AccountsExit, Result, ToAccountInfos, ToAccountMetas};
use crate::{Accounts, AccountsExit, Key, Result, ToAccountInfos, ToAccountMetas};
use solana_program::account_info::AccountInfo;
use solana_program::instruction::AccountMeta;
use solana_program::pubkey::Pubkey;
Expand Down Expand Up @@ -43,3 +43,9 @@ impl<'info> ToAccountInfos<'info> for AccountInfo<'info> {
}

impl<'info> AccountsExit<'info> for AccountInfo<'info> {}

impl<'info> Key for AccountInfo<'info> {
fn key(&self) -> Pubkey {
*self.key
}
}
8 changes: 7 additions & 1 deletion lang/src/accounts/account_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::bpf_writer::BpfWriter;
use crate::error::ErrorCode;
use crate::{
Accounts, AccountsClose, AccountsExit, Owner, Result, ToAccountInfo, ToAccountInfos,
Accounts, AccountsClose, AccountsExit, Key, Owner, Result, ToAccountInfo, ToAccountInfos,
ToAccountMetas, ZeroCopy,
};
use arrayref::array_ref;
Expand Down Expand Up @@ -267,3 +267,9 @@ impl<'info, T: ZeroCopy + Owner> ToAccountInfos<'info> for AccountLoader<'info,
vec![self.acc_info.clone()]
}
}

impl<'info, T: ZeroCopy + Owner> Key for AccountLoader<'info, T> {
fn key(&self) -> Pubkey {
*self.acc_info.key
}
}
7 changes: 7 additions & 0 deletions lang/src/accounts/cpi_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,10 @@ where
Self::new(a.to_account_info(), Box::new(a.into_inner()))
}
}

#[allow(deprecated)]
impl<'info, T: AccountDeserialize + Clone> Key for CpiAccount<'info, T> {
fn key(&self) -> Pubkey {
*self.info.key
}
}
9 changes: 8 additions & 1 deletion lang/src/accounts/cpi_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::error::ErrorCode;
#[allow(deprecated)]
use crate::{accounts::state::ProgramState, context::CpiStateContext};
use crate::{
AccountDeserialize, AccountSerialize, Accounts, AccountsExit, Result, ToAccountInfos,
AccountDeserialize, AccountSerialize, Accounts, AccountsExit, Key, Result, ToAccountInfos,
ToAccountMetas,
};
use solana_program::account_info::AccountInfo;
Expand Down Expand Up @@ -139,3 +139,10 @@ impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsExit<'info
for CpiState<'info, T>
{
}

#[allow(deprecated)]
impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Key for CpiState<'info, T> {
fn key(&self) -> Pubkey {
*self.inner.info.key
}
}
11 changes: 9 additions & 2 deletions lang/src/accounts/loader.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::bpf_writer::BpfWriter;
use crate::error::ErrorCode;
use crate::{
Accounts, AccountsClose, AccountsExit, Result, ToAccountInfo, ToAccountInfos, ToAccountMetas,
ZeroCopy,
Accounts, AccountsClose, AccountsExit, Key, Result, ToAccountInfo, ToAccountInfos,
ToAccountMetas, ZeroCopy,
};
use arrayref::array_ref;
use solana_program::account_info::AccountInfo;
Expand Down Expand Up @@ -214,3 +214,10 @@ impl<'info, T: ZeroCopy> ToAccountInfos<'info> for Loader<'info, T> {
vec![self.acc_info.clone()]
}
}

#[allow(deprecated)]
impl<'info, T: ZeroCopy> Key for Loader<'info, T> {
fn key(&self) -> Pubkey {
*self.acc_info.key
}
}
11 changes: 9 additions & 2 deletions lang/src/accounts/program.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Type validating that the account is the given Program
use crate::error::ErrorCode;
use crate::*;
use crate::{
AccountDeserialize, Accounts, AccountsExit, Id, Key, Result, ToAccountInfos, ToAccountMetas,
};
use solana_program::account_info::AccountInfo;
use solana_program::bpf_loader_upgradeable::{self, UpgradeableLoaderState};
use solana_program::instruction::AccountMeta;
Expand Down Expand Up @@ -178,3 +179,9 @@ impl<'info, T: Id + Clone> Deref for Program<'info, T> {
}

impl<'info, T: AccountDeserialize + Id + Clone> AccountsExit<'info> for Program<'info, T> {}

impl<'info, T: AccountDeserialize + Id + Clone> Key for Program<'info, T> {
fn key(&self) -> Pubkey {
*self.info.key
}
}
9 changes: 8 additions & 1 deletion lang/src/accounts/program_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::accounts::cpi_account::CpiAccount;
use crate::bpf_writer::BpfWriter;
use crate::error::ErrorCode;
use crate::{
AccountDeserialize, AccountSerialize, Accounts, AccountsClose, AccountsExit, Result,
AccountDeserialize, AccountSerialize, Accounts, AccountsClose, AccountsExit, Key, Result,
ToAccountInfo, ToAccountInfos, ToAccountMetas,
};
use solana_program::account_info::AccountInfo;
Expand Down Expand Up @@ -184,3 +184,10 @@ where
Self::new(a.to_account_info(), Deref::deref(&a).clone())
}
}

#[allow(deprecated)]
impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Key for ProgramAccount<'info, T> {
fn key(&self) -> Pubkey {
*self.inner.info.key
}
}
8 changes: 7 additions & 1 deletion lang/src/accounts/signer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Type validating that the account signed the transaction
use crate::error::ErrorCode;
use crate::*;
use crate::{Accounts, AccountsExit, Key, Result, ToAccountInfos, ToAccountMetas};
use solana_program::account_info::AccountInfo;
use solana_program::instruction::AccountMeta;
use solana_program::pubkey::Pubkey;
Expand Down Expand Up @@ -103,3 +103,9 @@ impl<'info> Deref for Signer<'info> {
&self.info
}
}

impl<'info> Key for Signer<'info> {
fn key(&self) -> Pubkey {
*self.info.key
}
}
8 changes: 7 additions & 1 deletion lang/src/accounts/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::accounts::cpi_account::CpiAccount;
use crate::bpf_writer::BpfWriter;
use crate::error::ErrorCode;
use crate::{
AccountDeserialize, AccountSerialize, Accounts, AccountsExit, Result, ToAccountInfo,
AccountDeserialize, AccountSerialize, Accounts, AccountsExit, Key, Result, ToAccountInfo,
ToAccountInfos, ToAccountMetas,
};
use solana_program::account_info::AccountInfo;
Expand Down Expand Up @@ -161,3 +161,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
}
}
6 changes: 6 additions & 0 deletions lang/src/accounts/system_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,9 @@ impl<'info> Deref for SystemAccount<'info> {
&self.info
}
}

impl<'info> Key for SystemAccount<'info> {
fn key(&self) -> Pubkey {
*self.info.key
}
}
8 changes: 7 additions & 1 deletion lang/src/accounts/sysvar.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Type validating that the account is a sysvar and deserializing it
use crate::error::ErrorCode;
use crate::{Accounts, AccountsExit, Result, ToAccountInfos, ToAccountMetas};
use crate::{Accounts, AccountsExit, Key, Result, ToAccountInfos, ToAccountMetas};
use solana_program::account_info::AccountInfo;
use solana_program::instruction::AccountMeta;
use solana_program::pubkey::Pubkey;
Expand Down Expand Up @@ -114,3 +114,9 @@ impl<'a, T: solana_program::sysvar::Sysvar> DerefMut for Sysvar<'a, T> {
}

impl<'info, T: solana_program::sysvar::Sysvar> AccountsExit<'info> for Sysvar<'info, T> {}

impl<'info, T: solana_program::sysvar::Sysvar> Key for Sysvar<'info, T> {
fn key(&self) -> Pubkey {
*self.info.key
}
}
8 changes: 7 additions & 1 deletion lang/src/accounts/unchecked_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! that no checks are performed
use crate::error::ErrorCode;
use crate::{Accounts, AccountsExit, Result, ToAccountInfos, ToAccountMetas};
use crate::{Accounts, AccountsExit, Key, Result, ToAccountInfos, ToAccountMetas};
use solana_program::account_info::AccountInfo;
use solana_program::instruction::AccountMeta;
use solana_program::pubkey::Pubkey;
Expand Down Expand Up @@ -68,3 +68,9 @@ impl<'info> Deref for UncheckedAccount<'info> {
&self.0
}
}

impl<'info> Key for UncheckedAccount<'info> {
fn key(&self) -> Pubkey {
*self.0.key
}
}
7 changes: 2 additions & 5 deletions lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,9 @@ pub trait Key {
fn key(&self) -> Pubkey;
}

impl<'info, T> Key for T
where
T: AsRef<AccountInfo<'info>>,
{
impl Key for Pubkey {
fn key(&self) -> Pubkey {
*self.as_ref().key
*self
}
}

Expand Down
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2
.program_seed
.clone()
// If they specified a seeds::program to use when deriving the PDA, use it.
.map(|program_id| quote! { #program_id })
.map(|program_id| quote! { #program_id.key() })
// Otherwise fall back to the current program's program_id.
.unwrap_or(quote! { program_id });

Expand Down

0 comments on commit 54c07be

Please sign in to comment.