From f174134aa23e96a2121fb4106fb23fbdad4d641a Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 30 Dec 2021 12:12:19 +0100 Subject: Style chains --- macros/src/lib.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'macros/src') diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 3b1d0548..55e597b1 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -47,7 +47,7 @@ fn expand(mut impl_block: syn::ItemImpl) -> Result { mod #module { use std::marker::PhantomData; use once_cell::sync::Lazy; - use crate::eval::{Property, StyleId}; + use crate::eval::{Nonfolding, Property, StyleId}; use super::*; #impl_block @@ -106,17 +106,24 @@ fn process_const( // initialization value of the const. let default = &item.expr; + let mut folder = None; + let mut nonfolding = Some(quote! { + impl Nonfolding for Key {} + }); + // Look for a folding function like `#[fold(u64::add)]`. - let mut combinator = None; for attr in &item.attrs { if attr.path.is_ident("fold") { - let fold: syn::Expr = attr.parse_args()?; - combinator = Some(quote! { - fn combine(inner: Self::Value, outer: Self::Value) -> Self::Value { - let f: fn(Self::Value, Self::Value) -> Self::Value = #fold; + let func: syn::Expr = attr.parse_args()?; + folder = Some(quote! { + const FOLDABLE: bool = true; + + fn fold(inner: Self::Value, outer: Self::Value) -> Self::Value { + let f: fn(Self::Value, Self::Value) -> Self::Value = #func; f(inner, outer) } }); + nonfolding = None; } } @@ -136,8 +143,10 @@ fn process_const( &*LAZY } - #combinator + #folder } + + #nonfolding }; // The module that will contain the `Key` type. -- cgit v1.2.3