|
| 1 | +use std::mem; |
| 2 | + |
| 3 | +/// Implemented on syn types which can contain lifetimes. |
| 4 | +pub trait DropLifetimes { |
| 5 | + /// Drops any lifetimes inside `self`. |
| 6 | + fn drop_lifetimes(&mut self); |
| 7 | +} |
| 8 | + |
| 9 | +impl DropLifetimes for syn::Type { |
| 10 | + fn drop_lifetimes(&mut self) { |
| 11 | + match self { |
| 12 | + syn::Type::Array(ty) => ty.drop_lifetimes(), |
| 13 | + syn::Type::BareFn(ty) => ty.drop_lifetimes(), |
| 14 | + syn::Type::Group(ty) => ty.drop_lifetimes(), |
| 15 | + syn::Type::ImplTrait(ty) => ty.drop_lifetimes(), |
| 16 | + syn::Type::Paren(ty) => ty.drop_lifetimes(), |
| 17 | + syn::Type::Path(ty) => ty.drop_lifetimes(), |
| 18 | + syn::Type::Ptr(ty) => ty.drop_lifetimes(), |
| 19 | + syn::Type::Reference(ty) => ty.drop_lifetimes(), |
| 20 | + syn::Type::Slice(ty) => ty.drop_lifetimes(), |
| 21 | + syn::Type::TraitObject(ty) => ty.drop_lifetimes(), |
| 22 | + syn::Type::Tuple(ty) => ty.drop_lifetimes(), |
| 23 | + _ => {} |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl DropLifetimes for syn::TypeArray { |
| 29 | + fn drop_lifetimes(&mut self) { |
| 30 | + self.elem.drop_lifetimes() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl DropLifetimes for syn::TypeBareFn { |
| 35 | + fn drop_lifetimes(&mut self) { |
| 36 | + self.lifetimes = None; |
| 37 | + self.inputs.iter_mut().for_each(|i| i.drop_lifetimes()); |
| 38 | + self.output.drop_lifetimes(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl DropLifetimes for syn::BareFnArg { |
| 43 | + fn drop_lifetimes(&mut self) { |
| 44 | + self.ty.drop_lifetimes(); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl DropLifetimes for syn::ReturnType { |
| 49 | + fn drop_lifetimes(&mut self) { |
| 50 | + if let syn::ReturnType::Type(_, t) = self { |
| 51 | + t.drop_lifetimes(); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl DropLifetimes for syn::TypeGroup { |
| 57 | + fn drop_lifetimes(&mut self) { |
| 58 | + self.elem.drop_lifetimes() |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl DropLifetimes for syn::TypeImplTrait { |
| 63 | + fn drop_lifetimes(&mut self) { |
| 64 | + self.bounds.drop_lifetimes(); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl<T: Default + Clone> DropLifetimes for syn::punctuated::Punctuated<syn::TypeParamBound, T> { |
| 69 | + fn drop_lifetimes(&mut self) { |
| 70 | + *self = mem::take(self) |
| 71 | + .into_iter() |
| 72 | + .filter_map(|mut i| match &mut i { |
| 73 | + syn::TypeParamBound::Trait(t) => { |
| 74 | + t.drop_lifetimes(); |
| 75 | + Some(i) |
| 76 | + } |
| 77 | + _ => None, |
| 78 | + }) |
| 79 | + .collect(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl DropLifetimes for syn::TraitBound { |
| 84 | + fn drop_lifetimes(&mut self) { |
| 85 | + self.lifetimes = None; |
| 86 | + self.path.drop_lifetimes(); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl DropLifetimes for syn::Path { |
| 91 | + fn drop_lifetimes(&mut self) { |
| 92 | + self.segments.iter_mut().for_each(|i| i.drop_lifetimes()); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl DropLifetimes for syn::PathSegment { |
| 97 | + fn drop_lifetimes(&mut self) { |
| 98 | + if let syn::PathArguments::AngleBracketed(args) = &mut self.arguments { |
| 99 | + args.args = mem::take(&mut args.args) |
| 100 | + .into_iter() |
| 101 | + .filter_map(|mut i| { |
| 102 | + match &mut i { |
| 103 | + syn::GenericArgument::Type(t) => t.drop_lifetimes(), |
| 104 | + syn::GenericArgument::Binding(t) => t.drop_lifetimes(), |
| 105 | + syn::GenericArgument::Constraint(t) => t.drop_lifetimes(), |
| 106 | + syn::GenericArgument::Const(_) => {} |
| 107 | + _ => return None, |
| 108 | + }; |
| 109 | + Some(i) |
| 110 | + }) |
| 111 | + .collect(); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl DropLifetimes for syn::Binding { |
| 117 | + fn drop_lifetimes(&mut self) { |
| 118 | + self.ty.drop_lifetimes(); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl DropLifetimes for syn::Constraint { |
| 123 | + fn drop_lifetimes(&mut self) { |
| 124 | + self.bounds.drop_lifetimes(); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +impl DropLifetimes for syn::TypeParen { |
| 129 | + fn drop_lifetimes(&mut self) { |
| 130 | + self.elem.drop_lifetimes(); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +impl DropLifetimes for syn::TypePath { |
| 135 | + fn drop_lifetimes(&mut self) { |
| 136 | + if let Some(qself) = &mut self.qself { |
| 137 | + qself.ty.drop_lifetimes(); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl DropLifetimes for syn::TypePtr { |
| 143 | + fn drop_lifetimes(&mut self) { |
| 144 | + self.elem.drop_lifetimes(); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +impl DropLifetimes for syn::TypeReference { |
| 149 | + fn drop_lifetimes(&mut self) { |
| 150 | + self.lifetime = None; |
| 151 | + self.elem.drop_lifetimes(); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +impl DropLifetimes for syn::TypeSlice { |
| 156 | + fn drop_lifetimes(&mut self) { |
| 157 | + self.elem.drop_lifetimes(); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +impl DropLifetimes for syn::TypeTraitObject { |
| 162 | + fn drop_lifetimes(&mut self) { |
| 163 | + self.bounds.drop_lifetimes(); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +impl DropLifetimes for syn::TypeTuple { |
| 168 | + fn drop_lifetimes(&mut self) { |
| 169 | + self.elems.iter_mut().for_each(|i| i.drop_lifetimes()); |
| 170 | + } |
| 171 | +} |
0 commit comments