Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 6820ad7

Browse files
committed
Use load functions
1 parent 561e1a2 commit 6820ad7

21 files changed

+107
-110
lines changed

interface/src/state/account.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use pinocchio::pubkey::Pubkey;
22

3-
use super::{account_state::AccountState, COption, Initializable, RawType, Viewable};
3+
use super::{account_state::AccountState, COption, Initializable, RawType};
44

55
/// Incinerator address.
66
const INCINERATOR_ID: Pubkey =
@@ -145,5 +145,3 @@ impl Initializable for Account {
145145
self.state != AccountState::Uninitialized
146146
}
147147
}
148-
149-
impl Viewable<Account> for Account {}

interface/src/state/mint.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use pinocchio::pubkey::Pubkey;
22

3-
use super::{COption, Initializable, RawType, Viewable};
3+
use super::{COption, Initializable, RawType};
44

55
/// Internal representation of a mint data.
66
#[repr(C)]
@@ -94,5 +94,3 @@ impl Initializable for Mint {
9494
self.is_initialized == 1
9595
}
9696
}
97-
98-
impl Viewable<Mint> for Mint {}

interface/src/state/mod.rs

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -25,69 +25,68 @@ pub trait Initializable {
2525
fn is_initialized(&self) -> bool;
2626
}
2727

