@@ -11,61 +11,64 @@ pub mod instructions;
11
11
12
12
pinocchio_pubkey:: declare_id!( "11111111111111111111111111111111" ) ;
13
13
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
+ }
18
19
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 ) ) ,
22
23
}
23
24
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
+ {
25
37
fn invoke ( self ) -> pinocchio:: ProgramResult {
26
38
self . invoke_signed ( & [ ] )
27
39
}
28
40
29
41
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
+ } )
31
45
}
32
46
33
47
unsafe fn invoke_access_unchecked ( self ) -> pinocchio:: ProgramResult {
34
48
self . invoke_singed_access_unchecked ( & [ ] )
35
49
}
36
50
37
51
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 {
39
53
cpi:: invoke_signed_access_unchecked ( & ix, acc, sigs)
40
54
} )
41
55
}
42
-
43
- fn invoke_invoker (
44
- self ,
45
- signers : & [ Signer ] ,
46
- invoker : impl FnOnce ( Instruction , & [ & AccountInfo ; N ] , & [ Signer ] ) -> pinocchio:: ProgramResult ,
47
- ) -> pinocchio:: ProgramResult ;
48
56
}
49
57
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
+ T : Into < InvokeParts < ' a , N , M , J > >
57
60
{
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 )
71
74
}
0 commit comments