aboutsummaryrefslogtreecommitdiffstats
path: root/components/style_derive/compute_squared_distance.rs
diff options
context:
space:
mode:
Diffstat (limited to 'components/style_derive/compute_squared_distance.rs')
-rw-r--r--components/style_derive/compute_squared_distance.rs79
1 files changed, 11 insertions, 68 deletions
diff --git a/components/style_derive/compute_squared_distance.rs b/components/style_derive/compute_squared_distance.rs
index 46aa376319d..df02d1312cc 100644
--- a/components/style_derive/compute_squared_distance.rs
+++ b/components/style_derive/compute_squared_distance.rs
@@ -2,50 +2,30 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+use cg;
use quote;
-use std::borrow::Cow;
use syn;
-use synstructure;
pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
let name = &input.ident;
- let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
- let mut where_clause = where_clause.clone();
- for param in &input.generics.ty_params {
- where_clause.predicates.push(where_predicate(syn::Ty::Path(None, param.ident.clone().into())))
- }
+ let trait_path = &["values", "distance", "ComputeSquaredDistance"];
+ let (impl_generics, ty_generics, mut where_clause) =
+ cg::trait_parts(&input, trait_path);
- let variants = variants(&input);
+ let variants = cg::variants(&input);
let mut match_body = quote!();
match_body.append_all(variants.iter().map(|variant| {
- let name = match input.body {
- syn::Body::Struct(_) => Cow::Borrowed(&input.ident),
- syn::Body::Enum(_) => {
- Cow::Owned(syn::Ident::from(format!("{}::{}", input.ident, variant.ident)))
- },
- };
- let (this_pattern, this_info) = synstructure::match_pattern(
- &name,
- &variant.data,
- &synstructure::BindOpts::with_prefix(
- synstructure::BindStyle::Ref,
- "this".to_owned(),
- ),
- );
- let (other_pattern, other_info) = synstructure::match_pattern(
- &name,
- &variant.data,
- &synstructure::BindOpts::with_prefix(
- synstructure::BindStyle::Ref,
- "other".to_owned(),
- ),
- );
+ let name = cg::variant_ctor(&input, variant);
+ let (this_pattern, this_info) = cg::ref_pattern(&name, &variant, "this");
+ let (other_pattern, other_info) = cg::ref_pattern(&name, &variant, "other");
let sum = if this_info.is_empty() {
quote! { ::values::distance::SquaredDistance::Value(0.) }
} else {
let mut sum = quote!();
sum.append_separated(this_info.iter().zip(&other_info).map(|(this, other)| {
- where_clause.predicates.push(where_predicate(this.field.ty.clone()));
+ where_clause.predicates.push(
+ cg::where_predicate(this.field.ty.clone(), trait_path),
+ );
quote! {
::values::distance::ComputeSquaredDistance::compute_squared_distance(#this, #other)?
}
@@ -78,40 +58,3 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
}
}
}
-
-fn variants(input: &syn::DeriveInput) -> Cow<[syn::Variant]> {
- match input.body {
- syn::Body::Enum(ref variants) => (&**variants).into(),
- syn::Body::Struct(ref data) => {
- vec![syn::Variant {
- ident: input.ident.clone(),
- attrs: input.attrs.clone(),
- data: data.clone(),
- discriminant: None,
- }].into()
- },
- }
-}
-
-fn where_predicate(ty: syn::Ty) -> syn::WherePredicate {
- syn::WherePredicate::BoundPredicate(
- syn::WhereBoundPredicate {
- bound_lifetimes: vec![],
- bounded_ty: ty,
- bounds: vec![syn::TyParamBound::Trait(
- syn::PolyTraitRef {
- bound_lifetimes: vec![],
- trait_ref: syn::Path {
- global: true,
- segments: vec![
- "values".into(),
- "distance".into(),
- "ComputeSquaredDistance".into(),
- ],
- },
- },
- syn::TraitBoundModifier::None,
- )],
- },
- )
-}