28-
/// Trait for `RawType`s that can be *viewed* from a byte slice.
29-
pub trait Viewable<T: Initializable + RawType> {
30-
/// Return a reference for an initialized `T` from the given bytes.
31-
///
32-
/// # Safety
33-
///
34-
/// The caller must ensure that `bytes` contains a valid representation of `T`.
35-
#[inline(always)]
36-
unsafe fn load(bytes: &[u8]) -> Result<&T, ProgramError> {
37-
Self::load_unchecked(bytes).and_then(|t| {
38-
// checks if the data is initialized
39-
if t.is_initialized() {
40-
Ok(t)
41-
} else {
42-
Err(ProgramError::UninitializedAccount)
43-
}
44-
})
45-
}
46-
47-
/// Return a `T` reference from the given bytes.
48-
///
49-
/// This function does not check if the data is initialized.
50-
///
51-
/// # Safety
52-
///
53-
/// The caller must ensure that `bytes` contains a valid representation of `T`.
54-
#[inline(always)]
55-
unsafe fn load_unchecked(bytes: &[u8]) -> Result<&T, ProgramError> {
56-
if bytes.len() != T::LEN {
57-
return Err(ProgramError::InvalidAccountData);
28+
/// Return a reference for an initialized `T` from the given bytes.
29+
///
30+
/// # Safety
31+
///
32+
/// The caller must ensure that `bytes` contains a valid representation of `T`.
33+
#[inline(always)]
34+
pub unsafe fn load<T: Initializable + RawType>(bytes: &[u8]) -> Result<&T, ProgramError> {
35+
load_unchecked(bytes).and_then(|t: &T| {
36+
// checks if the data is initialized
37+
if t.is_initialized() {
38+
Ok(t)
39+
} else {
40+
Err(ProgramError::UninitializedAccount)
5841
}
59-
Ok(&*(bytes.as_ptr() as *const T))
60-
}
42+
})
43+
}
6144

62-
/// Return a mutable reference for an initialized `T` from the given bytes.
63-
///
64-
/// # Safety
65-
///
66-
/// The caller must ensure that `bytes` contains a valid representation of `T`.
67-
#[inline(always)]
68-
unsafe fn load_mut(bytes: &mut [u8]) -> Result<&mut T, ProgramError> {
69-
Self::load_mut_unchecked(bytes).and_then(|t| {
70-
// checks if the data is initialized
71-
if t.is_initialized() {
72-
Ok(t)
73-
} else {
74-
Err(ProgramError::UninitializedAccount)
75-
}
76-
})
45+
/// Return a `T` reference from the given bytes.
46+
///
47+
/// This function does not check if the data is initialized.
48+
///
49+
/// # Safety
50+
///
51+
/// The caller must ensure that `bytes` contains a valid representation of `T`.
52+
#[inline(always)]
53+
pub unsafe fn load_unchecked<T: RawType>(bytes: &[u8]) -> Result<&T, ProgramError> {
54+
if bytes.len() != T::LEN {
55+
return Err(ProgramError::InvalidAccountData);
7756
}
57+
Ok(&*(bytes.as_ptr() as *const T))
58+
}
7859

79-
/// Return a mutable `T` reference from the given bytes.
80-
///
81-
/// This function does not check if the data is initialized.
82-
///
83-
/// # Safety
84-
///
85-
/// The caller must ensure that `bytes` contains a valid representation of `T`.
86-
#[inline(always)]
87-
unsafe fn load_mut_unchecked(bytes: &mut [u8]) -> Result<&mut T, ProgramError> {
88-
if bytes.len() != T::LEN {
89-
return Err(ProgramError::InvalidAccountData);
60+
/// Return a mutable reference for an initialized `T` from the given bytes.
61+
///
62+
/// # Safety
63+
///
64+
/// The caller must ensure that `bytes` contains a valid representation of `T`.
65+
#[inline(always)]
66+
pub unsafe fn load_mut<T: Initializable + RawType>(
67+
bytes: &mut [u8],
68+
) -> Result<&mut T, ProgramError> {
69+
load_mut_unchecked(bytes).and_then(|t: &mut T| {
70+
// checks if the data is initialized
71+
if t.is_initialized() {
72+
Ok(t)
73+
} else {
74+
Err(ProgramError::UninitializedAccount)
9075
}
91-
Ok(&mut *(bytes.as_mut_ptr() as *mut T))
76+
})
77+
}
78+
79+
/// Return a mutable `T` reference from the given bytes.
80+
///
81+
/// This function does not check if the data is initialized.
82+
///
83+
/// # Safety
84+
///
85+
/// The caller must ensure that `bytes` contains a valid representation of `T`.
86+
#[inline(always)]
87+
pub unsafe fn load_mut_unchecked<T: RawType>(bytes: &mut [u8]) -> Result<&mut T, ProgramError> {
88+
if bytes.len() != T::LEN {
89+
return Err(ProgramError::InvalidAccountData);
9290
}
91+
Ok(&mut *(bytes.as_mut_ptr() as *mut T))
9392
}

interface/src/state/multisig.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use pinocchio::pubkey::Pubkey;
22

3-
use super::{Initializable, RawType, Viewable};
3+
use super::{Initializable, RawType};
44

55
/// Minimum number of multisignature signers (min N)
66
pub const MIN_SIGNERS: usize = 1;
@@ -47,5 +47,3 @@ impl Initializable for Multisig {
4747
self.is_initialized == 1
4848
}
4949
}
50-
51-
impl Viewable<Multisig> for Multisig {}

program/src/processor/amount_to_ui_amount.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use pinocchio::{
55
use pinocchio_log::logger::{Argument, Logger};
66
use token_interface::{
77
error::TokenError,
8-
state::{mint::Mint, Viewable},
8+
state::{load, mint::Mint},
99
};
1010

1111
use super::{check_account_owner, MAX_DIGITS_U64};
@@ -31,7 +31,7 @@ pub fn process_amount_to_ui_amount(
3131
check_account_owner(mint_info)?;
3232
// SAFETY: there is a single borrow to the `Mint` account.
3333
let mint = unsafe {
34-
Mint::load(mint_info.borrow_data_unchecked()).map_err(|_| TokenError::InvalidMint)?
34+
load::<Mint>(mint_info.borrow_data_unchecked()).map_err(|_| TokenError::InvalidMint)?
3535
};
3636

3737
let mut logger = Logger::<MAX_UI_AMOUNT_LENGTH>::default();

program/src/processor/close_account.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use pinocchio::{
33
};
44
use token_interface::{
55
error::TokenError,
6-
state::{account::Account, Viewable},
6+
state::{account::Account, load_mut},
77
};
88

99
use super::validate_owner;
@@ -27,7 +27,7 @@ pub fn process_close_account(accounts: &[AccountInfo]) -> ProgramResult {
2727
}
2828

2929
let source_account =
30-
unsafe { Account::load_mut(source_account_info.borrow_mut_data_unchecked())? };
30+
unsafe { load_mut::<Account>(source_account_info.borrow_mut_data_unchecked())? };
3131

3232
if !source_account.is_native() && source_account.amount() != 0 {
3333
return Err(TokenError::NonNativeHasBalance.into());

program/src/processor/get_account_data_size.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use pinocchio::{
33
};
44
use token_interface::{
55
error::TokenError,
6-
state::{account::Account, mint::Mint, RawType, Viewable},
6+
state::{account::Account, load, mint::Mint, RawType},
77
};
88

99
use super::check_account_owner;
@@ -18,7 +18,7 @@ pub fn process_get_account_data_size(accounts: &[AccountInfo]) -> ProgramResult
1818
check_account_owner(mint_info)?;
1919

2020
let _ = unsafe {
21-
Mint::load(mint_info.borrow_data_unchecked()).map_err(|_| TokenError::InvalidMint)
21+
load::<Mint>(mint_info.borrow_data_unchecked()).map_err(|_| TokenError::InvalidMint)
2222
};
2323

2424
set_return_data(&Account::LEN.to_le_bytes());

program/src/processor/initialize_immutable_owner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use pinocchio::{account_info::AccountInfo, msg, program_error::ProgramError, ProgramResult};
22
use token_interface::{
33
error::TokenError,
4-
state::{account::Account, Initializable, Viewable},
4+
state::{account::Account, load_unchecked, Initializable},
55
};
66

77
#[inline(always)]
88
pub fn process_initialize_immutable_owner(accounts: &[AccountInfo]) -> ProgramResult {
99
let token_account_info = accounts.first().ok_or(ProgramError::NotEnoughAccountKeys)?;
1010

11-
let account = unsafe { Account::load_unchecked(token_account_info.borrow_data_unchecked())? };
11+
let account = unsafe { load_unchecked::<Account>(token_account_info.borrow_data_unchecked())? };
1212

1313
if account.is_initialized() {
1414
return Err(TokenError::AlreadyInUse.into());

program/src/processor/initialize_mint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use pinocchio::{
88
};
99
use token_interface::{
1010
error::TokenError,
11-
state::{mint::Mint, Initializable, Viewable},
11+
state::{load_mut_unchecked, mint::Mint, Initializable},
1212
};
1313

1414
#[inline(always)]
@@ -35,7 +35,7 @@ pub fn process_initialize_mint(
3535
(mint_info, None)
3636
};
3737

38-
let mint = unsafe { Mint::load_mut_unchecked(mint_info.borrow_mut_data_unchecked())? };
38+
let mint = unsafe { load_mut_unchecked::<Mint>(mint_info.borrow_mut_data_unchecked())? };
3939

4040
if mint.is_initialized() {
4141
return Err(TokenError::AlreadyInUse.into());

program/src/processor/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ use pinocchio::{
1111
use token_interface::{
1212
error::TokenError,
1313
state::{
14+
load,
1415
multisig::{Multisig, MAX_SIGNERS},
15-
RawType, Viewable,
16+
RawType,
1617
},
1718
};
1819

@@ -98,7 +99,7 @@ fn validate_owner(
9899
}
99100

100101
if owner_account_info.data_len() == Multisig::LEN && &crate::ID != owner_account_info.owner() {
101-
let multisig = unsafe { Multisig::load(owner_account_info.borrow_data_unchecked())? };
102+
let multisig = unsafe { load::<Multisig>(owner_account_info.borrow_data_unchecked())? };
102103

103104
let mut num_signers = 0;
104105
let mut matched = [false; MAX_SIGNERS];

0 commit comments

Comments
 (0)