24
24
transfer_fee:: { TransferFeeAmount , TransferFeeConfig } ,
25
25
transfer_hook:: { TransferHook , TransferHookAccount } ,
26
26
} ,
27
- state:: { Account , Mint , Multisig } ,
27
+ state:: { Account , Mint , Multisig , PackedSizeOf } ,
28
28
} ,
29
29
bytemuck:: { Pod , Zeroable } ,
30
30
num_enum:: { IntoPrimitive , TryFromPrimitive } ,
@@ -298,7 +298,7 @@ fn type_and_tlv_indices<S: BaseState>(
298
298
if rest_input. is_empty ( ) {
299
299
Ok ( None )
300
300
} else {
301
- let account_type_index = BASE_ACCOUNT_LENGTH . saturating_sub ( S :: LEN ) ;
301
+ let account_type_index = BASE_ACCOUNT_LENGTH . saturating_sub ( S :: SIZE_OF ) ;
302
302
// check padding is all zeroes
303
303
let tlv_start_index = account_type_index. saturating_add ( size_of :: < AccountType > ( ) ) ;
304
304
if rest_input. len ( ) <= tlv_start_index {
@@ -427,7 +427,7 @@ pub trait BaseStateWithExtensions<S: BaseState> {
427
427
fn try_get_account_len ( & self ) -> Result < usize , ProgramError > {
428
428
let tlv_info = get_tlv_data_info ( self . get_tlv_data ( ) ) ?;
429
429
if tlv_info. extension_types . is_empty ( ) {
430
- Ok ( S :: LEN )
430
+ Ok ( S :: SIZE_OF )
431
431
} else {
432
432
let total_len = tlv_info
433
433
. used_len
@@ -468,13 +468,13 @@ pub struct StateWithExtensionsOwned<S: BaseState> {
468
468
/// Raw TLV data, deserialized on demand
469
469
tlv_data : Vec < u8 > ,
470
470
}
471
- impl < S : BaseState > StateWithExtensionsOwned < S > {
471
+ impl < S : BaseState + Pack > StateWithExtensionsOwned < S > {
472
472
/// Unpack base state, leaving the extension data as a slice
473
473
///
474
474
/// Fails if the base state is not initialized.
475
475
pub fn unpack ( mut input : Vec < u8 > ) -> Result < Self , ProgramError > {
476
- check_min_len_and_not_multisig ( & input, S :: LEN ) ?;
477
- let mut rest = input. split_off ( S :: LEN ) ;
476
+ check_min_len_and_not_multisig ( & input, S :: SIZE_OF ) ?;
477
+ let mut rest = input. split_off ( S :: SIZE_OF ) ;
478
478
let base = S :: unpack ( & input) ?;
479
479
if let Some ( ( account_type_index, tlv_start_index) ) = type_and_tlv_indices :: < S > ( & rest) ? {
480
480
// type_and_tlv_indices() checks that returned indexes are within range
@@ -501,19 +501,19 @@ impl<S: BaseState> BaseStateWithExtensions<S> for StateWithExtensionsOwned<S> {
501
501
/// Encapsulates immutable base state data (mint or account) with possible
502
502
/// extensions
503
503
#[ derive( Debug , PartialEq ) ]
504
- pub struct StateWithExtensions < ' data , S : BaseState > {
504
+ pub struct StateWithExtensions < ' data , S : BaseState + Pack > {
505
505
/// Unpacked base data
506
506
pub base : S ,
507
507
/// Slice of data containing all TLV data, deserialized on demand
508
508
tlv_data : & ' data [ u8 ] ,
509
509
}
510
- impl < ' data , S : BaseState > StateWithExtensions < ' data , S > {
510
+ impl < ' data , S : BaseState + Pack > StateWithExtensions < ' data , S > {
511
511
/// Unpack base state, leaving the extension data as a slice
512
512
///
513
513
/// Fails if the base state is not initialized.
514
514
pub fn unpack ( input : & ' data [ u8 ] ) -> Result < Self , ProgramError > {
515
- check_min_len_and_not_multisig ( input, S :: LEN ) ?;
516
- let ( base_data, rest) = input. split_at ( S :: LEN ) ;
515
+ check_min_len_and_not_multisig ( input, S :: SIZE_OF ) ?;
516
+ let ( base_data, rest) = input. split_at ( S :: SIZE_OF ) ;
517
517
let base = S :: unpack ( base_data) ?;
518
518
if let Some ( ( account_type_index, tlv_start_index) ) = type_and_tlv_indices :: < S > ( rest) ? {
519
519
// type_and_tlv_indices() checks that returned indexes are within range
@@ -532,7 +532,7 @@ impl<'data, S: BaseState> StateWithExtensions<'data, S> {
532
532
}
533
533
}
534
534
}
535
- impl < ' a , S : BaseState > BaseStateWithExtensions < S > for StateWithExtensions < ' a , S > {
535
+ impl < ' a , S : BaseState + Pack > BaseStateWithExtensions < S > for StateWithExtensions < ' a , S > {
536
536
fn get_tlv_data ( & self ) -> & [ u8 ] {
537
537
self . tlv_data
538
538
}
@@ -784,13 +784,13 @@ pub struct StateWithExtensionsMut<'data, S: BaseState> {
784
784
/// Slice of data containing all TLV data, deserialized on demand
785
785
tlv_data : & ' data mut [ u8 ] ,
786
786
}
787
- impl < ' data , S : BaseState > StateWithExtensionsMut < ' data , S > {
787
+ impl < ' data , S : BaseState + Pack > StateWithExtensionsMut < ' data , S > {
788
788
/// Unpack base state, leaving the extension data as a mutable slice
789
789
///
790
790
/// Fails if the base state is not initialized.
791
791
pub fn unpack ( input : & ' data mut [ u8 ] ) -> Result < Self , ProgramError > {
792
- check_min_len_and_not_multisig ( input, S :: LEN ) ?;
793
- let ( base_data, rest) = input. split_at_mut ( S :: LEN ) ;
792
+ check_min_len_and_not_multisig ( input, S :: SIZE_OF ) ?;
793
+ let ( base_data, rest) = input. split_at_mut ( S :: SIZE_OF ) ;
794
794
let base = S :: unpack ( base_data) ?;
795
795
let ( account_type, tlv_data) = unpack_type_and_tlv_data :: < S > ( rest) ?;
796
796
Ok ( Self {
@@ -806,8 +806,8 @@ impl<'data, S: BaseState> StateWithExtensionsMut<'data, S> {
806
806
///
807
807
/// Fails if the base state has already been initialized.
808
808
pub fn unpack_uninitialized ( input : & ' data mut [ u8 ] ) -> Result < Self , ProgramError > {
809
- check_min_len_and_not_multisig ( input, S :: LEN ) ?;
810
- let ( base_data, rest) = input. split_at_mut ( S :: LEN ) ;
809
+ check_min_len_and_not_multisig ( input, S :: SIZE_OF ) ?;
810
+ let ( base_data, rest) = input. split_at_mut ( S :: SIZE_OF ) ;
811
811
let base = S :: unpack_unchecked ( base_data) ?;
812
812
if base. is_initialized ( ) {
813
813
return Err ( TokenError :: AlreadyInUse . into ( ) ) ;
@@ -892,8 +892,8 @@ fn unpack_uninitialized_type_and_tlv_data<S: BaseState>(
892
892
/// This method assumes that the `base_data` has already been packed with data
893
893
/// of the desired type.
894
894
pub fn set_account_type < S : BaseState > ( input : & mut [ u8 ] ) -> Result < ( ) , ProgramError > {
895
- check_min_len_and_not_multisig ( input, S :: LEN ) ?;
896
- let ( base_data, rest) = input. split_at_mut ( S :: LEN ) ;
895
+ check_min_len_and_not_multisig ( input, S :: SIZE_OF ) ?;
896
+ let ( base_data, rest) = input. split_at_mut ( S :: SIZE_OF ) ;
897
897
if S :: ACCOUNT_TYPE == AccountType :: Account && !is_initialized_account ( base_data) ? {
898
898
return Err ( ProgramError :: InvalidAccountData ) ;
899
899
}
@@ -1113,7 +1113,7 @@ impl ExtensionType {
1113
1113
extension_types : & [ Self ] ,
1114
1114
) -> Result < usize , ProgramError > {
1115
1115
if extension_types. is_empty ( ) {
1116
- Ok ( S :: LEN )
1116
+ Ok ( S :: SIZE_OF )
1117
1117
} else {
1118
1118
let extension_size = Self :: try_get_total_tlv_len ( extension_types) ?;
1119
1119
let total_len = extension_size. saturating_add ( BASE_ACCOUNT_AND_TYPE_LENGTH ) ;
@@ -1216,7 +1216,7 @@ impl ExtensionType {
1216
1216
}
1217
1217
1218
1218
/// Trait for base states, specifying the associated enum
1219
- pub trait BaseState : Pack + IsInitialized {
1219
+ pub trait BaseState : PackedSizeOf + IsInitialized {
1220
1220
/// Associated extension type enum, checked at the start of TLV entries
1221
1221
const ACCOUNT_TYPE : AccountType ;
1222
1222
}
@@ -1287,7 +1287,7 @@ impl Extension for AccountPaddingTest {
1287
1287
/// NOTE: Since this function deals with fixed-size extensions, it does not
1288
1288
/// handle _decreasing_ the size of an account's data buffer, like the function
1289
1289
/// `alloc_and_serialize_variable_len_extension` does.
1290
- pub fn alloc_and_serialize < S : BaseState , V : Default + Extension + Pod > (
1290
+ pub fn alloc_and_serialize < S : BaseState + Pack , V : Default + Extension + Pod > (
1291
1291
account_info : & AccountInfo ,
1292
1292
new_extension : & V ,
1293
1293
overwrite : bool ,
@@ -1324,7 +1324,10 @@ pub fn alloc_and_serialize<S: BaseState, V: Default + Extension + Pod>(
1324
1324
///
1325
1325
/// NOTE: Unlike the `reallocate` instruction, this function will reduce the
1326
1326
/// size of an account if it has too many bytes allocated for the given value.
1327
- pub fn alloc_and_serialize_variable_len_extension < S : BaseState , V : Extension + VariableLenPack > (
1327
+ pub fn alloc_and_serialize_variable_len_extension <
1328
+ S : BaseState + Pack ,
1329
+ V : Extension + VariableLenPack ,
1330
+ > (
1328
1331
account_info : & AccountInfo ,
1329
1332
new_extension : & V ,
1330
1333
overwrite : bool ,
0 commit comments