From f252ae34ec806ce51d5bc58f35c9f7aa289d7c02 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Thu, 30 Jun 2022 17:18:28 -0600 Subject: [PATCH] support variant, enum, and union derives (#4359) * support variant, enum, and union derives This is the second stage of implementing #4308. It adds support for deriving variant, enum, and union impls for `ComponentType`, `Lift`, and `Lower`. It also fixes derived record impls for generic `struct`s, which I had intended to support in my previous commit, but forgot to test. Signed-off-by: Joel Dice * deduplicate component-macro code Thanks to @jameysharp for the suggestion! Signed-off-by: Joel Dice --- crates/component-macro/src/lib.rs | 538 ++++++++++++++++++-- crates/wasmtime/src/component/func/typed.rs | 89 ++++ crates/wasmtime/src/component/mod.rs | 3 +- tests/all/component_model/macros.rs | 323 +++++++++++- 4 files changed, 901 insertions(+), 52 deletions(-) diff --git a/crates/component-macro/src/lib.rs b/crates/component-macro/src/lib.rs index 4e1abe6c03..b956c9e318 100644 --- a/crates/component-macro/src/lib.rs +++ b/crates/component-macro/src/lib.rs @@ -1,16 +1,32 @@ use proc_macro2::{Literal, TokenStream, TokenTree}; use quote::{format_ident, quote}; use std::collections::HashSet; +use std::fmt; use syn::{parse_macro_input, parse_quote, Data, DeriveInput, Error, Result}; -#[derive(Debug)] -enum Style { - Record, +#[derive(Debug, Copy, Clone)] +enum VariantStyle { Variant, Enum, Union, } +impl fmt::Display for VariantStyle { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + Self::Variant => "variant", + Self::Enum => "enum", + Self::Union => "union", + }) + } +} + +#[derive(Debug, Copy, Clone)] +enum Style { + Record, + Variant(VariantStyle), +} + fn find_style(input: &DeriveInput) -> Result