fn create_method_wrapper(method: &TraitItemMethod) -> TraitItemMethod {
        struct ArgInfo {
            call_toks: proc_macro2::TokenStream,
        }

        // Calculate name and pattern tokens
        fn arg_pat_info(pat: &Pat) -> ArgInfo {
            match pat {
                Pat::Ident(ident) => {
                    let toks = quote::quote! {
                        #ident
                    };
                    ArgInfo { call_toks: toks }
                }
                Pat::Tuple(tup) => {
                    let infos = tup.elems.iter().map(arg_pat_info);

                    let toks = {
                        let mut toks = proc_macro2::TokenStream::new();

                        for info in infos {
                            toks.extend(info.call_toks);
                            toks.extend(quote::quote!(,));
                        }

                        toks
                    };

                    ArgInfo {
                        call_toks: quote::quote!((#toks)),
                    }
                }
                Pat::TupleStruct(_tup) => unimplemented!(),
                p => panic!("Unsupported pattern type: {:?}", p),
            }
        }

        let mut m: TraitItemMethod = (*method).clone();

        let argument_data = m
            .sig
            .inputs
            .clone()
            .into_iter()
            .map(|t: FnArg| match &t {
                FnArg::Receiver(_) => quote::quote!(self),
                FnArg::Typed(p) => {
                    let info = arg_pat_info(&p.pat);

                    info.call_toks
                }
            })
            .collect::<Vec<_>>();

        let arguments = {
            let mut toks = proc_macro2::TokenStream::new();

            for arg in argument_data {
                toks.extend(arg);
                toks.extend(quote::quote!(,));
            }

            toks
        };

        let body: TokenStream = {
            let name = contract_method_impl_name(&m.sig.ident.to_string());
            let name = syn::Ident::new(&name, m.sig.ident.span());

            quote::quote! {
                {
                    Self::#name(#arguments)
                }
            }
        };

        let mut attrs = vec![];

        // keep the documentation and contracts of the original method
        attrs.extend(
            m.attrs
                .iter()
                .filter(|a| {
                    let name =
                        a.path.segments.last().unwrap().ident.to_string();
                    // is doc?
                    if name == "doc" {
                        return true;
                    }

                    // is contract?
                    ContractType::contract_type_and_mode(&name).is_some()
                })
                .cloned(),
        );
        // always inline
        attrs.push(syn::parse_quote!(#[inline(always)]));

        m.attrs = attrs;

        {
            let block: syn::Block = syn::parse2(body).unwrap();
            m.default = Some(block);
            m.semi_token = None;
        }

        m
    }
/// Create a wrapper function which has a default implementation and
/// includes contracts.
///
/// This new function forwards the call to the actual implementation.
Create a wrapper function which has a default implementation and
includes contracts.

This new function forwards the call to the actual implementation.
Create a wrapper function which has a default implementation and includes contracts . This new function forwards the call to the actual implementation .
fn create_method_wrapper(method: &TraitItemMethod) -> TraitItemMethod {
        struct ArgInfo {
            call_toks: proc_macro2::TokenStream,
        }
        // Calculate name and pattern tokens
        fn arg_pat_info(pat: &Pat) -> ArgInfo {
            match pat {
                Pat::Ident(ident) => {
                    let toks = quote::quote! {
                        #ident
                    };
                    ArgInfo { call_toks: toks }
                }
                Pat::Tuple(tup) => {
                    let infos = tup.elems.iter().map(arg_pat_info);
                    let toks = {
                        let mut toks = proc_macro2::TokenStream::new();
                        for info in infos {
                            toks.extend(info.call_toks);
                            toks.extend(quote::quote!(,));
                        }
                        toks
                    };
                    ArgInfo {
                        call_toks: quote::quote!((#toks)),
                    }
                }
                Pat::TupleStruct(_tup) => unimplemented!(),
                p => panic!("Unsupported pattern type: {:?}", p),
            }
        }
        let mut m: TraitItemMethod = (*method).clone();
        let argument_data = m
            .sig
            .inputs
            .clone()
            .into_iter()
            .map(|t: FnArg| match &t {
                FnArg::Receiver(_) => quote::quote!(self),
                FnArg::Typed(p) => {
                    let info = arg_pat_info(&p.pat);
                    info.call_toks
                }
            })
            .collect::<Vec<_>>();
        let arguments = {
            let mut toks = proc_macro2::TokenStream::new();
            for arg in argument_data {
                toks.extend(arg);
                toks.extend(quote::quote!(,));
            }
            toks
        };
        let body: TokenStream = {
            let name = contract_method_impl_name(&m.sig.ident.to_string());
            let name = syn::Ident::new(&name, m.sig.ident.span());
            quote::quote! {
                {
                    Self::#name(#arguments)
                }
            }
        };
        let mut attrs = vec![];
        // keep the documentation and contracts of the original method
        attrs.extend(
            m.attrs
                .iter()
                .filter(|a| {
                    let name =
                        a.path.segments.last().unwrap().ident.to_string();
                    // is doc?
                    if name == "doc" {
                        return true;
                    }
                    // is contract?
                    ContractType::contract_type_and_mode(&name).is_some()
                })
                .cloned(),
        );
        // always inline
        attrs.push(syn::parse_quote!(#[inline(always)]));
        m.attrs = attrs;
        {
            let block: syn::Block = syn::parse2(body).unwrap();
            m.default = Some(block);
            m.semi_token = None;
        }
        m
    }
fn create_method_wrapper ( method : & TraitItemMethod ) -> TraitItemMethod { struct ArgInfo { call_toks : proc_macro2 :: TokenStream , } // Calculate name and pattern tokens fn arg_pat_info ( pat : & Pat ) -> ArgInfo { match pat { Pat :: Ident ( ident ) => { let toks = quote :: quote ! { ident } ; ArgInfo { call_toks : toks } } Pat :: Tuple ( tup ) => { let infos = tup . elems . iter ( ) . map ( arg_pat_info ) ; let toks = { let mut toks = proc_macro2 :: TokenStream :: new ( ) ; for info in infos { toks . extend ( info . call_toks ) ; toks . extend ( quote :: quote ! ( ) ) ; } toks } ; ArgInfo { call_toks : quote :: quote ! ( ( toks ) ) , } } Pat :: TupleStruct ( _tup ) => unimplemented ! ( ) , p => panic ! ( " " p ) , } } let mut m : TraitItemMethod = ( * method ) . clone ( ) ; let argument_data = m . sig . inputs . clone ( ) . into_iter ( ) . map ( | t : FnArg | match & t { FnArg :: Receiver ( _ ) => quote :: quote ! ( self ) , FnArg :: Typed ( p ) => { let info = arg_pat_info ( & p . pat ) ; info . call_toks } } ) . collect :: < Vec < _ > > ( ) ; let arguments = { let mut toks = proc_macro2 :: TokenStream :: new ( ) ; for arg in argument_data { toks . extend ( arg ) ; toks . extend ( quote :: quote ! ( ) ) ; } toks } ; let body : TokenStream = { let name = contract_method_impl_name ( & m . sig . ident . to_string ( ) ) ; let name = syn :: Ident :: new ( & name , m . sig . ident . span ( ) ) ; quote :: quote ! { { Self name ( arguments ) } } } ; let mut attrs = vec ! [ ] ; // keep the documentation and contracts of the original method attrs . extend ( m . attrs . iter ( ) . filter ( | a | { let name = a . path . segments . last ( ) . unwrap ( ) . ident . to_string ( ) ; // is doc? if name == " " { return true ; } // is contract? ContractType :: contract_type_and_mode ( & name ) . is_some ( ) } ) . cloned ( ) , ) ; // always inline attrs . push ( syn :: parse_quote ! ( [ inline ( always ) ] ) ) ; m . attrs = attrs ; { let block : syn :: Block = syn :: parse2 ( body ) . unwrap ( ) ; m . default = Some ( block ) ; m . semi_token = None ; } m }
Create a wrapper function which has a default implementation and
includes contracts.
Create a wrapper function which has a default implementation and includes contracts .
method
&TraitItemMethod
method
&TraitItemMethod