Skip to content

Commit adc4e0a

Browse files
committed
chore : cleanup token crate
1 parent 1c80d4e commit adc4e0a

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

programs/token/src/instructions/mint_to.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::{write_bytes, UNINIT_BYTE};
1515
/// 0. `[WRITE]` The mint.
1616
/// 1. `[WRITE]` The account to mint tokens to.
1717
/// 2. `[SIGNER]` The mint's minting authority.
18-
///
1918
pub struct MintTo<'a> {
2019
/// Mint Account.
2120
pub mint: &'a AccountInfo,

programs/token/src/instructions/mint_to_checked.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::{write_bytes, UNINIT_BYTE};
1515
/// 0. `[WRITE]` The mint.
1616
/// 1. `[WRITE]` The account to mint tokens to.
1717
/// 2. `[SIGNER]` The mint's minting authority.
18-
///
1918
pub struct MintToChecked<'a> {
2019
/// Mint Account.
2120
pub mint: &'a AccountInfo,

programs/token/src/state/mint.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl Mint {
5151
return Err(ProgramError::InvalidAccountOwner);
5252
}
5353
Ok(Ref::map(account_info.try_borrow_data()?, |data| unsafe {
54-
Self::from_bytes(data)
54+
Self::from_bytes_unchecked(data)
5555
}))
5656
}
5757

@@ -74,16 +74,21 @@ impl Mint {
7474
if account_info.owner() != &ID {
7575
return Err(ProgramError::InvalidAccountOwner);
7676
}
77-
Ok(Self::from_bytes(account_info.borrow_data_unchecked()))
77+
Ok(Self::from_bytes_unchecked(
78+
account_info.borrow_data_unchecked(),
79+
))
7880
}
7981

8082
/// Return a `Mint` from the given bytes.
8183
///
8284
/// # Safety
8385
///
84-
/// The caller must ensure that `bytes` contains a valid representation of `Mint`.
86+
/// The caller must ensure that `bytes` contains a valid representation of `Mint`, and
87+
/// it is properly aligned to be interpreted as an instance of `Mint`.
88+
/// At the moment `Mint` has an alignment of 1 byte.
89+
/// This method does not perform a length validation.
8590
#[inline(always)]
86-
pub unsafe fn from_bytes(bytes: &[u8]) -> &Self {
91+
pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self {
8792
&*(bytes.as_ptr() as *const Mint)
8893
}
8994

@@ -110,7 +115,7 @@ impl Mint {
110115
}
111116

112117
pub fn supply(&self) -> u64 {
113-
unsafe { core::ptr::read_unaligned(self.supply.as_ptr() as *const u64) }
118+
u64::from_le_bytes(self.supply)
114119
}
115120

116121
pub fn decimals(&self) -> u8 {

programs/token/src/state/token.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl TokenAccount {
6666
return Err(ProgramError::InvalidAccountData);
6767
}
6868
Ok(Ref::map(account_info.try_borrow_data()?, |data| unsafe {
69-
Self::from_bytes(data)
69+
Self::from_bytes_unchecked(data)
7070
}))
7171
}
7272

@@ -89,16 +89,21 @@ impl TokenAccount {
8989
if account_info.owner() != &ID {
9090
return Err(ProgramError::InvalidAccountData);
9191
}
92-
Ok(Self::from_bytes(account_info.borrow_data_unchecked()))
92+
Ok(Self::from_bytes_unchecked(
93+
account_info.borrow_data_unchecked(),
94+
))
9395
}
9496

9597
/// Return a `TokenAccount` from the given bytes.
9698
///
9799
/// # Safety
98100
///
99-
/// The caller must ensure that `bytes` contains a valid representation of `TokenAccount`.
101+
/// The caller must ensure that `bytes` contains a valid representation of `TokenAccount`, and
102+
/// it is properly aligned to be interpreted as an instance of `TokenAccount`.
103+
/// At the moment `TokenAccount` has an alignment of 1 byte.
104+
/// This method does not perform a length validation.
100105
#[inline(always)]
101-
pub unsafe fn from_bytes(bytes: &[u8]) -> &Self {
106+
pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self {
102107
&*(bytes.as_ptr() as *const TokenAccount)
103108
}
104109

@@ -111,7 +116,7 @@ impl TokenAccount {
111116
}
112117

113118
pub fn amount(&self) -> u64 {
114-
unsafe { core::ptr::read_unaligned(self.amount.as_ptr() as *const u64) }
119+
u64::from_le_bytes(self.amount)
115120
}
116121

117122
#[inline(always)]
@@ -157,11 +162,11 @@ impl TokenAccount {
157162
/// skips the `Option` check.
158163
#[inline(always)]
159164
pub fn native_amount_unchecked(&self) -> u64 {
160-
unsafe { core::ptr::read_unaligned(self.native_amount.as_ptr() as *const u64) }
165+
u64::from_le_bytes(self.native_amount)
161166
}
162167

163168
pub fn delegated_amount(&self) -> u64 {
164-
unsafe { core::ptr::read_unaligned(self.delegated_amount.as_ptr() as *const u64) }
169+
u64::from_le_bytes(self.delegated_amount)
165170
}
166171

167172
#[inline(always)]

0 commit comments

Comments
 (0)