فهرست منبع

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

Kirill Fomichev 4 سال پیش
والد
کامیت
c67dea4379
9فایلهای تغییر یافته به همراه81 افزوده شده و 17 حذف شده
  1. 2 0
      CHANGELOG.md
  2. 7 1
      lang/src/account_info.rs
  3. 7 1
      lang/src/cpi_account.rs
  4. 16 2
      lang/src/cpi_state.rs
  5. 0 9
      lang/src/lib.rs
  6. 14 1
      lang/src/loader.rs
  7. 7 1
      lang/src/program_account.rs
  8. 15 1
      lang/src/state.rs
  9. 13 1
      lang/src/sysvar.rs

+ 2 - 0
CHANGELOG.md

@@ -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
 

+ 7 - 1
lang/src/account_info.rs

@@ -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
+    }
+}

+ 7 - 1
lang/src/cpi_account.rs

@@ -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
+    }
+}

+ 16 - 2
lang/src/cpi_state.rs

@@ -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
+    }
+}

+ 0 - 9
lang/src/lib.rs

@@ -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

+ 14 - 1
lang/src/loader.rs

@@ -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
+    }
+}

+ 7 - 1
lang/src/program_account.rs

@@ -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
+    }
+}

+ 15 - 1
lang/src/state.rs

@@ -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
+    }
+}

+ 13 - 1
lang/src/sysvar.rs

@@ -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
+    }
+}