@@ -11,61 +11,64 @@ pub mod instructions;
1111
1212pinocchio_pubkey:: declare_id!( "11111111111111111111111111111111" ) ;
1313
14- pub trait InvokeParts {
15- type Accounts ;
16- type AccountMetas ;
17- type InstructionData ;
14+ pub struct InvokeParts < ' a , const N : usize , const M : usize , const J : usize > {
15+ pub accounts : [ & ' a AccountInfo ; N ] ,
16+ pub account_metas : [ AccountMeta < ' a > ; M ] ,
17+ pub instruction_data : InstructionData < J > ,
18+ }
1819
19- fn accounts ( & self ) -> Self :: Accounts ;
20- fn account_metas ( & self ) -> Self :: AccountMetas ;
21- fn instruction_data ( & self ) -> ( Self :: InstructionData , usize ) ;
20+ pub enum InstructionData < const N : usize > {
21+ Full ( [ u8 ; N ] ) ,
22+ Truncated ( ( [ u8 ; N ] , usize ) ) ,
2223}
2324
24- pub trait Invoke < const N : usize > : Sized {
25+ impl < const N : usize > InstructionData < N > {
26+ pub fn data ( & self ) -> & [ u8 ] {
27+ match * self {
28+ InstructionData :: Full ( ref data) => data,
29+ InstructionData :: Truncated ( ( ref data, end) ) => & data[ ..end] ,
30+ }
31+ }
32+ }
33+
34+ pub trait Invoke < ' a , const N : usize , const M : usize , const J : usize > :
35+ Into < InvokeParts < ' a , N , M , J > >
36+ {
2537 fn invoke ( self ) -> pinocchio:: ProgramResult {
2638 self . invoke_signed ( & [ ] )
2739 }
2840
2941 fn invoke_signed ( self , signers : & [ Signer ] ) -> pinocchio:: ProgramResult {
30- self . invoke_invoker ( signers, |ix, acc, sigs| cpi:: invoke_signed ( & ix, acc, sigs) )
42+ invoke_invoker ( self . into ( ) , signers, |ix, acc, sigs| {
43+ cpi:: invoke_signed ( & ix, acc, sigs)
44+ } )
3145 }
3246
3347 unsafe fn invoke_access_unchecked ( self ) -> pinocchio:: ProgramResult {
3448 self . invoke_singed_access_unchecked ( & [ ] )
3549 }
3650
3751 unsafe fn invoke_singed_access_unchecked ( self , signers : & [ Signer ] ) -> pinocchio:: ProgramResult {
38- self . invoke_invoker ( signers, |ix, acc, sigs| unsafe {
52+ invoke_invoker ( self . into ( ) , signers, |ix, acc, sigs| unsafe {
3953 cpi:: invoke_signed_access_unchecked ( & ix, acc, sigs)
4054 } )
4155 }
42-
43- fn invoke_invoker (
44- self ,
45- signers : & [ Signer ] ,
46- invoker : impl FnOnce ( Instruction , & [ & AccountInfo ; N ] , & [ Signer ] ) -> pinocchio:: ProgramResult ,
47- ) -> pinocchio:: ProgramResult ;
4856}
4957
50- impl < ' a , const N : usize , const M : usize , const J : usize , T > Invoke < N > for T
51- where
52- T : InvokeParts <
53- Accounts = [ & ' a AccountInfo ; N ] ,
54- AccountMetas = [ AccountMeta < ' a > ; M ] ,
55- InstructionData = [ u8 ; J ] ,
56- > ,
58+ impl < ' a , const N : usize , const M : usize , const J : usize , T > Invoke < ' a , N , M , J > for T where
59+ InvokeParts < ' a , N , M , J > : From < T >
5760{
58- fn invoke_invoker (
59- self ,
60- signers : & [ Signer ] ,
61- invoker : impl FnOnce ( Instruction , & [ & AccountInfo ; N ] , & [ Signer ] ) -> pinocchio :: ProgramResult ,
62- ) -> pinocchio :: ProgramResult {
63- let ( data , end ) = self . instruction_data ( ) ;
64- let instruction = Instruction {
65- program_id : & crate :: ID ,
66- accounts : & self . account_metas ( ) ,
67- data : & data [ ..end ] ,
68- } ;
69- invoker ( instruction , & self . accounts ( ) , signers )
70- }
61+ }
62+
63+ fn invoke_invoker < ' a , const N : usize , const M : usize , const J : usize > (
64+ invoke_parts : InvokeParts < ' a , N , M , J > ,
65+ signers : & [ Signer ] ,
66+ invoker : impl FnOnce ( Instruction , & [ & AccountInfo ; N ] , & [ Signer ] ) -> pinocchio :: ProgramResult ,
67+ ) -> pinocchio :: ProgramResult {
68+ let instruction = Instruction {
69+ program_id : & crate :: ID ,
70+ accounts : & invoke_parts . account_metas ,
71+ data : invoke_parts . instruction_data . data ( ) ,
72+ } ;
73+ invoker ( instruction , & invoke_parts . accounts , signers )
7174}
0 commit comments