Skip to content

Commit

Permalink
lang: Add deprecated attribute to Loader (#1078)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-schaaf committed Nov 29, 2021
1 parent 7c479ad commit 926c024
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ incremented for features.

* lang: Add `deprecated` attribute to `ProgramAccount` ([#1014](https://github.com/project-serum/anchor/pull/1014)).
* cli: Add version number from programs `Cargo.toml` into extracted IDL
* lang: Add `deprecated` attribute to `Loader`([#1078](https://github.com/project-serum/anchor/pull/1078))

### Features

Expand Down
7 changes: 4 additions & 3 deletions lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub use crate::cpi_account::CpiAccount;
#[doc(hidden)]
#[allow(deprecated)]
pub use crate::cpi_state::CpiState;
#[allow(deprecated)]
pub use crate::loader::Loader;
pub use crate::loader_account::AccountLoader;
pub use crate::program::Program;
Expand Down Expand Up @@ -251,13 +252,13 @@ pub mod prelude {
pub use super::{
access_control, account, declare_id, emit, error, event, interface, program, require,
state, zero_copy, Account, AccountDeserialize, AccountLoader, AccountSerialize, Accounts,
AccountsExit, AnchorDeserialize, AnchorSerialize, Context, CpiContext, Id, Key, Loader,
Owner, Program, Signer, System, SystemAccount, Sysvar, ToAccountInfo, ToAccountInfos,
AccountsExit, AnchorDeserialize, AnchorSerialize, Context, CpiContext, Id, Key, Owner,
Program, Signer, System, SystemAccount, Sysvar, ToAccountInfo, ToAccountInfos,
ToAccountMetas, UncheckedAccount,
};

#[allow(deprecated)]
pub use super::{CpiAccount, CpiState, CpiStateContext, ProgramAccount, ProgramState};
pub use super::{CpiAccount, CpiState, CpiStateContext, Loader, ProgramAccount, ProgramState};

pub use borsh;
pub use solana_program::account_info::{next_account_info, AccountInfo};
Expand Down
16 changes: 16 additions & 0 deletions lang/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ use std::ops::DerefMut;
/// induce a `RefCell` panic, especially when sharing accounts across CPI
/// boundaries. When in doubt, one should make sure all refs resulting from a
/// call to `load` are dropped before CPI.
#[deprecated(since = "0.18.0", note = "Please use AccountLoader instead")]
pub struct Loader<'info, T: ZeroCopy> {
acc_info: AccountInfo<'info>,
phantom: PhantomData<&'info T>,
}

#[allow(deprecated)]
impl<'info, T: ZeroCopy + fmt::Debug> fmt::Debug for Loader<'info, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Loader")
Expand All @@ -39,6 +41,7 @@ impl<'info, T: ZeroCopy + fmt::Debug> fmt::Debug for Loader<'info, T> {
}
}

#[allow(deprecated)]
impl<'info, T: ZeroCopy> Loader<'info, T> {
fn new(acc_info: AccountInfo<'info>) -> Loader<'info, T> {
Self {
Expand All @@ -49,6 +52,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {

/// Constructs a new `Loader` from a previously initialized account.
#[inline(never)]
#[allow(deprecated)]
pub fn try_from(
program_id: &Pubkey,
acc_info: &AccountInfo<'info>,
Expand All @@ -68,6 +72,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
}

/// Constructs a new `Loader` from an uninitialized account.
#[allow(deprecated)]
#[inline(never)]
pub fn try_from_unchecked(
program_id: &Pubkey,
Expand All @@ -80,6 +85,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
}

/// Returns a Ref to the account data structure for reading.
#[allow(deprecated)]
pub fn load(&self) -> Result<Ref<T>, ProgramError> {
let data = self.acc_info.try_borrow_data()?;

Expand All @@ -93,6 +99,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
}

/// Returns a `RefMut` to the account data structure for reading or writing.
#[allow(deprecated)]
pub fn load_mut(&self) -> Result<RefMut<T>, ProgramError> {
// AccountInfo api allows you to borrow mut even if the account isn't
// writable, so add this check for a better dev experience.
Expand All @@ -115,6 +122,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {

/// Returns a `RefMut` to the account data structure for reading or writing.
/// Should only be called once, when the account is being initialized.
#[allow(deprecated)]
pub fn load_init(&self) -> Result<RefMut<T>, ProgramError> {
// AccountInfo api allows you to borrow mut even if the account isn't
// writable, so add this check for a better dev experience.
Expand All @@ -138,6 +146,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
}
}

#[allow(deprecated)]
impl<'info, T: ZeroCopy> Accounts<'info> for Loader<'info, T> {
#[inline(never)]
fn try_accounts(
Expand All @@ -155,6 +164,7 @@ impl<'info, T: ZeroCopy> Accounts<'info> for Loader<'info, T> {
}
}

#[allow(deprecated)]
impl<'info, T: ZeroCopy> AccountsExit<'info> for Loader<'info, T> {
// The account *cannot* be loaded when this is called.
fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
Expand All @@ -166,12 +176,14 @@ impl<'info, T: ZeroCopy> AccountsExit<'info> for Loader<'info, T> {
}
}

#[allow(deprecated)]
impl<'info, T: ZeroCopy> AccountsClose<'info> for Loader<'info, T> {
fn close(&self, sol_destination: AccountInfo<'info>) -> ProgramResult {
crate::common::close(self.to_account_info(), sol_destination)
}
}

#[allow(deprecated)]
impl<'info, T: ZeroCopy> ToAccountMetas for Loader<'info, T> {
fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
let is_signer = is_signer.unwrap_or(self.acc_info.is_signer);
Expand All @@ -183,24 +195,28 @@ impl<'info, T: ZeroCopy> ToAccountMetas for Loader<'info, T> {
}
}

#[allow(deprecated)]
impl<'info, T: ZeroCopy> AsRef<AccountInfo<'info>> for Loader<'info, T> {
fn as_ref(&self) -> &AccountInfo<'info> {
&self.acc_info
}
}

#[allow(deprecated)]
impl<'info, T: ZeroCopy> ToAccountInfos<'info> for Loader<'info, T> {
fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
vec![self.acc_info.clone()]
}
}

#[allow(deprecated)]
impl<'info, T: ZeroCopy> ToAccountInfo<'info> for Loader<'info, T> {
fn to_account_info(&self) -> AccountInfo<'info> {
self.acc_info.clone()
}
}

#[allow(deprecated)]
impl<'info, T: ZeroCopy> Key for Loader<'info, T> {
fn key(&self) -> Pubkey {
*self.acc_info.key
Expand Down
3 changes: 2 additions & 1 deletion lang/tests/generics_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ where
{
pub non_generic: AccountInfo<'info>,
pub generic: Account<'info, T>,
pub const_generic: Loader<'info, FooAccount<N>>,

pub const_generic: AccountLoader<'info, FooAccount<N>>,
pub const_generic_loader: AccountLoader<'info, FooAccount<N>>,
pub associated: Account<'info, Associated<U>>,
}
Expand Down

0 comments on commit 926c024

Please sign in to comment.