@@ -318,7 +318,7 @@ impl Account {
318318 matches ! ( self . is_native, COption :: Some ( _) )
319319 }
320320
321- fn unpack_unchecked ( data : & [ u8 ] ) -> Result < Self , ProgramError > {
321+ fn unpack_from_slice ( data : & [ u8 ] ) -> Result < Self , ProgramError > {
322322 if data. len ( ) < Self :: LEN {
323323 return Err ( ProgramError :: InvalidAccountData ) ;
324324 }
@@ -348,18 +348,26 @@ impl Account {
348348 } )
349349 }
350350
351+ fn unpack_unchecked ( data : & [ u8 ] ) -> Result < Self , ProgramError > {
352+ Self :: unpack_from_slice ( data)
353+ }
354+
355+ fn pack_into_slice ( self , dst : & mut [ u8 ] ) {
356+ dst[ 0 ..32 ] . copy_from_slice ( self . mint . as_ref ( ) ) ;
357+ dst[ 32 ..64 ] . copy_from_slice ( self . owner . as_ref ( ) ) ;
358+ dst[ 64 ..72 ] . copy_from_slice ( & self . amount . to_le_bytes ( ) ) ;
359+ encode_coption_key ( & self . delegate , & mut dst[ 72 ..108 ] ) ;
360+ dst[ 108 ] = self . state as u8 ;
361+ encode_coption_u64 ( & self . is_native , & mut dst[ 109 ..121 ] ) ;
362+ dst[ 121 ..129 ] . copy_from_slice ( & self . delegated_amount . to_le_bytes ( ) ) ;
363+ encode_coption_key ( & self . close_authority , & mut dst[ 129 ..165 ] ) ;
364+ }
365+
351366 fn pack ( account : Account , dst : & mut [ u8 ] ) -> Result < ( ) , ProgramError > {
352367 if dst. len ( ) < Self :: LEN {
353368 return Err ( ProgramError :: InvalidAccountData ) ;
354369 }
355- dst[ 0 ..32 ] . copy_from_slice ( account. mint . as_ref ( ) ) ;
356- dst[ 32 ..64 ] . copy_from_slice ( account. owner . as_ref ( ) ) ;
357- dst[ 64 ..72 ] . copy_from_slice ( & account. amount . to_le_bytes ( ) ) ;
358- encode_coption_key ( & account. delegate , & mut dst[ 72 ..108 ] ) ;
359- dst[ 108 ] = account. state as u8 ;
360- encode_coption_u64 ( & account. is_native , & mut dst[ 109 ..121 ] ) ;
361- dst[ 121 ..129 ] . copy_from_slice ( & account. delegated_amount . to_le_bytes ( ) ) ;
362- encode_coption_key ( & account. close_authority , & mut dst[ 129 ..165 ] ) ;
370+ account. pack_into_slice ( dst) ;
363371 Ok ( ( ) )
364372 }
365373}
@@ -388,7 +396,7 @@ impl Default for Mint {
388396impl Mint {
389397 const LEN : usize = 82 ;
390398
391- fn unpack_unchecked ( data : & [ u8 ] ) -> Result < Self , ProgramError > {
399+ fn unpack_from_slice ( data : & [ u8 ] ) -> Result < Self , ProgramError > {
392400 if data. len ( ) < Self :: LEN {
393401 return Err ( ProgramError :: InvalidAccountData ) ;
394402 }
@@ -410,22 +418,23 @@ impl Mint {
410418 } )
411419 }
412420
421+ fn unpack_unchecked ( data : & [ u8 ] ) -> Result < Self , ProgramError > {
422+ Self :: unpack_from_slice ( data)
423+ }
424+
425+ fn pack_into_slice ( self , dst : & mut [ u8 ] ) {
426+ encode_coption_key ( & self . mint_authority , & mut dst[ 0 ..36 ] ) ;
427+ dst[ 36 ..44 ] . copy_from_slice ( & self . supply . to_le_bytes ( ) ) ;
428+ dst[ 44 ] = self . decimals ;
429+ dst[ 45 ] = self . is_initialized as u8 ;
430+ encode_coption_key ( & self . freeze_authority , & mut dst[ 46 ..82 ] ) ;
431+ }
432+
413433 fn pack ( mint : Mint , dst : & mut [ u8 ] ) -> Result < ( ) , ProgramError > {
414434 if dst. len ( ) < Self :: LEN {
415435 return Err ( ProgramError :: InvalidAccountData ) ;
416436 }
417- let Mint {
418- mint_authority,
419- supply,
420- decimals,
421- is_initialized,
422- freeze_authority,
423- } = mint;
424- encode_coption_key ( & mint_authority, & mut dst[ 0 ..36 ] ) ;
425- dst[ 36 ..44 ] . copy_from_slice ( & supply. to_le_bytes ( ) ) ;
426- dst[ 44 ] = decimals;
427- dst[ 45 ] = is_initialized as u8 ;
428- encode_coption_key ( & freeze_authority, & mut dst[ 46 ..82 ] ) ;
437+ mint. pack_into_slice ( dst) ;
429438 Ok ( ( ) )
430439 }
431440}
@@ -454,7 +463,7 @@ impl Default for Multisig {
454463impl Multisig {
455464 const LEN : usize = 355 ;
456465
457- fn unpack_unchecked ( data : & [ u8 ] ) -> Result < Self , ProgramError > {
466+ fn unpack_from_slice ( data : & [ u8 ] ) -> Result < Self , ProgramError > {
458467 if data. len ( ) < Self :: LEN {
459468 return Err ( ProgramError :: InvalidAccountData ) ;
460469 }
@@ -479,17 +488,25 @@ impl Multisig {
479488 } )
480489 }
481490
491+ fn unpack_unchecked ( data : & [ u8 ] ) -> Result < Self , ProgramError > {
492+ Self :: unpack_from_slice ( data)
493+ }
494+
495+ fn pack_into_slice ( self , dst : & mut [ u8 ] ) {
496+ dst[ 0 ] = self . m ;
497+ dst[ 1 ] = self . n ;
498+ dst[ 2 ] = self . is_initialized as u8 ;
499+ for ( i, signer) in self . signers . iter ( ) . enumerate ( ) {
500+ let start = 3 + i * 32 ;
501+ dst[ start..start + 32 ] . copy_from_slice ( signer. as_ref ( ) ) ;
502+ }
503+ }
504+
482505 fn pack ( multisig : Multisig , dst : & mut [ u8 ] ) -> Result < ( ) , ProgramError > {
483506 if dst. len ( ) < Self :: LEN {
484507 return Err ( ProgramError :: InvalidAccountData ) ;
485508 }
486- dst[ 0 ] = multisig. m ;
487- dst[ 1 ] = multisig. n ;
488- dst[ 2 ] = multisig. is_initialized as u8 ;
489- for ( i, signer) in multisig. signers . into_iter ( ) . enumerate ( ) {
490- let start = 3 + i * 32 ;
491- dst[ start..start + 32 ] . copy_from_slice ( signer. as_ref ( ) ) ;
492- }
509+ multisig. pack_into_slice ( dst) ;
493510 Ok ( ( ) )
494511 }
495512}
0 commit comments