@@ -12,7 +12,7 @@ use proc_macro2::Span;
1212use syn:: {
1313 parse:: { self , Parse } ,
1414 spanned:: Spanned ,
15- FnArg , ItemFn , LitInt , LitStr , PathArguments , ReturnType , Type , Visibility ,
15+ FnArg , ItemFn , LitInt , LitStr , PatType , PathArguments , ReturnType , Type , Visibility ,
1616} ;
1717
1818use proc_macro:: TokenStream ;
@@ -67,54 +67,42 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
6767 . into ( ) ;
6868 }
6969
70- #[ cfg( not( feature = "u-boot" ) ) ]
71- for arg in & f. sig . inputs {
72- match arg {
73- FnArg :: Receiver ( _) => {
74- return parse:: Error :: new ( arg. span ( ) , "invalid argument" )
75- . to_compile_error ( )
76- . into ( ) ;
77- }
78- FnArg :: Typed ( t) => {
79- if !is_simple_type ( & t. ty , "usize" ) {
80- return parse:: Error :: new ( t. ty . span ( ) , "argument type must be usize" )
81- . to_compile_error ( )
82- . into ( ) ;
83- }
84- }
70+ fn check_simple_type ( argument : & PatType , ty : & str ) -> Option < TokenStream > {
71+ let inv_type_message = format ! ( "argument type must be {}" , ty) ;
72+
73+ if !is_simple_type ( & argument. ty , ty) {
74+ let error = parse:: Error :: new ( argument. ty . span ( ) , inv_type_message) ;
75+
76+ Some ( error. to_compile_error ( ) . into ( ) )
77+ } else {
78+ None
79+ }
80+ }
81+ fn check_argument_type ( argument : & FnArg , ty : & str ) -> Option < TokenStream > {
82+ let argument_error = parse:: Error :: new ( argument. span ( ) , "invalid argument" ) ;
83+ let argument_error = argument_error. to_compile_error ( ) . into ( ) ;
84+
85+ match argument {
86+ FnArg :: Typed ( argument) => check_simple_type ( argument, ty) ,
87+ FnArg :: Receiver ( _) => Some ( argument_error) ,
8588 }
8689 }
90+ #[ cfg( not( feature = "u-boot" ) ) ]
91+ for argument in f. sig . inputs . iter ( ) {
92+ if let Some ( message) = check_argument_type ( argument, "usize" ) {
93+ return message;
94+ } ;
95+ }
8796 #[ cfg( feature = "u-boot" ) ]
88- if let Some ( a1) = f. sig . inputs . get ( 0 ) {
89- match a1 {
90- FnArg :: Receiver ( _) => {
91- return parse:: Error :: new ( a1. span ( ) , "invalid argument" )
92- . to_compile_error ( )
93- . into ( ) ;
94- }
95- FnArg :: Typed ( t) => {
96- if !is_simple_type ( & t. ty , "c_int" ) {
97- return parse:: Error :: new ( t. ty . span ( ) , "argument type must be c_int" )
98- . to_compile_error ( )
99- . into ( ) ;
100- }
101- }
97+ if let Some ( argument) = f. sig . inputs . get ( 0 ) {
98+ if let Some ( message) = check_argument_type ( argument, "c_int" ) {
99+ return message;
102100 }
103- if let Some ( a2) = f. sig . inputs . get ( 1 ) {
104- match a2 {
105- FnArg :: Receiver ( _) => {
106- return parse:: Error :: new ( a2. span ( ) , "invalid argument" )
107- . to_compile_error ( )
108- . into ( ) ;
109- }
110- FnArg :: Typed ( t) => {
111- if !is_simple_type ( & t. ty , "usize" ) {
112- return parse:: Error :: new ( t. ty . span ( ) , "argument type must be usize" )
113- . to_compile_error ( )
114- . into ( ) ;
115- }
116- }
117- }
101+ }
102+ #[ cfg( feature = "u-boot" ) ]
103+ if let Some ( argument) = f. sig . inputs . get ( 1 ) {
104+ if let Some ( message) = check_argument_type ( argument, "usize" ) {
105+ return message;
118106 }
119107 }
120108
0 commit comments