@@ -48,6 +48,26 @@ pub fn fn_arg_ident(arg: &FnArg) -> Result<Ident, Error> {
4848 ) )
4949}
5050
51+ /// Modifies a Pat removing any 'mut' on an Ident.
52+ pub fn pat_unwrap_mut ( p : Pat ) -> Pat {
53+ match p {
54+ Pat :: Ident ( PatIdent {
55+ attrs,
56+ by_ref,
57+ mutability : Some ( _) ,
58+ ident,
59+ subpat,
60+ } ) => Pat :: Ident ( PatIdent {
61+ attrs,
62+ by_ref,
63+ mutability : None ,
64+ ident,
65+ subpat,
66+ } ) ,
67+ _ => p,
68+ }
69+ }
70+
5171/// Returns a clone of the type from the FnArg.
5272pub fn fn_arg_ref_type ( arg : & FnArg , lifetime : Option < & Lifetime > ) -> Result < Type , Error > {
5373 if let FnArg :: Typed ( pat_type) = arg {
@@ -70,22 +90,13 @@ pub fn fn_arg_ref_type(arg: &FnArg, lifetime: Option<&Lifetime>) -> Result<Type,
7090}
7191
7292/// Returns a clone of FnArg with the type as a reference if the arg is a typed
73- /// arg and its type is not already a reference.
93+ /// arg and its type is not already a reference. Mutability from the ident is stripped.
7494pub fn fn_arg_make_ref ( arg : & FnArg , lifetime : Option < & Lifetime > ) -> FnArg {
7595 if let FnArg :: Typed ( pat_type) = arg {
7696 if !matches ! ( * pat_type. ty, Type :: Reference ( _) ) {
7797 return FnArg :: Typed ( PatType {
7898 attrs : pat_type. attrs . clone ( ) ,
79- pat : Box :: new ( match & * pat_type. pat {
80- Pat :: Ident ( pat_ident) => Pat :: Ident ( PatIdent {
81- attrs : pat_ident. attrs . clone ( ) ,
82- by_ref : pat_ident. by_ref ,
83- mutability : None , // Strip mutability for reference parameters
84- ident : pat_ident. ident . clone ( ) ,
85- subpat : pat_ident. subpat . clone ( ) ,
86- } ) ,
87- _ => * pat_type. pat . clone ( ) ,
88- } ) ,
99+ pat : Box :: new ( pat_unwrap_mut ( * pat_type. pat . clone ( ) ) ) ,
89100 colon_token : pat_type. colon_token ,
90101 ty : Box :: new ( Type :: Reference ( TypeReference {
91102 and_token : And :: default ( ) ,
@@ -99,21 +110,14 @@ pub fn fn_arg_make_ref(arg: &FnArg, lifetime: Option<&Lifetime>) -> FnArg {
99110 arg. clone ( )
100111}
101112
113+ /// Returns a clone of FnArg with the type as an Into if the arg is a typed
114+ /// arg. Mutability from the ident is stripped.
102115pub fn fn_arg_make_into ( arg : & FnArg ) -> FnArg {
103116 if let FnArg :: Typed ( pat_type) = arg {
104117 let ty = & pat_type. ty ;
105118 return FnArg :: Typed ( PatType {
106119 attrs : pat_type. attrs . clone ( ) ,
107- pat : Box :: new ( match & * pat_type. pat {
108- Pat :: Ident ( pat_ident) => Pat :: Ident ( PatIdent {
109- attrs : pat_ident. attrs . clone ( ) ,
110- by_ref : pat_ident. by_ref ,
111- mutability : None , // Strip mutability for Into parameters
112- ident : pat_ident. ident . clone ( ) ,
113- subpat : pat_ident. subpat . clone ( ) ,
114- } ) ,
115- _ => * pat_type. pat . clone ( ) ,
116- } ) ,
120+ pat : Box :: new ( pat_unwrap_mut ( * pat_type. pat . clone ( ) ) ) ,
117121 colon_token : pat_type. colon_token ,
118122 ty : Box :: new ( syn:: parse_quote! { impl Into <#ty> } ) ,
119123 } ) ;
0 commit comments