@@ -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+ }
19+
20+ pub enum InstructionData < const N : usize > {
21+ Full ( [ u8 ; N ] ) ,
22+ Truncated ( ( [ u8 ; N ] , usize ) ) ,
23+ }
1824
19- fn accounts ( & self ) -> Self :: Accounts ;
20- fn account_metas ( & self ) -> Self :: AccountMetas ;
21- fn instruction_data ( & self ) -> ( Self :: InstructionData , usize ) ;
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+ }
2232}
2333
24- pub trait Invoke < const N : usize > : Sized {
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+ Self :: 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+ Self :: invoke_invoker ( self . into ( ) , signers, |ix, acc, sigs| unsafe {
3953 cpi:: invoke_signed_access_unchecked ( & ix, acc, sigs)
4054 } )
4155 }
4256
4357 fn invoke_invoker (
44- self ,
45- signers : & [ Signer ] ,
46- invoker : impl FnOnce ( Instruction , & [ & AccountInfo ; N ] , & [ Signer ] ) -> pinocchio:: ProgramResult ,
47- ) -> pinocchio:: ProgramResult ;
48- }
49-
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- > ,
57- {
58- fn invoke_invoker (
59- self ,
58+ invoke_parts : InvokeParts < ' a , N , M , J > ,
6059 signers : & [ Signer ] ,
6160 invoker : impl FnOnce ( Instruction , & [ & AccountInfo ; N ] , & [ Signer ] ) -> pinocchio:: ProgramResult ,
6261 ) -> pinocchio:: ProgramResult {
63- let ( data, end) = self . instruction_data ( ) ;
6462 let instruction = Instruction {
6563 program_id : & crate :: ID ,
66- accounts : & self . account_metas ( ) ,
67- data : & data [ ..end ] ,
64+ accounts : & invoke_parts . account_metas ,
65+ data : invoke_parts . instruction_data . data ( ) ,
6866 } ;
69- invoker ( instruction, & self . accounts ( ) , signers)
67+ invoker ( instruction, & invoke_parts . accounts , signers)
7068 }
7169}
70+
71+ impl < ' a , const N : usize , const M : usize , const J : usize , T > Invoke < ' a , N , M , J > for T where
72+ InvokeParts < ' a , N , M , J > : From < T >
73+ {
74+ }
0 commit comments