From a5b0c7aa37e1839f1ebfd516e7fbeb7a5851471f Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Wed, 28 Feb 2024 18:17:19 -0800 Subject: [PATCH 1/7] unconditionally add serde --- Cargo.lock | 3 + crates/wast/src/component/alias.rs | 10 +++ crates/wast/src/component/binary.rs | 7 +- crates/wast/src/component/component.rs | 16 +++++ crates/wast/src/component/custom.rs | 2 + crates/wast/src/component/expand.rs | 5 +- crates/wast/src/component/export.rs | 8 +++ crates/wast/src/component/func.rs | 32 ++++++++- crates/wast/src/component/import.rs | 20 +++++- crates/wast/src/component/instance.rs | 20 ++++++ crates/wast/src/component/item_ref.rs | 25 ++++--- crates/wast/src/component/module.rs | 6 ++ crates/wast/src/component/resolve.rs | 9 +-- crates/wast/src/component/types.rs | 98 +++++++++++++++++++++++--- crates/wast/src/core/custom.rs | 16 +++++ crates/wast/src/core/export.rs | 6 ++ crates/wast/src/core/expr.rs | 68 ++++++++++++++++++ crates/wast/src/core/func.rs | 8 +++ crates/wast/src/core/global.rs | 6 ++ crates/wast/src/core/import.rs | 8 +++ crates/wast/src/core/memory.rs | 13 ++++ crates/wast/src/core/module.rs | 9 +++ crates/wast/src/core/table.rs | 14 ++++ crates/wast/src/core/tag.rs | 9 +++ crates/wast/src/core/types.rs | 37 ++++++++++ crates/wast/src/lib.rs | 3 + crates/wast/src/token.rs | 8 +++ crates/wast/src/wat.rs | 4 ++ 28 files changed, 440 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 014a6db7da..5b519f30e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2224,6 +2224,9 @@ dependencies = [ "leb128", "memchr", "rayon", + "serde", + "serde_derive", + "serde_json", "unicode-width", "wasm-encoder 0.201.0", "wasmparser 0.201.0", diff --git a/crates/wast/src/component/alias.rs b/crates/wast/src/component/alias.rs index dffcda1445..50e2ac83ed 100644 --- a/crates/wast/src/component/alias.rs +++ b/crates/wast/src/component/alias.rs @@ -2,11 +2,13 @@ use crate::core::ExportKind; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, Index, NameAnnotation, Span}; +use serde_derive::{Serialize, Deserialize}; /// A inline alias for component exported items. /// /// Handles both `core export` and `export` aliases #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct InlineExportAlias<'a, const CORE: bool> { /// The instance to alias the export from. pub instance: Index<'a>, @@ -29,11 +31,13 @@ impl<'a, const CORE: bool> Parse<'a> for InlineExportAlias<'a, CORE> { /// An alias to a component item. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Alias<'a> { /// Where this `alias` was defined. pub span: Span, /// An identifier that this alias is resolved with (optionally) for name /// resolution. + #[serde(borrow)] pub id: Option>, /// An optional name for this alias stored in the custom `name` section. pub name: Option>, @@ -137,6 +141,8 @@ impl<'a> Parse<'a> for Alias<'a> { /// Represents the kind of instance export alias. #[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ComponentExportAliasKind { /// The alias is to a core module export. CoreModule, @@ -187,6 +193,8 @@ impl<'a> Parse<'a> for ComponentExportAliasKind { /// Represents the kind of outer alias. #[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ComponentOuterAliasKind { /// The alias is to an outer core module. CoreModule, @@ -227,6 +235,8 @@ impl<'a> Parse<'a> for ComponentOuterAliasKind { /// The target of a component alias. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum AliasTarget<'a> { /// The alias is to an export of a component instance. Export { diff --git a/crates/wast/src/component/binary.rs b/crates/wast/src/component/binary.rs index a57cc0f912..a4a9eefdc5 100644 --- a/crates/wast/src/component/binary.rs +++ b/crates/wast/src/component/binary.rs @@ -8,6 +8,7 @@ use wasm_encoder::{ ComponentTypeSection, CoreTypeEncoder, CoreTypeSection, InstanceSection, NameMap, NestedComponentSection, RawSection, SectionId, }; +use serde::Serialize as SerializeT; pub fn encode(component: &Component<'_>) -> Vec { match &component.kind { @@ -731,14 +732,14 @@ impl From> for u32 { } } -impl From<&ItemRef<'_, T>> for u32 { +impl From<&ItemRef<'_, T>> for u32 { fn from(i: &ItemRef<'_, T>) -> Self { assert!(i.export_names.is_empty()); i.idx.into() } } -impl From<&CoreTypeUse<'_, T>> for u32 { +impl From<&CoreTypeUse<'_, T>> for u32 { fn from(u: &CoreTypeUse<'_, T>) -> Self { match u { CoreTypeUse::Inline(_) => unreachable!("should be expanded already"), @@ -747,7 +748,7 @@ impl From<&CoreTypeUse<'_, T>> for u32 { } } -impl From<&ComponentTypeUse<'_, T>> for u32 { +impl From<&ComponentTypeUse<'_, T>> for u32 { fn from(u: &ComponentTypeUse<'_, T>) -> Self { match u { ComponentTypeUse::Inline(_) => unreachable!("should be expanded already"), diff --git a/crates/wast/src/component/component.rs b/crates/wast/src/component/component.rs index cc9171b505..36884f32e9 100644 --- a/crates/wast/src/component/component.rs +++ b/crates/wast/src/component/component.rs @@ -5,13 +5,16 @@ use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::Index; use crate::token::{Id, NameAnnotation, Span}; +use serde_derive::{Serialize, Deserialize}; /// A parsed WebAssembly component module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Component<'a> { /// Where this `component` was defined pub span: Span, /// An optional identifier this component is known by + #[serde(borrow)] pub id: Option>, /// An optional `@name` annotation for this component pub name: Option>, @@ -21,8 +24,11 @@ pub struct Component<'a> { /// The different kinds of ways to define a component. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ComponentKind<'a> { /// A component defined in the textual s-expression format. + #[serde(borrow)] Text(Vec>), /// A component that had its raw binary bytes defined via the `binary` /// directive. @@ -139,7 +145,10 @@ impl<'a> Parse<'a> for Component<'a> { /// A listing of all possible fields that can make up a WebAssembly component. #[allow(missing_docs)] #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ComponentField<'a> { + #[serde(borrow)] CoreModule(CoreModule<'a>), CoreInstance(CoreInstance<'a>), CoreType(CoreType<'a>), @@ -220,8 +229,10 @@ impl<'a> Parse<'a> for ComponentField<'a> { /// A function to call at instantiation time. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Start<'a> { /// The function to call. + #[serde(borrow)] pub func: Index<'a>, /// The arguments to pass to the function. pub args: Vec>, @@ -259,10 +270,12 @@ impl<'a> Parse<'a> for Start<'a> { /// A nested WebAssembly component. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct NestedComponent<'a> { /// Where this `component` was defined pub span: Span, /// An optional identifier this component is known by + #[serde(borrow)] pub id: Option>, /// An optional `@name` annotation for this component pub name: Option>, @@ -275,10 +288,13 @@ pub struct NestedComponent<'a> { /// The different kinds of ways to define a nested component. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum NestedComponentKind<'a> { /// This is actually an inline import of a component Import { /// The information about where this is being imported from. + #[serde(borrow)] import: InlineImport<'a>, /// The type of component being imported. ty: ComponentTypeUse<'a, ComponentType<'a>>, diff --git a/crates/wast/src/component/custom.rs b/crates/wast/src/component/custom.rs index b17a7fafb4..a63bafcd4a 100644 --- a/crates/wast/src/component/custom.rs +++ b/crates/wast/src/component/custom.rs @@ -1,9 +1,11 @@ use crate::annotation; use crate::parser::{Parse, Parser, Result}; use crate::token::Span; +use serde_derive::{Serialize, Deserialize}; /// A custom section within a component. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Custom<'a> { /// Where this `@custom` was defined. pub span: Span, diff --git a/crates/wast/src/component/expand.rs b/crates/wast/src/component/expand.rs index 10523eacc8..a729dca1e2 100644 --- a/crates/wast/src/component/expand.rs +++ b/crates/wast/src/component/expand.rs @@ -6,6 +6,7 @@ use crate::token::Id; use crate::token::{Index, Span}; use std::collections::HashMap; use std::mem; +use serde::Serialize as SerializeT; /// Performs an AST "expansion" pass over the component fields provided. /// @@ -599,7 +600,7 @@ impl<'a> Expander<'a> { *ty = ComponentValType::Ref(idx); } - fn expand_core_type_use( + fn expand_core_type_use( &mut self, item: &mut CoreTypeUse<'a, T>, ) -> CoreItemRef<'a, kw::r#type> @@ -652,7 +653,7 @@ impl<'a> Expander<'a> { ret } - fn expand_component_type_use( + fn expand_component_type_use( &mut self, item: &mut ComponentTypeUse<'a, T>, ) -> ItemRef<'a, kw::r#type> diff --git a/crates/wast/src/component/export.rs b/crates/wast/src/component/export.rs index d9e2ae4b4c..e165577756 100644 --- a/crates/wast/src/component/export.rs +++ b/crates/wast/src/component/export.rs @@ -2,13 +2,16 @@ use super::{ComponentExternName, ItemRef, ItemSigNoName}; use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Id, Index, NameAnnotation, Span}; +use serde_derive::{Serialize, Deserialize}; /// An entry in a WebAssembly component's export section. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ComponentExport<'a> { /// Where this export was defined. pub span: Span, /// Optional identifier bound to this export. + #[serde(borrow)] pub id: Option>, /// An optional name for this instance stored in the custom `name` section. pub debug_name: Option>, @@ -55,11 +58,14 @@ impl<'a> Parse<'a> for Vec> { /// The kind of exported item. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ComponentExportKind<'a> { /// The export is a core module. /// /// Note this isn't a core item ref as currently only /// components can export core modules. + #[serde(borrow)] CoreModule(ItemRef<'a, kw::module>), /// The export is a function. Func(ItemRef<'a, kw::func>), @@ -171,8 +177,10 @@ impl Peek for ComponentExportKind<'_> { /// A listing of inline `(export "foo" )` statements on a WebAssembly /// component item in its textual format. #[derive(Debug, Default)] +#[derive(Serialize, Deserialize)] pub struct InlineExport<'a> { /// The extra names to export an item as, if any. + #[serde(borrow)] pub names: Vec>, } diff --git a/crates/wast/src/component/func.rs b/crates/wast/src/component/func.rs index 24cbb3eb67..a02ea8b635 100644 --- a/crates/wast/src/component/func.rs +++ b/crates/wast/src/component/func.rs @@ -2,16 +2,20 @@ use crate::component::*; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, Index, LParen, NameAnnotation, Span}; +use serde_derive::{Serialize, Deserialize}; +use serde::Serialize as SerializeT; /// A declared core function. /// /// This is a member of both the core alias and canon sections. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct CoreFunc<'a> { /// Where this `core func` was defined. pub span: Span, /// An identifier that this function is resolved with (optionally) for name /// resolution. + #[serde(borrow)] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -39,10 +43,13 @@ impl<'a> Parse<'a> for CoreFunc<'a> { /// Represents the kind of core functions. #[derive(Debug)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum CoreFuncKind<'a> { /// The core function is defined in terms of lowering a component function. /// /// The core function is actually a member of the canon section. + #[serde(borrow)] Lower(CanonLower<'a>), /// The core function is defined in terms of aliasing a module instance export. /// @@ -84,11 +91,13 @@ impl<'a> Parse<'a> for CoreFuncKind<'a> { /// /// This may be a member of the import, alias, or canon sections. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Func<'a> { /// Where this `func` was defined. pub span: Span, /// An identifier that this function is resolved with (optionally) for name /// resolution. + #[serde(borrow)] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -119,6 +128,8 @@ impl<'a> Parse<'a> for Func<'a> { /// Represents the kind of component functions. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum FuncKind<'a> { /// A function which is actually defined as an import, such as: /// @@ -127,6 +138,7 @@ pub enum FuncKind<'a> { /// ``` Import { /// The import name of this import. + #[serde(borrow)] import: InlineImport<'a>, /// The type that this function will have. ty: ComponentTypeUse<'a, ComponentFunctionType<'a>>, @@ -171,11 +183,13 @@ impl<'a> Parse<'a> for FuncKind<'a> { /// /// This is a member of the canonical section. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct CanonicalFunc<'a> { /// Where this `func` was defined. pub span: Span, /// An identifier that this function is resolved with (optionally) for name /// resolution. + #[serde(borrow)] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -247,10 +261,13 @@ impl<'a> CanonicalFunc<'a> { /// Possible ways to define a canonical function in the text format. #[derive(Debug)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum CanonicalFuncKind<'a> { /// A canonical function that is defined in terms of lifting a core function. Lift { /// The lifted function's type. + #[serde(borrow)] ty: ComponentTypeUse<'a, ComponentFunctionType<'a>>, /// Information relating to the lifting of the core function. info: CanonLift<'a>, @@ -265,8 +282,10 @@ pub enum CanonicalFuncKind<'a> { /// Information relating to lifting a core function. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct CanonLift<'a> { /// The core function being lifted. + #[serde(borrow)] pub func: CoreItemRef<'a, kw::func>, /// The canonical options for the lifting. pub opts: Vec>, @@ -302,8 +321,10 @@ impl Default for CanonLift<'_> { /// Information relating to lowering a component function. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct CanonLower<'a> { /// The function being lowered. + #[serde(borrow)] pub func: ItemRef<'a, kw::func>, /// The canonical options for the lowering. pub opts: Vec>, @@ -336,8 +357,10 @@ impl Default for CanonLower<'_> { /// Information relating to the `resource.new` intrinsic. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct CanonResourceNew<'a> { /// The resource type that this intrinsic creates an owned reference to. + #[serde(borrow)] pub ty: Index<'a>, } @@ -361,8 +384,10 @@ impl Default for CanonResourceNew<'_> { /// Information relating to the `resource.drop` intrinsic. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct CanonResourceDrop<'a> { /// The resource type that this intrinsic is dropping. + #[serde(borrow)] pub ty: Index<'a>, } @@ -386,8 +411,10 @@ impl Default for CanonResourceDrop<'_> { /// Information relating to the `resource.rep` intrinsic. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct CanonResourceRep<'a> { /// The resource type that this intrinsic is accessing. + #[serde(borrow)] pub ty: Index<'a>, } @@ -411,6 +438,8 @@ impl Default for CanonResourceRep<'_> { #[derive(Debug)] /// Canonical ABI options. +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum CanonOpt<'a> { /// Encode strings as UTF-8. StringUtf8, @@ -419,6 +448,7 @@ pub enum CanonOpt<'a> { /// Encode strings as "compact UTF-16". StringLatin1Utf16, /// Use the specified memory for canonical ABI memory access. + #[serde(borrow)] Memory(CoreItemRef<'a, kw::memory>), /// Use the specified reallocation function for memory allocations. Realloc(CoreItemRef<'a, kw::func>), @@ -467,7 +497,7 @@ impl<'a> Parse<'a> for CanonOpt<'a> { } } -fn parse_trailing_item_ref(kind: T, parser: Parser) -> Result> { +fn parse_trailing_item_ref(kind: T, parser: Parser) -> Result> { Ok(CoreItemRef { kind, idx: parser.parse()?, diff --git a/crates/wast/src/component/import.rs b/crates/wast/src/component/import.rs index 6689f4a765..f8d4eddd8f 100644 --- a/crates/wast/src/component/import.rs +++ b/crates/wast/src/component/import.rs @@ -2,13 +2,16 @@ use crate::component::*; use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Id, Index, LParen, NameAnnotation, Span}; +use serde_derive::{Serialize, Deserialize}; /// An `import` statement and entry in a WebAssembly component. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ComponentImport<'a> { /// Where this `import` was defined pub span: Span, /// The name of the item being imported. + #[serde(borrow)] pub name: ComponentExternName<'a>, /// The item that's being imported. pub item: ItemSig<'a>, @@ -25,6 +28,7 @@ impl<'a> Parse<'a> for ComponentImport<'a> { /// The different ways an import can be named. #[derive(Debug, Copy, Clone)] +#[derive(Serialize, Deserialize)] pub struct ComponentExternName<'a>(pub &'a str); impl<'a> Parse<'a> for ComponentExternName<'a> { @@ -49,11 +53,13 @@ impl<'a> Parse<'a> for ComponentExternName<'a> { /// An item signature for imported items. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ItemSig<'a> { /// Where this item is defined in the source. pub span: Span, /// An optional identifier used during name resolution to refer to this item /// from the rest of the component. + #[serde(borrow)] pub id: Option>, /// An optional name which, for functions, will be stored in the /// custom `name` section. @@ -70,7 +76,11 @@ impl<'a> Parse<'a> for ItemSig<'a> { /// An item signature for imported items. #[derive(Debug)] -pub struct ItemSigNoName<'a>(pub ItemSig<'a>); +#[derive(Serialize, Deserialize)] +pub struct ItemSigNoName<'a>( + #[serde(borrow)] + pub ItemSig<'a> +); impl<'a> Parse<'a> for ItemSigNoName<'a> { fn parse(parser: Parser<'a>) -> Result { @@ -114,8 +124,11 @@ fn parse_item_sig<'a>(parser: Parser<'a>, name: bool) -> Result> { /// The kind of signatures for imported items. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ItemSigKind<'a> { /// The item signature is for a core module. + #[serde(borrow)] CoreModule(CoreTypeUse<'a, ModuleType<'a>>), /// The item signature is for a function. Func(ComponentTypeUse<'a, ComponentFunctionType<'a>>), @@ -131,8 +144,11 @@ pub enum ItemSigKind<'a> { /// Represents the bounds applied to types being imported. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum TypeBounds<'a> { /// The equality type bounds. + #[serde(borrow)] Eq(Index<'a>), /// A resource type is imported/exported, SubResource, @@ -159,8 +175,10 @@ impl<'a> Parse<'a> for TypeBounds<'a> { /// This is the same as `core::InlineImport` except only one string import is /// required. #[derive(Debug, Clone)] +#[derive(Serialize, Deserialize)] pub struct InlineImport<'a> { /// The name of the item being imported. + #[serde(borrow)] pub name: ComponentExternName<'a>, } diff --git a/crates/wast/src/component/instance.rs b/crates/wast/src/component/instance.rs index dc7b3faf44..6d907005b3 100644 --- a/crates/wast/src/component/instance.rs +++ b/crates/wast/src/component/instance.rs @@ -3,14 +3,17 @@ use crate::core; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, LParen, NameAnnotation, Span}; +use serde_derive::{Serialize, Deserialize}; /// A core instance defined by instantiation or exporting core items. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct CoreInstance<'a> { /// Where this `core instance` was defined. pub span: Span, /// An identifier that this instance is resolved with (optionally) for name /// resolution. + #[serde(borrow)] pub id: Option>, /// An optional name for this instance stored in the custom `name` section. pub name: Option>, @@ -37,10 +40,13 @@ impl<'a> Parse<'a> for CoreInstance<'a> { /// The kinds of core instances in the text format. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum CoreInstanceKind<'a> { /// Instantiate a core module. Instantiate { /// The module being instantiated. + #[serde(borrow)] module: ItemRef<'a, kw::module>, /// Arguments used to instantiate the instance. args: Vec>, @@ -73,6 +79,7 @@ impl Default for kw::module { /// An argument to instantiate a core module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct CoreInstantiationArg<'a> { /// The name of the instantiation argument. pub name: &'a str, @@ -102,8 +109,11 @@ impl<'a> Parse<'a> for Vec> { /// The kind of core instantiation argument. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum CoreInstantiationArgKind<'a> { /// The argument is a reference to an instance. + #[serde(borrow)] Instance(CoreItemRef<'a, kw::instance>), /// The argument is an instance created from local exported core items. /// @@ -127,6 +137,7 @@ impl<'a> Parse<'a> for CoreInstantiationArgKind<'a> { /// An exported item as part of a core instance. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct CoreInstanceExport<'a> { /// Where this export was defined. pub span: Span, @@ -158,11 +169,13 @@ impl<'a> Parse<'a> for Vec> { /// A component instance defined by instantiation or exporting items. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Instance<'a> { /// Where this `instance` was defined. pub span: Span, /// An identifier that this instance is resolved with (optionally) for name /// resolution. + #[serde(borrow)] pub id: Option>, /// An optional name for this instance stored in the custom `name` section. pub name: Option>, @@ -193,10 +206,13 @@ impl<'a> Parse<'a> for Instance<'a> { /// The kinds of instances in the text format. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum InstanceKind<'a> { /// The `(instance (import "x"))` sugar syntax Import { /// The name of the import + #[serde(borrow)] import: InlineImport<'a>, /// The type of the instance being imported ty: ComponentTypeUse<'a, InstanceType<'a>>, @@ -243,6 +259,7 @@ impl Default for kw::component { /// An argument to instantiate a component. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct InstantiationArg<'a> { /// The name of the instantiation argument. pub name: &'a str, @@ -272,8 +289,11 @@ impl<'a> Parse<'a> for Vec> { /// The kind of instantiation argument. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum InstantiationArgKind<'a> { /// The argument is a reference to a component item. + #[serde(borrow)] Item(ComponentExportKind<'a>), /// The argument is an instance created from local exported items. /// diff --git a/crates/wast/src/component/item_ref.rs b/crates/wast/src/component/item_ref.rs index cc8edc9b5b..810f9e83a9 100644 --- a/crates/wast/src/component/item_ref.rs +++ b/crates/wast/src/component/item_ref.rs @@ -1,5 +1,7 @@ use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::Index; +use serde::Serialize as SerializeT; +use serde_derive::{Serialize, Deserialize}; fn peek(cursor: Cursor) -> Result { // This is a little fancy because when parsing something like: @@ -39,7 +41,8 @@ fn peek(cursor: Cursor) -> Result { /// Parses core item references. #[derive(Clone, Debug)] -pub struct CoreItemRef<'a, K> { +#[derive(Serialize, Deserialize)] +pub struct CoreItemRef<'a, K: SerializeT> { /// The item kind being parsed. pub kind: K, /// The item or instance reference. @@ -48,7 +51,7 @@ pub struct CoreItemRef<'a, K> { pub export_name: Option<&'a str>, } -impl<'a, K: Parse<'a>> Parse<'a> for CoreItemRef<'a, K> { +impl<'a, K: Parse<'a> + SerializeT> Parse<'a> for CoreItemRef<'a, K> { fn parse(parser: Parser<'a>) -> Result { // This does not parse the surrounding `(` and `)` because // core prefix is context dependent and only the caller knows if it should be @@ -64,7 +67,7 @@ impl<'a, K: Parse<'a>> Parse<'a> for CoreItemRef<'a, K> { } } -impl<'a, K: Peek> Peek for CoreItemRef<'a, K> { +impl<'a, K: Peek + SerializeT> Peek for CoreItemRef<'a, K> { fn peek(cursor: Cursor<'_>) -> Result { peek::(cursor) } @@ -76,16 +79,18 @@ impl<'a, K: Peek> Peek for CoreItemRef<'a, K> { /// Parses component item references. #[derive(Clone, Debug)] -pub struct ItemRef<'a, K> { +#[derive(Serialize, Deserialize)] +pub struct ItemRef<'a, K: SerializeT> { /// The item kind being parsed. pub kind: K, /// The item or instance reference. + #[serde(borrow)] pub idx: Index<'a>, /// Export names to resolve the item from. pub export_names: Vec<&'a str>, } -impl<'a, K: Parse<'a>> Parse<'a> for ItemRef<'a, K> { +impl<'a, K: Parse<'a> + SerializeT> Parse<'a> for ItemRef<'a, K> { fn parse(parser: Parser<'a>) -> Result { let kind = parser.parse::()?; let idx = parser.parse()?; @@ -101,7 +106,7 @@ impl<'a, K: Parse<'a>> Parse<'a> for ItemRef<'a, K> { } } -impl<'a, K: Peek> Peek for ItemRef<'a, K> { +impl<'a, K: Peek + SerializeT> Peek for ItemRef<'a, K> { fn peek(cursor: Cursor<'_>) -> Result { peek::(cursor) } @@ -113,9 +118,9 @@ impl<'a, K: Peek> Peek for ItemRef<'a, K> { /// Convenience structure to parse `$f` or `(item $f)`. #[derive(Clone, Debug)] -pub struct IndexOrRef<'a, K>(pub ItemRef<'a, K>); +pub struct IndexOrRef<'a, K: SerializeT>(pub ItemRef<'a, K>); -impl<'a, K> Parse<'a> for IndexOrRef<'a, K> +impl<'a, K: SerializeT> Parse<'a> for IndexOrRef<'a, K> where K: Parse<'a> + Default, { @@ -134,9 +139,9 @@ where /// Convenience structure to parse `$f` or `(item $f)`. #[derive(Clone, Debug)] -pub struct IndexOrCoreRef<'a, K>(pub CoreItemRef<'a, K>); +pub struct IndexOrCoreRef<'a, K: SerializeT>(pub CoreItemRef<'a, K>); -impl<'a, K> Parse<'a> for IndexOrCoreRef<'a, K> +impl<'a, K: SerializeT> Parse<'a> for IndexOrCoreRef<'a, K> where K: Parse<'a> + Default, { diff --git a/crates/wast/src/component/module.rs b/crates/wast/src/component/module.rs index 6871af8d4c..77d8dc2730 100644 --- a/crates/wast/src/component/module.rs +++ b/crates/wast/src/component/module.rs @@ -3,16 +3,19 @@ use crate::core; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, NameAnnotation, Span}; +use serde_derive::{Serialize, Deserialize}; /// A core WebAssembly module to be created as part of a component. /// /// This is a member of the core module section. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct CoreModule<'a> { /// Where this `core module` was defined. pub span: Span, /// An identifier that this module is resolved with (optionally) for name /// resolution. + #[serde(borrow)] pub id: Option>, /// An optional name for this module stored in the custom `name` section. pub name: Option>, @@ -25,10 +28,13 @@ pub struct CoreModule<'a> { /// Possible ways to define a core module in the text format. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum CoreModuleKind<'a> { /// A core module which is actually defined as an import Import { /// Where this core module is imported from + #[serde(borrow)] import: InlineImport<'a>, /// The type that this core module will have. ty: CoreTypeUse<'a, ModuleType<'a>>, diff --git a/crates/wast/src/component/resolve.rs b/crates/wast/src/component/resolve.rs index c0122ef73f..9873362248 100644 --- a/crates/wast/src/component/resolve.rs +++ b/crates/wast/src/component/resolve.rs @@ -5,6 +5,7 @@ use crate::names::Namespace; use crate::token::Span; use crate::token::{Id, Index}; use crate::Error; +use serde::Serialize as SerializeT; /// Resolve the fields of a component and everything nested within it, changing /// `Index::Id` to `Index::Num` and expanding alias syntax sugar. @@ -391,7 +392,7 @@ impl<'a> Resolver<'a> { Ok(()) } - fn core_type_use(&mut self, ty: &mut CoreTypeUse<'a, T>) -> Result<(), Error> { + fn core_type_use(&mut self, ty: &mut CoreTypeUse<'a, T>) -> Result<(), Error> { let item = match ty { CoreTypeUse::Ref(r) => r, CoreTypeUse::Inline(_) => { @@ -401,7 +402,7 @@ impl<'a> Resolver<'a> { self.core_item_ref(item) } - fn component_type_use(&mut self, ty: &mut ComponentTypeUse<'a, T>) -> Result<(), Error> { + fn component_type_use(&mut self, ty: &mut ComponentTypeUse<'a, T>) -> Result<(), Error> { let item = match ty { ComponentTypeUse::Ref(r) => r, ComponentTypeUse::Inline(_) => { @@ -607,7 +608,7 @@ impl<'a> Resolver<'a> { ) } - fn core_item_ref(&mut self, item: &mut CoreItemRef<'a, K>) -> Result<(), Error> + fn core_item_ref(&mut self, item: &mut CoreItemRef<'a, K>) -> Result<(), Error> where K: CoreItem + Copy, { @@ -643,7 +644,7 @@ impl<'a> Resolver<'a> { Ok(()) } - fn component_item_ref(&mut self, item: &mut ItemRef<'a, K>) -> Result<(), Error> + fn component_item_ref(&mut self, item: &mut ItemRef<'a, K>) -> Result<(), Error> where K: ComponentItem + Copy, { diff --git a/crates/wast/src/component/types.rs b/crates/wast/src/component/types.rs index 72eced02a8..73f99d5faa 100644 --- a/crates/wast/src/component/types.rs +++ b/crates/wast/src/component/types.rs @@ -7,14 +7,18 @@ use crate::parser::{Parse, Parser, Result}; use crate::token::Index; use crate::token::LParen; use crate::token::{Id, NameAnnotation, Span}; +use serde_derive::{Serialize, Deserialize}; +use serde::Serialize as SerializeT; /// A core type declaration. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct CoreType<'a> { /// Where this type was defined. pub span: Span, /// An optional identifier to refer to this `core type` by as part of name /// resolution. + #[serde(borrow)] pub id: Option>, /// An optional name for this type stored in the custom `name` section. pub name: Option>, @@ -44,8 +48,11 @@ impl<'a> Parse<'a> for CoreType<'a> { /// In the future this may be removed when module types are a part of /// a core module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum CoreTypeDef<'a> { /// The type definition is one of the core types. + #[serde(borrow)] Def(core::TypeDef<'a>), /// The type definition is a module type. Module(ModuleType<'a>), @@ -64,8 +71,10 @@ impl<'a> Parse<'a> for CoreTypeDef<'a> { /// A type definition for a core module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ModuleType<'a> { /// The declarations of the module type. + #[serde(borrow)] pub decls: Vec>, } @@ -80,6 +89,8 @@ impl<'a> Parse<'a> for ModuleType<'a> { /// The declarations of a [`ModuleType`]. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ModuleTypeDecl<'a> { /// A core type. Type(core::Type<'a>), @@ -123,11 +134,13 @@ impl<'a> Parse<'a> for Vec> { /// A type declaration in a component. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Type<'a> { /// Where this type was defined. pub span: Span, /// An optional identifier to refer to this `type` by as part of name /// resolution. + #[serde(borrow)] pub id: Option>, /// An optional name for this type stored in the custom `name` section. pub name: Option>, @@ -172,8 +185,11 @@ impl<'a> Type<'a> { /// A definition of a component type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum TypeDef<'a> { /// A defined value type. + #[serde(borrow)] Defined(ComponentDefinedType<'a>), /// A component function type. Func(ComponentFunctionType<'a>), @@ -220,6 +236,8 @@ impl<'a> Parse<'a> for TypeDef<'a> { /// A primitive value type. #[allow(missing_docs)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum PrimitiveValType { Bool, S8, @@ -312,8 +330,11 @@ impl Peek for PrimitiveValType { /// A component value type. #[allow(missing_docs)] #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ComponentValType<'a> { /// The value type is an inline defined type. + #[serde(borrow)] Inline(ComponentDefinedType<'a>), /// The value type is an index reference to a defined type. Ref(Index<'a>), @@ -344,7 +365,11 @@ impl Peek for ComponentValType<'_> { /// This variation does not parse type indexes. #[allow(missing_docs)] #[derive(Debug)] -pub struct InlineComponentValType<'a>(ComponentDefinedType<'a>); +#[derive(Serialize, Deserialize)] +pub struct InlineComponentValType<'a>( + #[serde(borrow)] + ComponentDefinedType<'a> +); impl<'a> Parse<'a> for InlineComponentValType<'a> { fn parse(parser: Parser<'a>) -> Result { @@ -364,8 +389,11 @@ impl<'a> Parse<'a> for InlineComponentValType<'a> { // A component defined type. #[allow(missing_docs)] #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ComponentDefinedType<'a> { Primitive(PrimitiveValType), + #[serde(borrow)] Record(Record<'a>), Variant(Variant<'a>), List(List<'a>), @@ -446,8 +474,10 @@ impl Peek for ComponentDefinedType<'_> { /// A record defined type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Record<'a> { /// The fields of the record. + #[serde(borrow)] pub fields: Vec>, } @@ -464,6 +494,7 @@ impl<'a> Parse<'a> for Record<'a> { /// A record type field. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct RecordField<'a> { /// The name of the field. pub name: &'a str, @@ -483,8 +514,10 @@ impl<'a> Parse<'a> for RecordField<'a> { /// A variant defined type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Variant<'a> { /// The cases of the variant type. + #[serde(borrow)] pub cases: Vec>, } @@ -501,6 +534,7 @@ impl<'a> Parse<'a> for Variant<'a> { /// A case of a variant type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct VariantCase<'a> { /// Where this `case` was defined pub span: Span, @@ -538,9 +572,15 @@ impl<'a> Parse<'a> for VariantCase<'a> { /// A refinement for a variant case. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum Refinement<'a> { /// The refinement is referenced by index. - Index(Span, Index<'a>), + Index( + Span, + #[serde(borrow)] + Index<'a> + ), /// The refinement has been resolved to an index into /// the cases of the variant. Resolved(u32), @@ -558,8 +598,10 @@ impl<'a> Parse<'a> for Refinement<'a> { /// A list type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct List<'a> { /// The element type of the array. + #[serde(borrow)] pub element: Box>, } @@ -574,8 +616,10 @@ impl<'a> Parse<'a> for List<'a> { /// A tuple type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Tuple<'a> { /// The types of the fields of the tuple. + #[serde(borrow)] pub fields: Vec>, } @@ -592,8 +636,10 @@ impl<'a> Parse<'a> for Tuple<'a> { /// A flags type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Flags<'a> { /// The names of the individual flags. + #[serde(borrow)] pub names: Vec<&'a str>, } @@ -610,8 +656,10 @@ impl<'a> Parse<'a> for Flags<'a> { /// An enum type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Enum<'a> { /// The tag names of the enum. + #[serde(borrow)] pub names: Vec<&'a str>, } @@ -628,8 +676,10 @@ impl<'a> Parse<'a> for Enum<'a> { /// An optional type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct OptionType<'a> { /// The type of the value, when a value is present. + #[serde(borrow)] pub element: Box>, } @@ -644,8 +694,10 @@ impl<'a> Parse<'a> for OptionType<'a> { /// A result type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ResultType<'a> { /// The type on success. + #[serde(borrow)] pub ok: Option>>, /// The type on failure. pub err: Option>>, @@ -674,9 +726,11 @@ impl<'a> Parse<'a> for ResultType<'a> { /// A component function type with parameters and result. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ComponentFunctionType<'a> { /// The parameters of a function, optionally each having an identifier for /// name resolution and a name for the custom `name` section. + #[serde(borrow)] pub params: Box<[ComponentFunctionParam<'a>]>, /// The result of a function, optionally each having an identifier for /// name resolution and a name for the custom `name` section. @@ -704,6 +758,7 @@ impl<'a> Parse<'a> for ComponentFunctionType<'a> { /// A parameter of a [`ComponentFunctionType`]. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ComponentFunctionParam<'a> { /// The name of the parameter pub name: &'a str, @@ -723,6 +778,7 @@ impl<'a> Parse<'a> for ComponentFunctionParam<'a> { /// A result of a [`ComponentFunctionType`]. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ComponentFunctionResult<'a> { /// An optionally-specified name of this result pub name: Option<&'a str>, @@ -742,10 +798,12 @@ impl<'a> Parse<'a> for ComponentFunctionResult<'a> { /// The type of an exported item from an component or instance type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ComponentExportType<'a> { /// Where this export was defined. pub span: Span, /// The name of this export. + #[serde(borrow)] pub name: ComponentExternName<'a>, /// The signature of the item. pub item: ItemSig<'a>, @@ -769,8 +827,10 @@ impl<'a> Parse<'a> for ComponentExportType<'a> { /// A type definition for a component type. #[derive(Debug, Default)] +#[derive(Serialize, Deserialize)] pub struct ComponentType<'a> { /// The declarations of the component type. + #[serde(borrow)] pub decls: Vec>, } @@ -785,8 +845,11 @@ impl<'a> Parse<'a> for ComponentType<'a> { /// A declaration of a component type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ComponentTypeDecl<'a> { /// A core type definition local to the component type. + #[serde(borrow)] CoreType(CoreType<'a>), /// A type definition local to the component type. Type(Type<'a>), @@ -829,8 +892,10 @@ impl<'a> Parse<'a> for Vec> { /// A type definition for an instance type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct InstanceType<'a> { /// The declarations of the instance type. + #[serde(borrow)] pub decls: Vec>, } @@ -845,8 +910,11 @@ impl<'a> Parse<'a> for InstanceType<'a> { /// A declaration of an instance type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum InstanceTypeDecl<'a> { /// A core type definition local to the component type. + #[serde(borrow)] CoreType(CoreType<'a>), /// A type definition local to the instance type. Type(Type<'a>), @@ -885,8 +953,10 @@ impl<'a> Parse<'a> for Vec> { /// A type definition for an instance type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ResourceType<'a> { /// Representation, in core WebAssembly, of this resource. + #[serde(borrow)] pub rep: core::ValType<'a>, /// The declarations of the instance type. pub dtor: Option>, @@ -912,7 +982,11 @@ impl<'a> Parse<'a> for ResourceType<'a> { /// A value type declaration used for values in import signatures. #[derive(Debug)] -pub struct ComponentValTypeUse<'a>(pub ComponentValType<'a>); +#[derive(Serialize, Deserialize)] +pub struct ComponentValTypeUse<'a>( + #[serde(borrow)] + pub ComponentValType<'a> +); impl<'a> Parse<'a> for ComponentValTypeUse<'a> { fn parse(parser: Parser<'a>) -> Result { @@ -928,14 +1002,17 @@ impl<'a> Parse<'a> for ComponentValTypeUse<'a> { /// This is the same as `TypeUse`, but accepts `$T` as shorthand for /// `(type $T)`. #[derive(Debug, Clone)] -pub enum CoreTypeUse<'a, T> { +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] +pub enum CoreTypeUse<'a, T: SerializeT> { /// The type that we're referencing. + #[serde(borrow)] Ref(CoreItemRef<'a, kw::r#type>), /// The inline type. Inline(T), } -impl<'a, T: Parse<'a>> Parse<'a> for CoreTypeUse<'a, T> { +impl<'a, T: Parse<'a> + SerializeT> Parse<'a> for CoreTypeUse<'a, T> { fn parse(parser: Parser<'a>) -> Result { // Here the core context is assumed, so no core prefix is expected if parser.peek::()? && parser.peek2::>()? { @@ -946,7 +1023,7 @@ impl<'a, T: Parse<'a>> Parse<'a> for CoreTypeUse<'a, T> { } } -impl Default for CoreTypeUse<'_, T> { +impl Default for CoreTypeUse<'_, T> { fn default() -> Self { let span = Span::from_offset(0); Self::Ref(CoreItemRef { @@ -962,14 +1039,17 @@ impl Default for CoreTypeUse<'_, T> { /// This is the same as `TypeUse`, but accepts `$T` as shorthand for /// `(type $T)`. #[derive(Debug, Clone)] -pub enum ComponentTypeUse<'a, T> { +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] +pub enum ComponentTypeUse<'a, T: SerializeT> { /// The type that we're referencing. + #[serde(borrow)] Ref(ItemRef<'a, kw::r#type>), /// The inline type. Inline(T), } -impl<'a, T: Parse<'a>> Parse<'a> for ComponentTypeUse<'a, T> { +impl<'a, T: Parse<'a> + SerializeT> Parse<'a> for ComponentTypeUse<'a, T> { fn parse(parser: Parser<'a>) -> Result { if parser.peek::()? && parser.peek2::>()? { Ok(Self::Ref(parser.parens(|parser| parser.parse())?)) @@ -979,7 +1059,7 @@ impl<'a, T: Parse<'a>> Parse<'a> for ComponentTypeUse<'a, T> { } } -impl Default for ComponentTypeUse<'_, T> { +impl Default for ComponentTypeUse<'_, T> { fn default() -> Self { let span = Span::from_offset(0); Self::Ref(ItemRef { diff --git a/crates/wast/src/core/custom.rs b/crates/wast/src/core/custom.rs index 2205bf514c..640f690033 100644 --- a/crates/wast/src/core/custom.rs +++ b/crates/wast/src/core/custom.rs @@ -1,11 +1,15 @@ use crate::parser::{Parse, Parser, Result}; use crate::token::{self, Span}; use crate::{annotation, kw}; +use serde_derive::{Serialize, Deserialize}; /// A custom section within a wasm module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum Custom<'a> { /// A raw custom section with the manual placement and bytes specified. + #[serde(borrow)] Raw(RawCustomSection<'a>), /// A producers custom section. Producers(Producers<'a>), @@ -47,6 +51,7 @@ impl<'a> Parse<'a> for Custom<'a> { /// A wasm custom section within a module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct RawCustomSection<'a> { /// Where this `@custom` was defined. pub span: Span, @@ -63,6 +68,8 @@ pub struct RawCustomSection<'a> { /// Possible locations to place a custom section within a module. #[derive(Debug, PartialEq, Copy, Clone)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum CustomPlace { /// This custom section will appear before the first section in the module. BeforeFirst, @@ -77,6 +84,8 @@ pub enum CustomPlace { /// Known sections that custom sections can be placed relative to. #[derive(Debug, PartialEq, Eq, Copy, Clone)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum CustomPlaceAnchor { Type, Import, @@ -196,7 +205,9 @@ impl<'a> Parse<'a> for CustomPlaceAnchor { /// A producers custom section #[allow(missing_docs)] #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Producers<'a> { + #[serde(borrow)] pub fields: Vec<(&'a str, Vec<(&'a str, &'a str)>)>, } @@ -244,13 +255,17 @@ impl<'a> Parse<'a> for Producers<'a> { /// A `dylink.0` custom section #[allow(missing_docs)] #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Dylink0<'a> { + #[serde(borrow)] pub subsections: Vec>, } /// Possible subsections of the `dylink.0` custom section #[derive(Debug)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum Dylink0Subsection<'a> { MemInfo { memory_size: u32, @@ -258,6 +273,7 @@ pub enum Dylink0Subsection<'a> { table_size: u32, table_align: u32, }, + #[serde(borrow)] Needed(Vec<&'a str>), ExportInfo(Vec<(&'a str, u32)>), ImportInfo(Vec<(&'a str, &'a str, u32)>), diff --git a/crates/wast/src/core/export.rs b/crates/wast/src/core/export.rs index 19765d36df..eb4b65db3d 100644 --- a/crates/wast/src/core/export.rs +++ b/crates/wast/src/core/export.rs @@ -1,9 +1,11 @@ use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Index, Span}; +use serde_derive::{Serialize, Deserialize}; /// A entry in a WebAssembly module's export section. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Export<'a> { /// Where this export was defined. pub span: Span, @@ -19,6 +21,8 @@ pub struct Export<'a> { /// contained in an [`Export`]. #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ExportKind { Func, Table, @@ -105,8 +109,10 @@ kw_conversions! { /// A listing of inline `(export "foo")` statements on a WebAssembly item in /// its textual format. #[derive(Debug, Default)] +#[derive(Serialize, Deserialize)] pub struct InlineExport<'a> { /// The extra names to export an item as, if any. + #[serde(borrow)] pub names: Vec<&'a str>, } diff --git a/crates/wast/src/core/expr.rs b/crates/wast/src/core/expr.rs index b45950b896..6439cedc29 100644 --- a/crates/wast/src/core/expr.rs +++ b/crates/wast/src/core/expr.rs @@ -5,6 +5,7 @@ use crate::kw; use crate::parser::{Cursor, Parse, Parser, Result}; use crate::token::*; use std::mem; +use serde_derive::{Serialize, Deserialize}; /// An expression, or a list of instructions, in the WebAssembly text format. /// @@ -13,7 +14,9 @@ use std::mem; /// at the end of an expression is not included in the `instrs` field. #[derive(Debug)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] pub struct Expression<'a> { + #[serde(borrow)] pub instrs: Box<[Instruction<'a>]>, pub branch_hints: Vec, } @@ -23,6 +26,7 @@ pub struct Expression<'a> { /// is to store the offset of the following instruction and check that /// it's followed by `br_if` or `if`. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct BranchHint { /// Index of instructions in `instrs` field of `Expression` that this hint /// appplies to. @@ -352,6 +356,8 @@ macro_rules! instructions { /// that this crate currently parses. #[derive(Debug)] #[allow(missing_docs)] + #[derive(Serialize, Deserialize)] + #[serde(tag = "type", content = "val")] pub enum Instruction<'a> { $( $(#[$doc])* @@ -446,6 +452,7 @@ macro_rules! instructions { instructions! { pub enum Instruction<'a> { + #[serde(borrow)] Block(Box>) : [0x02] : "block", If(Box>) : [0x04] : "if", Else(Option>) : [0x05] : "else", @@ -1125,7 +1132,9 @@ impl<'a> Instruction<'a> { /// the block. #[derive(Debug)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] pub struct BlockType<'a> { + #[serde(borrow)] pub label: Option>, pub label_name: Option>, pub ty: TypeUse<'a, FunctionType<'a>>, @@ -1145,7 +1154,9 @@ impl<'a> Parse<'a> for BlockType<'a> { #[derive(Debug)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] pub struct TryTable<'a> { + #[serde(borrow)] pub block: Box>, pub catches: Vec>, } @@ -1189,8 +1200,11 @@ impl<'a> Parse<'a> for TryTable<'a> { #[derive(Debug)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum TryTableCatchKind<'a> { // Catch a tagged exception, do not capture an exnref. + #[serde(borrow)] Catch(Index<'a>), // Catch a tagged exception, and capture the exnref. CatchRef(Index<'a>), @@ -1212,7 +1226,9 @@ impl<'a> TryTableCatchKind<'a> { #[derive(Debug)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] pub struct TryTableCatch<'a> { + #[serde(borrow)] pub kind: TryTableCatchKind<'a>, pub label: Index<'a>, } @@ -1220,7 +1236,9 @@ pub struct TryTableCatch<'a> { /// Extra information associated with the func.bind instruction. #[derive(Debug)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] pub struct FuncBindType<'a> { + #[serde(borrow)] pub ty: TypeUse<'a, FunctionType<'a>>, } @@ -1237,7 +1255,9 @@ impl<'a> Parse<'a> for FuncBindType<'a> { /// Extra information associated with the let instruction. #[derive(Debug)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] pub struct LetType<'a> { + #[serde(borrow)] pub block: Box>, pub locals: Box<[Local<'a>]>, } @@ -1254,7 +1274,9 @@ impl<'a> Parse<'a> for LetType<'a> { /// Extra information associated with the `br_table` instruction. #[allow(missing_docs)] #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct BrTableIndices<'a> { + #[serde(borrow)] pub labels: Vec>, pub default: Index<'a>, } @@ -1272,6 +1294,7 @@ impl<'a> Parse<'a> for BrTableIndices<'a> { /// Payload for lane-related instructions. Unsigned with no + prefix. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct LaneArg { /// The lane argument. pub lane: u8, @@ -1300,6 +1323,7 @@ impl<'a> Parse<'a> for LaneArg { /// Payload for memory-related instructions indicating offset/alignment of /// memory accesses. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct MemArg<'a> { /// The alignment of this access. /// @@ -1309,6 +1333,7 @@ pub struct MemArg<'a> { /// The offset, in bytes of this access. pub offset: u64, /// The memory index we're accessing + #[serde(borrow)] pub memory: Index<'a>, } @@ -1375,8 +1400,10 @@ impl<'a> MemArg<'a> { /// Extra data associated with the `loadN_lane` and `storeN_lane` instructions. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct LoadOrStoreLane<'a> { /// The memory argument for this instruction. + #[serde(borrow)] pub memarg: MemArg<'a>, /// The lane argument for this instruction. pub lane: LaneArg, @@ -1429,8 +1456,10 @@ impl<'a> LoadOrStoreLane<'a> { /// Extra data associated with the `call_indirect` instruction. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct CallIndirect<'a> { /// The table that this call is going to be indexing. + #[serde(borrow)] pub table: Index<'a>, /// Type type signature that this `call_indirect` instruction is using. pub ty: TypeUse<'a, FunctionType<'a>>, @@ -1450,8 +1479,10 @@ impl<'a> Parse<'a> for CallIndirect<'a> { /// Extra data associated with the `table.init` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct TableInit<'a> { /// The index of the table we're copying into. + #[serde(borrow)] pub table: Index<'a>, /// The index of the element segment we're copying into a table. pub elem: Index<'a>, @@ -1472,8 +1503,10 @@ impl<'a> Parse<'a> for TableInit<'a> { /// Extra data associated with the `table.copy` instruction. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct TableCopy<'a> { /// The index of the destination table to copy into. + #[serde(borrow)] pub dst: Index<'a>, /// The index of the source table to copy from. pub src: Index<'a>, @@ -1494,8 +1527,10 @@ impl<'a> Parse<'a> for TableCopy<'a> { /// Extra data associated with unary table instructions. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct TableArg<'a> { /// The index of the table argument. + #[serde(borrow)] pub dst: Index<'a>, } @@ -1512,8 +1547,10 @@ impl<'a> Parse<'a> for TableArg<'a> { /// Extra data associated with unary memory instructions. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct MemoryArg<'a> { /// The index of the memory space. + #[serde(borrow)] pub mem: Index<'a>, } @@ -1530,8 +1567,10 @@ impl<'a> Parse<'a> for MemoryArg<'a> { /// Extra data associated with the `memory.init` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct MemoryInit<'a> { /// The index of the data segment we're copying into memory. + #[serde(borrow)] pub data: Index<'a>, /// The index of the memory we're copying into, pub mem: Index<'a>, @@ -1552,8 +1591,10 @@ impl<'a> Parse<'a> for MemoryInit<'a> { /// Extra data associated with the `memory.copy` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct MemoryCopy<'a> { /// The index of the memory we're copying from. + #[serde(borrow)] pub src: Index<'a>, /// The index of the memory we're copying to. pub dst: Index<'a>, @@ -1574,8 +1615,10 @@ impl<'a> Parse<'a> for MemoryCopy<'a> { /// Extra data associated with the `struct.get/set` instructions #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct StructAccess<'a> { /// The index of the struct type we're accessing. + #[serde(borrow)] pub r#struct: Index<'a>, /// The index of the field of the struct we're accessing pub field: Index<'a>, @@ -1592,8 +1635,10 @@ impl<'a> Parse<'a> for StructAccess<'a> { /// Extra data associated with the `array.fill` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ArrayFill<'a> { /// The index of the array type we're filling. + #[serde(borrow)] pub array: Index<'a>, } @@ -1607,8 +1652,10 @@ impl<'a> Parse<'a> for ArrayFill<'a> { /// Extra data associated with the `array.copy` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ArrayCopy<'a> { /// The index of the array type we're copying to. + #[serde(borrow)] pub dest_array: Index<'a>, /// The index of the array type we're copying from. pub src_array: Index<'a>, @@ -1625,8 +1672,10 @@ impl<'a> Parse<'a> for ArrayCopy<'a> { /// Extra data associated with the `array.init_[data/elem]` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ArrayInit<'a> { /// The index of the array type we're initializing. + #[serde(borrow)] pub array: Index<'a>, /// The index of the data or elem segment we're reading from. pub segment: Index<'a>, @@ -1643,8 +1692,10 @@ impl<'a> Parse<'a> for ArrayInit<'a> { /// Extra data associated with the `array.new_fixed` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ArrayNewFixed<'a> { /// The index of the array type we're accessing. + #[serde(borrow)] pub array: Index<'a>, /// The amount of values to initialize the array with. pub length: u32, @@ -1661,8 +1712,10 @@ impl<'a> Parse<'a> for ArrayNewFixed<'a> { /// Extra data associated with the `array.new_data` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ArrayNewData<'a> { /// The index of the array type we're accessing. + #[serde(borrow)] pub array: Index<'a>, /// The data segment to initialize from. pub data_idx: Index<'a>, @@ -1679,8 +1732,10 @@ impl<'a> Parse<'a> for ArrayNewData<'a> { /// Extra data associated with the `array.new_elem` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct ArrayNewElem<'a> { /// The index of the array type we're accessing. + #[serde(borrow)] pub array: Index<'a>, /// The elem segment to initialize from. pub elem_idx: Index<'a>, @@ -1697,8 +1752,10 @@ impl<'a> Parse<'a> for ArrayNewElem<'a> { /// Extra data associated with the `ref.cast` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct RefCast<'a> { /// The type to cast to. + #[serde(borrow)] pub r#type: RefType<'a>, } @@ -1712,8 +1769,10 @@ impl<'a> Parse<'a> for RefCast<'a> { /// Extra data associated with the `ref.test` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct RefTest<'a> { /// The type to test for. + #[serde(borrow)] pub r#type: RefType<'a>, } @@ -1727,8 +1786,10 @@ impl<'a> Parse<'a> for RefTest<'a> { /// Extra data associated with the `br_on_cast` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct BrOnCast<'a> { /// The label to branch to. + #[serde(borrow)] pub label: Index<'a>, /// The type we're casting from. pub from_type: RefType<'a>, @@ -1748,8 +1809,10 @@ impl<'a> Parse<'a> for BrOnCast<'a> { /// Extra data associated with the `br_on_cast_fail` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct BrOnCastFail<'a> { /// The label to branch to. + #[serde(borrow)] pub label: Index<'a>, /// The type we're casting from. pub from_type: RefType<'a>, @@ -1770,6 +1833,8 @@ impl<'a> Parse<'a> for BrOnCastFail<'a> { /// Different ways to specify a `v128.const` instruction #[derive(Debug)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum V128Const { I8x16([i8; 16]), I16x8([i16; 8]), @@ -1935,6 +2000,7 @@ impl<'a> Parse<'a> for V128Const { /// Lanes being shuffled in the `i8x16.shuffle` instruction #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct I8x16Shuffle { #[allow(missing_docs)] pub lanes: [u8; 16], @@ -1967,8 +2033,10 @@ impl<'a> Parse<'a> for I8x16Shuffle { /// Payload of the `select` instructions #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct SelectTypes<'a> { #[allow(missing_docs)] + #[serde(borrow)] pub tys: Option>>, } diff --git a/crates/wast/src/core/func.rs b/crates/wast/src/core/func.rs index 24cc2993ca..b1a1400ddf 100644 --- a/crates/wast/src/core/func.rs +++ b/crates/wast/src/core/func.rs @@ -2,16 +2,19 @@ use crate::core::*; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, NameAnnotation, Span}; +use serde_derive::{Serialize, Deserialize}; /// A WebAssembly function to be inserted into a module. /// /// This is a member of both the function and code sections. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Func<'a> { /// Where this `func` was defined. pub span: Span, /// An identifier that this function is resolved with (optionally) for name /// resolution. + #[serde(borrow)] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -27,12 +30,15 @@ pub struct Func<'a> { /// Possible ways to define a function in the text format. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum FuncKind<'a> { /// A function which is actually defined as an import, such as: /// /// ```text /// (func (type 3) (import "foo" "bar")) /// ``` + #[serde(borrow)] Import(InlineImport<'a>), /// Almost all functions, those defined inline in a wasm module. @@ -82,9 +88,11 @@ impl<'a> Parse<'a> for Func<'a> { /// Each local has an optional identifier for name resolution, an optional name /// for the custom `name` section, and a value type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Local<'a> { /// An identifier that this local is resolved with (optionally) for name /// resolution. + #[serde(borrow)] pub id: Option>, /// An optional name for this local stored in the custom `name` section. pub name: Option>, diff --git a/crates/wast/src/core/global.rs b/crates/wast/src/core/global.rs index b8ce287fd8..05441658c6 100644 --- a/crates/wast/src/core/global.rs +++ b/crates/wast/src/core/global.rs @@ -2,13 +2,16 @@ use crate::core::*; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, NameAnnotation, Span}; +use serde_derive::{Serialize, Deserialize}; /// A WebAssembly global in a module #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Global<'a> { /// Where this `global` was defined. pub span: Span, /// An optional name to reference this global by + #[serde(borrow)] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -23,12 +26,15 @@ pub struct Global<'a> { /// Different kinds of globals that can be defined in a module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum GlobalKind<'a> { /// A global which is actually defined as an import, such as: /// /// ```text /// (global i32 (import "foo" "bar")) /// ``` + #[serde(borrow)] Import(InlineImport<'a>), /// A global defined inline in the module itself diff --git a/crates/wast/src/core/import.rs b/crates/wast/src/core/import.rs index e7c49bb862..0aafe28512 100644 --- a/crates/wast/src/core/import.rs +++ b/crates/wast/src/core/import.rs @@ -2,9 +2,11 @@ use crate::core::*; use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Id, NameAnnotation, Span}; +use serde_derive::{Serialize, Deserialize}; /// An `import` statement and entry in a WebAssembly module. #[derive(Debug, Clone)] +#[derive(Serialize, Deserialize)] pub struct Import<'a> { /// Where this `import` was defined pub span: Span, @@ -33,11 +35,13 @@ impl<'a> Parse<'a> for Import<'a> { #[derive(Debug, Clone)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] pub struct ItemSig<'a> { /// Where this item is defined in the source. pub span: Span, /// An optional identifier used during name resolution to refer to this item /// from the rest of the module. + #[serde(borrow)] pub id: Option>, /// An optional name which, for functions, will be stored in the /// custom `name` section. @@ -48,7 +52,10 @@ pub struct ItemSig<'a> { #[derive(Debug, Clone)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ItemKind<'a> { + #[serde(borrow)] Func(TypeUse<'a, FunctionType<'a>>), Table(TableType<'a>), Memory(MemoryType), @@ -113,6 +120,7 @@ impl<'a> Parse<'a> for ItemSig<'a> { /// `Peek` rather than `Option`. #[derive(Debug, Copy, Clone)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] pub struct InlineImport<'a> { pub module: &'a str, pub field: &'a str, diff --git a/crates/wast/src/core/memory.rs b/crates/wast/src/core/memory.rs index eb1baa1a95..81be9434b9 100644 --- a/crates/wast/src/core/memory.rs +++ b/crates/wast/src/core/memory.rs @@ -2,13 +2,16 @@ use crate::core::*; use crate::kw; use crate::parser::{Lookahead1, Parse, Parser, Peek, Result}; use crate::token::*; +use serde_derive::{Serialize, Deserialize}; /// A defined WebAssembly memory instance inside of a module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Memory<'a> { /// Where this `memory` was defined pub span: Span, /// An optional name to refer to this memory by. + #[serde(borrow)] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -21,10 +24,13 @@ pub struct Memory<'a> { /// Different syntactical ways a memory can be defined in a module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum MemoryKind<'a> { /// This memory is actually an inlined import definition. #[allow(missing_docs)] Import { + #[serde(borrow)] import: InlineImport<'a>, ty: MemoryType, }, @@ -91,11 +97,13 @@ impl<'a> Parse<'a> for Memory<'a> { /// A `data` directive in a WebAssembly module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Data<'a> { /// Where this `data` was defined pub span: Span, /// The optional name of this data segment + #[serde(borrow)] pub id: Option>, /// An optional name for this data stored in the custom `name` section. @@ -111,6 +119,8 @@ pub struct Data<'a> { /// Different kinds of data segments, either passive or active. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum DataKind<'a> { /// A passive data segment which isn't associated with a memory and is /// referenced from various instructions. @@ -120,6 +130,7 @@ pub enum DataKind<'a> { /// memory on module instantiation. Active { /// The memory that this `Data` will be associated with. + #[serde(borrow)] memory: Index<'a>, /// Initial offset to load this data segment at @@ -209,6 +220,8 @@ impl<'a> Parse<'a> for Data<'a> { /// Differnet ways the value of a data segment can be defined. #[derive(Debug)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum DataVal<'a> { String(&'a [u8]), Integral(Vec), diff --git a/crates/wast/src/core/module.rs b/crates/wast/src/core/module.rs index f74ce6b619..7fe142c505 100644 --- a/crates/wast/src/core/module.rs +++ b/crates/wast/src/core/module.rs @@ -2,15 +2,18 @@ use crate::core::*; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, Index, NameAnnotation, Span}; use crate::{annotation, kw}; +use serde_derive::{Serialize, Deserialize}; pub use crate::core::resolve::Names; /// A parsed WebAssembly core module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Module<'a> { /// Where this `module` was defined pub span: Span, /// An optional identifier this module is known by + #[serde(borrow)] pub id: Option>, /// An optional `@name` annotation for this module pub name: Option>, @@ -20,8 +23,11 @@ pub struct Module<'a> { /// The different kinds of ways to define a module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ModuleKind<'a> { /// A module defined in the textual s-expression format. + #[serde(borrow)] Text(Vec>), /// A module that had its raw binary bytes defined via the `binary` /// directive. @@ -142,7 +148,10 @@ impl<'a> Parse<'a> for Module<'a> { /// A listing of all possible fields that can make up a WebAssembly module. #[allow(missing_docs)] #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ModuleField<'a> { + #[serde(borrow)] Type(Type<'a>), Rec(Rec<'a>), Import(Import<'a>), diff --git a/crates/wast/src/core/table.rs b/crates/wast/src/core/table.rs index e7f0b0f974..c412fc3572 100644 --- a/crates/wast/src/core/table.rs +++ b/crates/wast/src/core/table.rs @@ -2,13 +2,16 @@ use crate::core::*; use crate::kw; use crate::parser::{Parse, Parser, Peek, Result}; use crate::token::{Id, Index, LParen, NameAnnotation, Span}; +use serde_derive::{Serialize, Deserialize}; /// A WebAssembly `table` directive in a module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Table<'a> { /// Where this table was defined. pub span: Span, /// An optional name to refer to this table by. + #[serde(borrow)] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -21,10 +24,13 @@ pub struct Table<'a> { /// Different ways to textually define a table. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum TableKind<'a> { /// This table is actually an inlined import definition. #[allow(missing_docs)] Import { + #[serde(borrow)] import: InlineImport<'a>, ty: TableType<'a>, }, @@ -100,10 +106,12 @@ impl<'a> Parse<'a> for Table<'a> { /// An `elem` segment in a WebAssembly module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Elem<'a> { /// Where this `elem` was defined. pub span: Span, /// An optional name by which to refer to this segment. + #[serde(borrow)] pub id: Option>, /// An optional name for this element stored in the custom `name` section. pub name: Option>, @@ -115,6 +123,8 @@ pub struct Elem<'a> { /// Different ways to define an element segment in an mdoule. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ElemKind<'a> { /// A passive segment that isn't associated with a table and can be used in /// various bulk-memory instructions. @@ -127,6 +137,7 @@ pub enum ElemKind<'a> { /// An active segment associated with a table. Active { /// The table this `elem` is initializing. + #[serde(borrow)] table: Index<'a>, /// The offset within `table` that we'll initialize at. offset: Expression<'a>, @@ -135,8 +146,11 @@ pub enum ElemKind<'a> { /// Different ways to define the element segment payload in a module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ElemPayload<'a> { /// This element segment has a contiguous list of function indices + #[serde(borrow)] Indices(Vec>), /// This element segment has a list of optional function indices, diff --git a/crates/wast/src/core/tag.rs b/crates/wast/src/core/tag.rs index 233b5e4cd0..d34636915a 100644 --- a/crates/wast/src/core/tag.rs +++ b/crates/wast/src/core/tag.rs @@ -2,13 +2,16 @@ use crate::core::*; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, NameAnnotation, Span}; +use serde_derive::{Serialize, Deserialize}; /// A WebAssembly tag directive, part of the exception handling proposal. #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Tag<'a> { /// Where this tag was defined pub span: Span, /// An optional name by which to refer to this tag in name resolution. + #[serde(borrow)] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -22,20 +25,26 @@ pub struct Tag<'a> { /// Listing of various types of tags that can be defined in a wasm module. #[derive(Clone, Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum TagType<'a> { /// An exception tag, where the payload is the type signature of the tag /// (constructor parameters, etc). + #[serde(borrow)] Exception(TypeUse<'a, FunctionType<'a>>), } /// Different kinds of tags that can be defined in a module. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum TagKind<'a> { /// An tag which is actually defined as an import, such as: /// /// ```text /// (tag (type 0) (import "foo" "bar")) /// ``` + #[serde(borrow)] Import(InlineImport<'a>), /// A tag defined inline in the module itself diff --git a/crates/wast/src/core/types.rs b/crates/wast/src/core/types.rs index 179a62b9b8..e0d8b4fb27 100644 --- a/crates/wast/src/core/types.rs +++ b/crates/wast/src/core/types.rs @@ -3,16 +3,20 @@ use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Id, Index, LParen, NameAnnotation, Span}; use std::mem; +use serde_derive::{Serialize, Deserialize}; /// The value types for a wasm module. #[allow(missing_docs)] #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum ValType<'a> { I32, I64, F32, F64, V128, + #[serde(borrow)] Ref(RefType<'a>), } @@ -59,6 +63,8 @@ impl<'a> Peek for ValType<'a> { /// A heap type for a reference type #[allow(missing_docs)] #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum HeapType<'a> { /// An untyped function reference: funcref. This is part of the reference /// types proposal. @@ -88,6 +94,7 @@ pub enum HeapType<'a> { None, /// A reference to a concrete function, struct, or array type defined by /// Wasm: `ref T`. This is part of the function references and GC proposals. + #[serde(borrow)] Concrete(Index<'a>), } @@ -158,8 +165,10 @@ impl<'a> Peek for HeapType<'a> { /// A reference type in a wasm module. #[allow(missing_docs)] #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] +#[derive(Serialize, Deserialize)] pub struct RefType<'a> { pub nullable: bool, + #[serde(borrow)] pub heap: HeapType<'a>, } @@ -338,9 +347,12 @@ impl<'a> Peek for RefType<'a> { /// The types of values that may be used in a struct or array. #[allow(missing_docs)] #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum StorageType<'a> { I8, I16, + #[serde(borrow)] Val(ValType<'a>), } @@ -363,8 +375,10 @@ impl<'a> Parse<'a> for StorageType<'a> { /// Type for a `global` in a wasm module #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Serialize, Deserialize)] pub struct GlobalType<'a> { /// The element type of this `global` + #[serde(borrow)] pub ty: ValType<'a>, /// Whether or not the global is mutable or not. pub mutable: bool, @@ -391,6 +405,7 @@ impl<'a> Parse<'a> for GlobalType<'a> { /// Min/max limits used for tables/memories. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Serialize, Deserialize)] pub struct Limits { /// The minimum number of units for this type. pub min: u32, @@ -412,6 +427,7 @@ impl<'a> Parse<'a> for Limits { /// Min/max limits used for 64-bit memories #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Serialize, Deserialize)] pub struct Limits64 { /// The minimum number of units for this type. pub min: u64, @@ -433,10 +449,12 @@ impl<'a> Parse<'a> for Limits64 { /// Configuration for a table of a wasm mdoule #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Serialize, Deserialize)] pub struct TableType<'a> { /// Limits on the element sizes of this table pub limits: Limits, /// The type of element stored in this table + #[serde(borrow)] pub elem: RefType<'a>, } @@ -451,6 +469,8 @@ impl<'a> Parse<'a> for TableType<'a> { /// Configuration for a memory of a wasm module #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum MemoryType { /// A 32-bit memory B32 { @@ -486,9 +506,11 @@ impl<'a> Parse<'a> for MemoryType { /// A function type with parameters and results. #[derive(Clone, Debug, Default)] +#[derive(Serialize, Deserialize)] pub struct FunctionType<'a> { /// The parameters of a function, optionally each having an identifier for /// name resolution and a name for the custom `name` section. + #[serde(borrow)] pub params: Box<[(Option>, Option>, ValType<'a>)]>, /// The results types of a function. pub results: Box<[ValType<'a>]>, @@ -601,8 +623,10 @@ impl<'a> From> for FunctionType<'a> { /// A struct type with fields. #[derive(Clone, Debug)] +#[derive(Serialize, Deserialize)] pub struct StructType<'a> { /// The fields of the struct + #[serde(borrow)] pub fields: Vec>, } @@ -630,8 +654,10 @@ impl<'a> Parse<'a> for StructType<'a> { /// A field of a struct type. #[derive(Clone, Debug)] +#[derive(Serialize, Deserialize)] pub struct StructField<'a> { /// An optional identifier for name resolution. + #[serde(borrow)] pub id: Option>, /// Whether this field may be mutated or not. pub mutable: bool, @@ -657,10 +683,12 @@ impl<'a> StructField<'a> { /// An array type with fields. #[derive(Clone, Debug)] +#[derive(Serialize, Deserialize)] pub struct ArrayType<'a> { /// Whether this field may be mutated or not. pub mutable: bool, /// The storage type stored in this field. + #[serde(borrow)] pub ty: StorageType<'a>, } @@ -701,8 +729,11 @@ impl<'a> Parse<'a> for ExportType<'a> { /// A definition of a type. #[derive(Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum TypeDef<'a> { /// A function type definition. + #[serde(borrow)] Func(FunctionType<'a>), /// A struct type definition. Struct(StructType<'a>), @@ -730,11 +761,13 @@ impl<'a> Parse<'a> for TypeDef<'a> { /// A type declaration in a module #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Type<'a> { /// Where this type was defined. pub span: Span, /// An optional identifier to refer to this `type` by as part of name /// resolution. + #[serde(borrow)] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -797,10 +830,12 @@ impl<'a> Parse<'a> for Type<'a> { /// A recursion group declaration in a module #[derive(Debug)] +#[derive(Serialize, Deserialize)] pub struct Rec<'a> { /// Where this recursion group was defined. pub span: Span, /// The types that we're defining in this group. + #[serde(borrow)] pub types: Vec>, } @@ -817,8 +852,10 @@ impl<'a> Parse<'a> for Rec<'a> { /// A reference to a type defined in this module. #[derive(Clone, Debug)] +#[derive(Serialize, Deserialize)] pub struct TypeUse<'a, T> { /// The type that we're referencing, if it was present. + #[serde(borrow)] pub index: Option>, /// The inline type, if present. pub inline: Option, diff --git a/crates/wast/src/lib.rs b/crates/wast/src/lib.rs index bb16574177..1fb9677242 100644 --- a/crates/wast/src/lib.rs +++ b/crates/wast/src/lib.rs @@ -96,6 +96,7 @@ macro_rules! custom_keyword { #[allow(non_camel_case_types)] #[allow(missing_docs)] #[derive(Debug, Copy, Clone)] + #[derive(Serialize, Deserialize)] pub struct $name(pub $crate::token::Span); impl<'a> $crate::parser::Parse<'a> for $name { @@ -373,6 +374,8 @@ id! { /// Common keyword used to parse WebAssembly text files. pub mod kw { + use serde_derive::{Serialize, Deserialize}; + custom_keyword!(after); custom_keyword!(alias); custom_keyword!(any); diff --git a/crates/wast/src/token.rs b/crates/wast/src/token.rs index 9dd15f97a8..82c7c3533f 100644 --- a/crates/wast/src/token.rs +++ b/crates/wast/src/token.rs @@ -8,9 +8,11 @@ use crate::{annotation, Error}; use std::fmt; use std::hash::{Hash, Hasher}; use std::str; +use serde_derive::{Serialize, Deserialize}; /// A position in the original source stream, used to render errors. #[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)] +#[derive(Serialize, Deserialize)] pub struct Span { pub(crate) offset: usize, } @@ -51,6 +53,7 @@ impl Span { /// An identifier is used to symbolically refer to items in a a wasm module, /// typically via the [`Index`] type. #[derive(Copy, Clone)] +#[derive(Serialize, Deserialize)] pub struct Id<'a> { name: &'a str, gen: u32, @@ -162,12 +165,15 @@ impl Peek for Id<'_> { /// The emission phase of a module will ensure that `Index::Id` is never used /// and switch them all to `Index::Num`. #[derive(Copy, Clone, Debug)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum Index<'a> { /// A numerical index that this references. The index space this is /// referencing is implicit based on where this [`Index`] is stored. Num(u32, Span), /// A human-readable identifier this references. Like `Num`, the namespace /// this references is based on where this is stored. + #[serde(borrow)] Id(Id<'a>), } @@ -276,6 +282,7 @@ impl<'a, K: Peek> Peek for ItemRef<'a, K> { /// An `@name` annotation in source, currently of the form `@name "foo"` #[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Serialize, Deserialize)] pub struct NameAnnotation<'a> { /// The name specified for the item pub name: &'a str, @@ -400,6 +407,7 @@ macro_rules! float { })*) => ($( /// A parsed floating-point type #[derive(Debug, Copy, Clone)] + #[derive(Serialize, Deserialize)] pub struct $name { /// The raw bits that this floating point number represents. pub bits: $int, diff --git a/crates/wast/src/wat.rs b/crates/wast/src/wat.rs index 6d9a233359..1d66422b1a 100644 --- a/crates/wast/src/wat.rs +++ b/crates/wast/src/wat.rs @@ -3,6 +3,7 @@ use crate::core::{Module, ModuleField, ModuleKind}; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::Span; +use serde_derive::{Serialize, Deserialize}; /// A `*.wat` file parser, or a parser for one parenthesized module. /// @@ -11,7 +12,10 @@ use crate::token::Span; /// of s-expressions that are module fields. #[derive(Debug)] #[allow(missing_docs)] +#[derive(Serialize, Deserialize)] +#[serde(tag = "type", content = "val")] pub enum Wat<'a> { + #[serde(borrow)] Module(Module<'a>), Component(Component<'a>), } From b12f5ea9c5cbd2c3630c80d627c7f13a129d3c13 Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Thu, 29 Feb 2024 20:01:51 -0800 Subject: [PATCH 2/7] add feature --- crates/wast/Cargo.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/wast/Cargo.toml b/crates/wast/Cargo.toml index c173450b54..2292ba4c0e 100644 --- a/crates/wast/Cargo.toml +++ b/crates/wast/Cargo.toml @@ -21,6 +21,9 @@ unicode-width = "0.1.9" memchr = "2.4.1" wasm-encoder = { workspace = true } bumpalo = "3.14.0" +serde_json = { workspace = true, optional = true } +serde = { workspace = true, optional = true } +serde_derive = { workspace = true, optional = true } [dev-dependencies] anyhow = { workspace = true } @@ -39,6 +42,9 @@ default = ['wasm-module'] # This feature is turned on by default. wasm-module = [] +# Includes support for serializing and deserializing the `Wat` type. +serde = ['dep:serde', 'dep:serde_derive', 'dep:serde_json'] + [[test]] name = "parse-fail" harness = false From 1d15fca87c3addbaa28d09b7d60acfbb8271dd0a Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Thu, 29 Feb 2024 20:33:49 -0800 Subject: [PATCH 3/7] gate bounds on feature --- crates/wast/src/component/binary.rs | 15 ++++++-- crates/wast/src/component/expand.rs | 10 ++++- crates/wast/src/component/func.rs | 5 ++- crates/wast/src/component/item_ref.rs | 54 +++++++++++++++++++++------ crates/wast/src/component/resolve.rs | 20 ++++++++-- crates/wast/src/component/types.rs | 30 ++++++++++++--- 6 files changed, 106 insertions(+), 28 deletions(-) diff --git a/crates/wast/src/component/binary.rs b/crates/wast/src/component/binary.rs index a4a9eefdc5..c9fccf612d 100644 --- a/crates/wast/src/component/binary.rs +++ b/crates/wast/src/component/binary.rs @@ -732,14 +732,20 @@ impl From> for u32 { } } -impl From<&ItemRef<'_, T>> for u32 { +impl< + #[cfg(feature = "serde")] T: SerializeT, + #[cfg(not(feature = "serde"))] T, +> From<&ItemRef<'_, T>> for u32 { fn from(i: &ItemRef<'_, T>) -> Self { assert!(i.export_names.is_empty()); i.idx.into() } } -impl From<&CoreTypeUse<'_, T>> for u32 { +impl< + #[cfg(feature = "serde")] T: SerializeT, + #[cfg(not(feature = "serde"))] T, +> From<&CoreTypeUse<'_, T>> for u32 { fn from(u: &CoreTypeUse<'_, T>) -> Self { match u { CoreTypeUse::Inline(_) => unreachable!("should be expanded already"), @@ -748,7 +754,10 @@ impl From<&CoreTypeUse<'_, T>> for u32 { } } -impl From<&ComponentTypeUse<'_, T>> for u32 { +impl< + #[cfg(feature = "serde")] T: SerializeT, + #[cfg(not(feature = "serde"))] T, +> From<&ComponentTypeUse<'_, T>> for u32 { fn from(u: &ComponentTypeUse<'_, T>) -> Self { match u { ComponentTypeUse::Inline(_) => unreachable!("should be expanded already"), diff --git a/crates/wast/src/component/expand.rs b/crates/wast/src/component/expand.rs index a729dca1e2..c260943612 100644 --- a/crates/wast/src/component/expand.rs +++ b/crates/wast/src/component/expand.rs @@ -600,7 +600,10 @@ impl<'a> Expander<'a> { *ty = ComponentValType::Ref(idx); } - fn expand_core_type_use( + fn expand_core_type_use< + #[cfg(feature = "serde")] T: SerializeT, + #[cfg(not(feature = "serde"))] T, + >( &mut self, item: &mut CoreTypeUse<'a, T>, ) -> CoreItemRef<'a, kw::r#type> @@ -653,7 +656,10 @@ impl<'a> Expander<'a> { ret } - fn expand_component_type_use( + fn expand_component_type_use< + #[cfg(feature = "serde")] T: SerializeT, + #[cfg(not(feature = "serde"))] T, + >( &mut self, item: &mut ComponentTypeUse<'a, T>, ) -> ItemRef<'a, kw::r#type> diff --git a/crates/wast/src/component/func.rs b/crates/wast/src/component/func.rs index a02ea8b635..04e5adcc81 100644 --- a/crates/wast/src/component/func.rs +++ b/crates/wast/src/component/func.rs @@ -497,7 +497,10 @@ impl<'a> Parse<'a> for CanonOpt<'a> { } } -fn parse_trailing_item_ref(kind: T, parser: Parser) -> Result> { +fn parse_trailing_item_ref< + #[cfg(feature = "serde")] T: SerializeT, + #[cfg(not(feature = "serde"))] T, +>(kind: T, parser: Parser) -> Result> { Ok(CoreItemRef { kind, idx: parser.parse()?, diff --git a/crates/wast/src/component/item_ref.rs b/crates/wast/src/component/item_ref.rs index 810f9e83a9..b75a4265cb 100644 --- a/crates/wast/src/component/item_ref.rs +++ b/crates/wast/src/component/item_ref.rs @@ -42,7 +42,10 @@ fn peek(cursor: Cursor) -> Result { /// Parses core item references. #[derive(Clone, Debug)] #[derive(Serialize, Deserialize)] -pub struct CoreItemRef<'a, K: SerializeT> { +pub struct CoreItemRef<'a, + #[cfg(feature = "serde")] K: SerializeT, + #[cfg(not(feature = "serde"))] K, +> { /// The item kind being parsed. pub kind: K, /// The item or instance reference. @@ -51,7 +54,10 @@ pub struct CoreItemRef<'a, K: SerializeT> { pub export_name: Option<&'a str>, } -impl<'a, K: Parse<'a> + SerializeT> Parse<'a> for CoreItemRef<'a, K> { +impl<'a, + #[cfg(feature = "serde")] K: Parse<'a> + SerializeT, + #[cfg(not(feature = "serde"))] K: Parse<'a>, +> Parse<'a> for CoreItemRef<'a, K> { fn parse(parser: Parser<'a>) -> Result { // This does not parse the surrounding `(` and `)` because // core prefix is context dependent and only the caller knows if it should be @@ -67,7 +73,10 @@ impl<'a, K: Parse<'a> + SerializeT> Parse<'a> for CoreItemRef<'a, K> { } } -impl<'a, K: Peek + SerializeT> Peek for CoreItemRef<'a, K> { +impl<'a, + #[cfg(feature = "serde")] K: Peek + SerializeT, + #[cfg(not(feature = "serde"))] K: Peek, +> Peek for CoreItemRef<'a, K> { fn peek(cursor: Cursor<'_>) -> Result { peek::(cursor) } @@ -80,7 +89,10 @@ impl<'a, K: Peek + SerializeT> Peek for CoreItemRef<'a, K> { /// Parses component item references. #[derive(Clone, Debug)] #[derive(Serialize, Deserialize)] -pub struct ItemRef<'a, K: SerializeT> { +pub struct ItemRef<'a, + #[cfg(feature = "serde")] K: SerializeT, + #[cfg(not(feature = "serde"))] K, +> { /// The item kind being parsed. pub kind: K, /// The item or instance reference. @@ -90,7 +102,10 @@ pub struct ItemRef<'a, K: SerializeT> { pub export_names: Vec<&'a str>, } -impl<'a, K: Parse<'a> + SerializeT> Parse<'a> for ItemRef<'a, K> { +impl<'a, + #[cfg(feature = "serde")] K: Parse<'a> + SerializeT, + #[cfg(not(feature = "serde"))] K: Parse<'a> +, +> Parse<'a> for ItemRef<'a, K> { fn parse(parser: Parser<'a>) -> Result { let kind = parser.parse::()?; let idx = parser.parse()?; @@ -106,7 +121,10 @@ impl<'a, K: Parse<'a> + SerializeT> Parse<'a> for ItemRef<'a, K> { } } -impl<'a, K: Peek + SerializeT> Peek for ItemRef<'a, K> { +impl<'a, + #[cfg(feature = "serde")] K: Peek + SerializeT, + #[cfg(not(feature = "serde"))] K: Peek, +> Peek for ItemRef<'a, K> { fn peek(cursor: Cursor<'_>) -> Result { peek::(cursor) } @@ -118,9 +136,15 @@ impl<'a, K: Peek + SerializeT> Peek for ItemRef<'a, K> { /// Convenience structure to parse `$f` or `(item $f)`. #[derive(Clone, Debug)] -pub struct IndexOrRef<'a, K: SerializeT>(pub ItemRef<'a, K>); - -impl<'a, K: SerializeT> Parse<'a> for IndexOrRef<'a, K> +pub struct IndexOrRef<'a, + #[cfg(feature = "serde")] K: SerializeT, + #[cfg(not(feature = "serde"))] K, +>(pub ItemRef<'a, K>); + +impl<'a, + #[cfg(feature = "serde")] K: SerializeT, + #[cfg(not(feature = "serde"))] K, +> Parse<'a> for IndexOrRef<'a, K> where K: Parse<'a> + Default, { @@ -139,9 +163,15 @@ where /// Convenience structure to parse `$f` or `(item $f)`. #[derive(Clone, Debug)] -pub struct IndexOrCoreRef<'a, K: SerializeT>(pub CoreItemRef<'a, K>); - -impl<'a, K: SerializeT> Parse<'a> for IndexOrCoreRef<'a, K> +pub struct IndexOrCoreRef<'a, + #[cfg(feature = "serde")] K: SerializeT, + #[cfg(not(feature = "serde"))] K, +>(pub CoreItemRef<'a, K>); + +impl<'a, + #[cfg(feature = "serde")] K: SerializeT, + #[cfg(not(feature = "serde"))] K, +> Parse<'a> for IndexOrCoreRef<'a, K> where K: Parse<'a> + Default, { diff --git a/crates/wast/src/component/resolve.rs b/crates/wast/src/component/resolve.rs index 9873362248..90ef0f7b0d 100644 --- a/crates/wast/src/component/resolve.rs +++ b/crates/wast/src/component/resolve.rs @@ -392,7 +392,10 @@ impl<'a> Resolver<'a> { Ok(()) } - fn core_type_use(&mut self, ty: &mut CoreTypeUse<'a, T>) -> Result<(), Error> { + fn core_type_use< + #[cfg(feature = "serde")] T: SerializeT, + #[cfg(not(feature = "serde"))] T, + >(&mut self, ty: &mut CoreTypeUse<'a, T>) -> Result<(), Error> { let item = match ty { CoreTypeUse::Ref(r) => r, CoreTypeUse::Inline(_) => { @@ -402,7 +405,10 @@ impl<'a> Resolver<'a> { self.core_item_ref(item) } - fn component_type_use(&mut self, ty: &mut ComponentTypeUse<'a, T>) -> Result<(), Error> { + fn component_type_use< + #[cfg(feature = "serde")] T: SerializeT, + #[cfg(not(feature = "serde"))] T, + >(&mut self, ty: &mut ComponentTypeUse<'a, T>) -> Result<(), Error> { let item = match ty { ComponentTypeUse::Ref(r) => r, ComponentTypeUse::Inline(_) => { @@ -608,7 +614,10 @@ impl<'a> Resolver<'a> { ) } - fn core_item_ref(&mut self, item: &mut CoreItemRef<'a, K>) -> Result<(), Error> + fn core_item_ref< + #[cfg(feature = "serde")] K: SerializeT, + #[cfg(not(feature = "serde"))] K, + >(&mut self, item: &mut CoreItemRef<'a, K>) -> Result<(), Error> where K: CoreItem + Copy, { @@ -644,7 +653,10 @@ impl<'a> Resolver<'a> { Ok(()) } - fn component_item_ref(&mut self, item: &mut ItemRef<'a, K>) -> Result<(), Error> + fn component_item_ref< + #[cfg(feature = "serde")] K: SerializeT, + #[cfg(not(feature = "serde"))] K, + >(&mut self, item: &mut ItemRef<'a, K>) -> Result<(), Error> where K: ComponentItem + Copy, { diff --git a/crates/wast/src/component/types.rs b/crates/wast/src/component/types.rs index 73f99d5faa..8f85c4b31f 100644 --- a/crates/wast/src/component/types.rs +++ b/crates/wast/src/component/types.rs @@ -1004,7 +1004,10 @@ impl<'a> Parse<'a> for ComponentValTypeUse<'a> { #[derive(Debug, Clone)] #[derive(Serialize, Deserialize)] #[serde(tag = "type", content = "val")] -pub enum CoreTypeUse<'a, T: SerializeT> { +pub enum CoreTypeUse<'a, + #[cfg(feature = "serde")] T: SerializeT, + #[cfg(not(feature = "serde"))] T, +> { /// The type that we're referencing. #[serde(borrow)] Ref(CoreItemRef<'a, kw::r#type>), @@ -1012,7 +1015,10 @@ pub enum CoreTypeUse<'a, T: SerializeT> { Inline(T), } -impl<'a, T: Parse<'a> + SerializeT> Parse<'a> for CoreTypeUse<'a, T> { +impl<'a, + #[cfg(feature = "serde")] T: Parse<'a> + SerializeT, + #[cfg(not(feature = "serde"))] T: Parse<'a>, +> Parse<'a> for CoreTypeUse<'a, T> { fn parse(parser: Parser<'a>) -> Result { // Here the core context is assumed, so no core prefix is expected if parser.peek::()? && parser.peek2::>()? { @@ -1023,7 +1029,10 @@ impl<'a, T: Parse<'a> + SerializeT> Parse<'a> for CoreTypeUse<'a, T> { } } -impl Default for CoreTypeUse<'_, T> { +impl< + #[cfg(feature = "serde")] T: SerializeT, + #[cfg(not(feature = "serde"))] T, +> Default for CoreTypeUse<'_, T> { fn default() -> Self { let span = Span::from_offset(0); Self::Ref(CoreItemRef { @@ -1041,7 +1050,10 @@ impl Default for CoreTypeUse<'_, T> { #[derive(Debug, Clone)] #[derive(Serialize, Deserialize)] #[serde(tag = "type", content = "val")] -pub enum ComponentTypeUse<'a, T: SerializeT> { +pub enum ComponentTypeUse<'a, + #[cfg(feature = "serde")] T: SerializeT, + #[cfg(not(feature = "serde"))] T, +> { /// The type that we're referencing. #[serde(borrow)] Ref(ItemRef<'a, kw::r#type>), @@ -1049,7 +1061,10 @@ pub enum ComponentTypeUse<'a, T: SerializeT> { Inline(T), } -impl<'a, T: Parse<'a> + SerializeT> Parse<'a> for ComponentTypeUse<'a, T> { +impl<'a, + #[cfg(feature = "serde")] T: Parse<'a> + SerializeT, + #[cfg(not(feature = "serde"))] T: Parse<'a>, +> Parse<'a> for ComponentTypeUse<'a, T> { fn parse(parser: Parser<'a>) -> Result { if parser.peek::()? && parser.peek2::>()? { Ok(Self::Ref(parser.parens(|parser| parser.parse())?)) @@ -1059,7 +1074,10 @@ impl<'a, T: Parse<'a> + SerializeT> Parse<'a> for ComponentTypeUse<'a, T> { } } -impl Default for ComponentTypeUse<'_, T> { +impl< + #[cfg(feature = "serde")] T: SerializeT, + #[cfg(not(feature = "serde"))] T, +> Default for ComponentTypeUse<'_, T> { fn default() -> Self { let span = Span::from_offset(0); Self::Ref(ItemRef { From 8ba3689ffc68d86fdb2fc7f03eeaba91831e8320 Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Thu, 29 Feb 2024 20:35:12 -0800 Subject: [PATCH 4/7] gate attributes on feature --- crates/wast/src/component/alias.rs | 16 ++- crates/wast/src/component/binary.rs | 1 + crates/wast/src/component/component.rs | 28 +++--- crates/wast/src/component/custom.rs | 3 +- crates/wast/src/component/expand.rs | 1 + crates/wast/src/component/export.rs | 14 +-- crates/wast/src/component/func.rs | 54 +++++----- crates/wast/src/component/import.rs | 29 +++--- crates/wast/src/component/instance.rs | 35 +++---- crates/wast/src/component/item_ref.rs | 8 +- crates/wast/src/component/module.rs | 10 +- crates/wast/src/component/resolve.rs | 1 + crates/wast/src/component/types.rs | 133 ++++++++++++------------- crates/wast/src/core/custom.rs | 27 +++-- crates/wast/src/core/export.rs | 10 +- crates/wast/src/core/expr.rs | 132 ++++++++++++------------ crates/wast/src/core/func.rs | 14 +-- crates/wast/src/core/global.rs | 10 +- crates/wast/src/core/import.rs | 14 +-- crates/wast/src/core/memory.rs | 22 ++-- crates/wast/src/core/module.rs | 15 ++- crates/wast/src/core/table.rs | 24 ++--- crates/wast/src/core/tag.rs | 15 ++- crates/wast/src/core/types.rs | 68 ++++++------- crates/wast/src/lib.rs | 5 +- crates/wast/src/token.rs | 14 +-- crates/wast/src/wat.rs | 6 +- 27 files changed, 341 insertions(+), 368 deletions(-) diff --git a/crates/wast/src/component/alias.rs b/crates/wast/src/component/alias.rs index 50e2ac83ed..46bc140cf7 100644 --- a/crates/wast/src/component/alias.rs +++ b/crates/wast/src/component/alias.rs @@ -2,13 +2,14 @@ use crate::core::ExportKind; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, Index, NameAnnotation, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A inline alias for component exported items. /// /// Handles both `core export` and `export` aliases #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct InlineExportAlias<'a, const CORE: bool> { /// The instance to alias the export from. pub instance: Index<'a>, @@ -31,13 +32,13 @@ impl<'a, const CORE: bool> Parse<'a> for InlineExportAlias<'a, CORE> { /// An alias to a component item. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Alias<'a> { /// Where this `alias` was defined. pub span: Span, /// An identifier that this alias is resolved with (optionally) for name /// resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this alias stored in the custom `name` section. pub name: Option>, @@ -141,8 +142,7 @@ impl<'a> Parse<'a> for Alias<'a> { /// Represents the kind of instance export alias. #[derive(Debug, Copy, Clone, PartialEq, Eq)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ComponentExportAliasKind { /// The alias is to a core module export. CoreModule, @@ -193,8 +193,7 @@ impl<'a> Parse<'a> for ComponentExportAliasKind { /// Represents the kind of outer alias. #[derive(Debug, Copy, Clone, PartialEq, Eq)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ComponentOuterAliasKind { /// The alias is to an outer core module. CoreModule, @@ -235,8 +234,7 @@ impl<'a> Parse<'a> for ComponentOuterAliasKind { /// The target of a component alias. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum AliasTarget<'a> { /// The alias is to an export of a component instance. Export { diff --git a/crates/wast/src/component/binary.rs b/crates/wast/src/component/binary.rs index c9fccf612d..ce44a23e85 100644 --- a/crates/wast/src/component/binary.rs +++ b/crates/wast/src/component/binary.rs @@ -8,6 +8,7 @@ use wasm_encoder::{ ComponentTypeSection, CoreTypeEncoder, CoreTypeSection, InstanceSection, NameMap, NestedComponentSection, RawSection, SectionId, }; +#[cfg(feature = "serde")] use serde::Serialize as SerializeT; pub fn encode(component: &Component<'_>) -> Vec { diff --git a/crates/wast/src/component/component.rs b/crates/wast/src/component/component.rs index 36884f32e9..64bd75f87f 100644 --- a/crates/wast/src/component/component.rs +++ b/crates/wast/src/component/component.rs @@ -5,16 +5,17 @@ use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::Index; use crate::token::{Id, NameAnnotation, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A parsed WebAssembly component module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Component<'a> { /// Where this `component` was defined pub span: Span, /// An optional identifier this component is known by - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional `@name` annotation for this component pub name: Option>, @@ -24,11 +25,10 @@ pub struct Component<'a> { /// The different kinds of ways to define a component. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ComponentKind<'a> { /// A component defined in the textual s-expression format. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Text(Vec>), /// A component that had its raw binary bytes defined via the `binary` /// directive. @@ -145,10 +145,9 @@ impl<'a> Parse<'a> for Component<'a> { /// A listing of all possible fields that can make up a WebAssembly component. #[allow(missing_docs)] #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ComponentField<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] CoreModule(CoreModule<'a>), CoreInstance(CoreInstance<'a>), CoreType(CoreType<'a>), @@ -229,10 +228,10 @@ impl<'a> Parse<'a> for ComponentField<'a> { /// A function to call at instantiation time. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Start<'a> { /// The function to call. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub func: Index<'a>, /// The arguments to pass to the function. pub args: Vec>, @@ -270,12 +269,12 @@ impl<'a> Parse<'a> for Start<'a> { /// A nested WebAssembly component. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct NestedComponent<'a> { /// Where this `component` was defined pub span: Span, /// An optional identifier this component is known by - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional `@name` annotation for this component pub name: Option>, @@ -288,13 +287,12 @@ pub struct NestedComponent<'a> { /// The different kinds of ways to define a nested component. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum NestedComponentKind<'a> { /// This is actually an inline import of a component Import { /// The information about where this is being imported from. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] import: InlineImport<'a>, /// The type of component being imported. ty: ComponentTypeUse<'a, ComponentType<'a>>, diff --git a/crates/wast/src/component/custom.rs b/crates/wast/src/component/custom.rs index a63bafcd4a..708e5421b9 100644 --- a/crates/wast/src/component/custom.rs +++ b/crates/wast/src/component/custom.rs @@ -1,11 +1,12 @@ use crate::annotation; use crate::parser::{Parse, Parser, Result}; use crate::token::Span; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A custom section within a component. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Custom<'a> { /// Where this `@custom` was defined. pub span: Span, diff --git a/crates/wast/src/component/expand.rs b/crates/wast/src/component/expand.rs index c260943612..e94e06fffa 100644 --- a/crates/wast/src/component/expand.rs +++ b/crates/wast/src/component/expand.rs @@ -6,6 +6,7 @@ use crate::token::Id; use crate::token::{Index, Span}; use std::collections::HashMap; use std::mem; +#[cfg(feature = "serde")] use serde::Serialize as SerializeT; /// Performs an AST "expansion" pass over the component fields provided. diff --git a/crates/wast/src/component/export.rs b/crates/wast/src/component/export.rs index e165577756..cf97bacebb 100644 --- a/crates/wast/src/component/export.rs +++ b/crates/wast/src/component/export.rs @@ -2,16 +2,17 @@ use super::{ComponentExternName, ItemRef, ItemSigNoName}; use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Id, Index, NameAnnotation, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// An entry in a WebAssembly component's export section. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ComponentExport<'a> { /// Where this export was defined. pub span: Span, /// Optional identifier bound to this export. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this instance stored in the custom `name` section. pub debug_name: Option>, @@ -58,14 +59,13 @@ impl<'a> Parse<'a> for Vec> { /// The kind of exported item. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ComponentExportKind<'a> { /// The export is a core module. /// /// Note this isn't a core item ref as currently only /// components can export core modules. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] CoreModule(ItemRef<'a, kw::module>), /// The export is a function. Func(ItemRef<'a, kw::func>), @@ -177,10 +177,10 @@ impl Peek for ComponentExportKind<'_> { /// A listing of inline `(export "foo" )` statements on a WebAssembly /// component item in its textual format. #[derive(Debug, Default)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct InlineExport<'a> { /// The extra names to export an item as, if any. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub names: Vec>, } diff --git a/crates/wast/src/component/func.rs b/crates/wast/src/component/func.rs index 04e5adcc81..9b7e6b4cb9 100644 --- a/crates/wast/src/component/func.rs +++ b/crates/wast/src/component/func.rs @@ -2,20 +2,22 @@ use crate::component::*; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, Index, LParen, NameAnnotation, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; +#[cfg(feature = "serde")] use serde::Serialize as SerializeT; /// A declared core function. /// /// This is a member of both the core alias and canon sections. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CoreFunc<'a> { /// Where this `core func` was defined. pub span: Span, /// An identifier that this function is resolved with (optionally) for name /// resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -43,13 +45,12 @@ impl<'a> Parse<'a> for CoreFunc<'a> { /// Represents the kind of core functions. #[derive(Debug)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum CoreFuncKind<'a> { /// The core function is defined in terms of lowering a component function. /// /// The core function is actually a member of the canon section. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Lower(CanonLower<'a>), /// The core function is defined in terms of aliasing a module instance export. /// @@ -91,13 +92,13 @@ impl<'a> Parse<'a> for CoreFuncKind<'a> { /// /// This may be a member of the import, alias, or canon sections. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Func<'a> { /// Where this `func` was defined. pub span: Span, /// An identifier that this function is resolved with (optionally) for name /// resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -128,8 +129,7 @@ impl<'a> Parse<'a> for Func<'a> { /// Represents the kind of component functions. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum FuncKind<'a> { /// A function which is actually defined as an import, such as: /// @@ -138,7 +138,7 @@ pub enum FuncKind<'a> { /// ``` Import { /// The import name of this import. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] import: InlineImport<'a>, /// The type that this function will have. ty: ComponentTypeUse<'a, ComponentFunctionType<'a>>, @@ -183,13 +183,13 @@ impl<'a> Parse<'a> for FuncKind<'a> { /// /// This is a member of the canonical section. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CanonicalFunc<'a> { /// Where this `func` was defined. pub span: Span, /// An identifier that this function is resolved with (optionally) for name /// resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -261,13 +261,12 @@ impl<'a> CanonicalFunc<'a> { /// Possible ways to define a canonical function in the text format. #[derive(Debug)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum CanonicalFuncKind<'a> { /// A canonical function that is defined in terms of lifting a core function. Lift { /// The lifted function's type. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] ty: ComponentTypeUse<'a, ComponentFunctionType<'a>>, /// Information relating to the lifting of the core function. info: CanonLift<'a>, @@ -282,10 +281,10 @@ pub enum CanonicalFuncKind<'a> { /// Information relating to lifting a core function. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CanonLift<'a> { /// The core function being lifted. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub func: CoreItemRef<'a, kw::func>, /// The canonical options for the lifting. pub opts: Vec>, @@ -321,10 +320,10 @@ impl Default for CanonLift<'_> { /// Information relating to lowering a component function. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CanonLower<'a> { /// The function being lowered. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub func: ItemRef<'a, kw::func>, /// The canonical options for the lowering. pub opts: Vec>, @@ -357,10 +356,10 @@ impl Default for CanonLower<'_> { /// Information relating to the `resource.new` intrinsic. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CanonResourceNew<'a> { /// The resource type that this intrinsic creates an owned reference to. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub ty: Index<'a>, } @@ -384,10 +383,10 @@ impl Default for CanonResourceNew<'_> { /// Information relating to the `resource.drop` intrinsic. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CanonResourceDrop<'a> { /// The resource type that this intrinsic is dropping. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub ty: Index<'a>, } @@ -411,10 +410,10 @@ impl Default for CanonResourceDrop<'_> { /// Information relating to the `resource.rep` intrinsic. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CanonResourceRep<'a> { /// The resource type that this intrinsic is accessing. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub ty: Index<'a>, } @@ -438,8 +437,7 @@ impl Default for CanonResourceRep<'_> { #[derive(Debug)] /// Canonical ABI options. -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum CanonOpt<'a> { /// Encode strings as UTF-8. StringUtf8, @@ -448,7 +446,7 @@ pub enum CanonOpt<'a> { /// Encode strings as "compact UTF-16". StringLatin1Utf16, /// Use the specified memory for canonical ABI memory access. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Memory(CoreItemRef<'a, kw::memory>), /// Use the specified reallocation function for memory allocations. Realloc(CoreItemRef<'a, kw::func>), diff --git a/crates/wast/src/component/import.rs b/crates/wast/src/component/import.rs index f8d4eddd8f..ce116174c3 100644 --- a/crates/wast/src/component/import.rs +++ b/crates/wast/src/component/import.rs @@ -2,16 +2,17 @@ use crate::component::*; use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Id, Index, LParen, NameAnnotation, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// An `import` statement and entry in a WebAssembly component. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ComponentImport<'a> { /// Where this `import` was defined pub span: Span, /// The name of the item being imported. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub name: ComponentExternName<'a>, /// The item that's being imported. pub item: ItemSig<'a>, @@ -28,7 +29,7 @@ impl<'a> Parse<'a> for ComponentImport<'a> { /// The different ways an import can be named. #[derive(Debug, Copy, Clone)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ComponentExternName<'a>(pub &'a str); impl<'a> Parse<'a> for ComponentExternName<'a> { @@ -53,13 +54,13 @@ impl<'a> Parse<'a> for ComponentExternName<'a> { /// An item signature for imported items. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ItemSig<'a> { /// Where this item is defined in the source. pub span: Span, /// An optional identifier used during name resolution to refer to this item /// from the rest of the component. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name which, for functions, will be stored in the /// custom `name` section. @@ -76,9 +77,9 @@ impl<'a> Parse<'a> for ItemSig<'a> { /// An item signature for imported items. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ItemSigNoName<'a>( - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub ItemSig<'a> ); @@ -124,11 +125,10 @@ fn parse_item_sig<'a>(parser: Parser<'a>, name: bool) -> Result> { /// The kind of signatures for imported items. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ItemSigKind<'a> { /// The item signature is for a core module. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] CoreModule(CoreTypeUse<'a, ModuleType<'a>>), /// The item signature is for a function. Func(ComponentTypeUse<'a, ComponentFunctionType<'a>>), @@ -144,11 +144,10 @@ pub enum ItemSigKind<'a> { /// Represents the bounds applied to types being imported. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum TypeBounds<'a> { /// The equality type bounds. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Eq(Index<'a>), /// A resource type is imported/exported, SubResource, @@ -175,10 +174,10 @@ impl<'a> Parse<'a> for TypeBounds<'a> { /// This is the same as `core::InlineImport` except only one string import is /// required. #[derive(Debug, Clone)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct InlineImport<'a> { /// The name of the item being imported. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub name: ComponentExternName<'a>, } diff --git a/crates/wast/src/component/instance.rs b/crates/wast/src/component/instance.rs index 6d907005b3..8a7a564ac6 100644 --- a/crates/wast/src/component/instance.rs +++ b/crates/wast/src/component/instance.rs @@ -3,17 +3,18 @@ use crate::core; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, LParen, NameAnnotation, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A core instance defined by instantiation or exporting core items. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CoreInstance<'a> { /// Where this `core instance` was defined. pub span: Span, /// An identifier that this instance is resolved with (optionally) for name /// resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this instance stored in the custom `name` section. pub name: Option>, @@ -40,13 +41,12 @@ impl<'a> Parse<'a> for CoreInstance<'a> { /// The kinds of core instances in the text format. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum CoreInstanceKind<'a> { /// Instantiate a core module. Instantiate { /// The module being instantiated. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] module: ItemRef<'a, kw::module>, /// Arguments used to instantiate the instance. args: Vec>, @@ -79,7 +79,7 @@ impl Default for kw::module { /// An argument to instantiate a core module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CoreInstantiationArg<'a> { /// The name of the instantiation argument. pub name: &'a str, @@ -109,11 +109,10 @@ impl<'a> Parse<'a> for Vec> { /// The kind of core instantiation argument. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum CoreInstantiationArgKind<'a> { /// The argument is a reference to an instance. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Instance(CoreItemRef<'a, kw::instance>), /// The argument is an instance created from local exported core items. /// @@ -137,7 +136,7 @@ impl<'a> Parse<'a> for CoreInstantiationArgKind<'a> { /// An exported item as part of a core instance. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CoreInstanceExport<'a> { /// Where this export was defined. pub span: Span, @@ -169,13 +168,13 @@ impl<'a> Parse<'a> for Vec> { /// A component instance defined by instantiation or exporting items. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Instance<'a> { /// Where this `instance` was defined. pub span: Span, /// An identifier that this instance is resolved with (optionally) for name /// resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this instance stored in the custom `name` section. pub name: Option>, @@ -206,13 +205,12 @@ impl<'a> Parse<'a> for Instance<'a> { /// The kinds of instances in the text format. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum InstanceKind<'a> { /// The `(instance (import "x"))` sugar syntax Import { /// The name of the import - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] import: InlineImport<'a>, /// The type of the instance being imported ty: ComponentTypeUse<'a, InstanceType<'a>>, @@ -259,7 +257,7 @@ impl Default for kw::component { /// An argument to instantiate a component. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct InstantiationArg<'a> { /// The name of the instantiation argument. pub name: &'a str, @@ -289,11 +287,10 @@ impl<'a> Parse<'a> for Vec> { /// The kind of instantiation argument. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum InstantiationArgKind<'a> { /// The argument is a reference to a component item. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Item(ComponentExportKind<'a>), /// The argument is an instance created from local exported items. /// diff --git a/crates/wast/src/component/item_ref.rs b/crates/wast/src/component/item_ref.rs index b75a4265cb..94f0ad1af5 100644 --- a/crates/wast/src/component/item_ref.rs +++ b/crates/wast/src/component/item_ref.rs @@ -1,6 +1,8 @@ use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::Index; +#[cfg(feature = "serde")] use serde::Serialize as SerializeT; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; fn peek(cursor: Cursor) -> Result { @@ -41,7 +43,7 @@ fn peek(cursor: Cursor) -> Result { /// Parses core item references. #[derive(Clone, Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CoreItemRef<'a, #[cfg(feature = "serde")] K: SerializeT, #[cfg(not(feature = "serde"))] K, @@ -88,7 +90,7 @@ impl<'a, /// Parses component item references. #[derive(Clone, Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ItemRef<'a, #[cfg(feature = "serde")] K: SerializeT, #[cfg(not(feature = "serde"))] K, @@ -96,7 +98,7 @@ pub struct ItemRef<'a, /// The item kind being parsed. pub kind: K, /// The item or instance reference. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub idx: Index<'a>, /// Export names to resolve the item from. pub export_names: Vec<&'a str>, diff --git a/crates/wast/src/component/module.rs b/crates/wast/src/component/module.rs index 77d8dc2730..d5f40cd2ff 100644 --- a/crates/wast/src/component/module.rs +++ b/crates/wast/src/component/module.rs @@ -3,19 +3,20 @@ use crate::core; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, NameAnnotation, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A core WebAssembly module to be created as part of a component. /// /// This is a member of the core module section. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CoreModule<'a> { /// Where this `core module` was defined. pub span: Span, /// An identifier that this module is resolved with (optionally) for name /// resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this module stored in the custom `name` section. pub name: Option>, @@ -28,13 +29,12 @@ pub struct CoreModule<'a> { /// Possible ways to define a core module in the text format. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum CoreModuleKind<'a> { /// A core module which is actually defined as an import Import { /// Where this core module is imported from - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] import: InlineImport<'a>, /// The type that this core module will have. ty: CoreTypeUse<'a, ModuleType<'a>>, diff --git a/crates/wast/src/component/resolve.rs b/crates/wast/src/component/resolve.rs index 90ef0f7b0d..3d1fed0848 100644 --- a/crates/wast/src/component/resolve.rs +++ b/crates/wast/src/component/resolve.rs @@ -5,6 +5,7 @@ use crate::names::Namespace; use crate::token::Span; use crate::token::{Id, Index}; use crate::Error; +#[cfg(feature = "serde")] use serde::Serialize as SerializeT; /// Resolve the fields of a component and everything nested within it, changing diff --git a/crates/wast/src/component/types.rs b/crates/wast/src/component/types.rs index 8f85c4b31f..0a71a13534 100644 --- a/crates/wast/src/component/types.rs +++ b/crates/wast/src/component/types.rs @@ -7,18 +7,20 @@ use crate::parser::{Parse, Parser, Result}; use crate::token::Index; use crate::token::LParen; use crate::token::{Id, NameAnnotation, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; +#[cfg(feature = "serde")] use serde::Serialize as SerializeT; /// A core type declaration. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CoreType<'a> { /// Where this type was defined. pub span: Span, /// An optional identifier to refer to this `core type` by as part of name /// resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this type stored in the custom `name` section. pub name: Option>, @@ -48,11 +50,10 @@ impl<'a> Parse<'a> for CoreType<'a> { /// In the future this may be removed when module types are a part of /// a core module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum CoreTypeDef<'a> { /// The type definition is one of the core types. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Def(core::TypeDef<'a>), /// The type definition is a module type. Module(ModuleType<'a>), @@ -71,10 +72,10 @@ impl<'a> Parse<'a> for CoreTypeDef<'a> { /// A type definition for a core module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ModuleType<'a> { /// The declarations of the module type. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub decls: Vec>, } @@ -89,8 +90,7 @@ impl<'a> Parse<'a> for ModuleType<'a> { /// The declarations of a [`ModuleType`]. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ModuleTypeDecl<'a> { /// A core type. Type(core::Type<'a>), @@ -134,13 +134,13 @@ impl<'a> Parse<'a> for Vec> { /// A type declaration in a component. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Type<'a> { /// Where this type was defined. pub span: Span, /// An optional identifier to refer to this `type` by as part of name /// resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this type stored in the custom `name` section. pub name: Option>, @@ -185,11 +185,10 @@ impl<'a> Type<'a> { /// A definition of a component type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum TypeDef<'a> { /// A defined value type. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Defined(ComponentDefinedType<'a>), /// A component function type. Func(ComponentFunctionType<'a>), @@ -236,8 +235,7 @@ impl<'a> Parse<'a> for TypeDef<'a> { /// A primitive value type. #[allow(missing_docs)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum PrimitiveValType { Bool, S8, @@ -330,11 +328,10 @@ impl Peek for PrimitiveValType { /// A component value type. #[allow(missing_docs)] #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ComponentValType<'a> { /// The value type is an inline defined type. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Inline(ComponentDefinedType<'a>), /// The value type is an index reference to a defined type. Ref(Index<'a>), @@ -365,9 +362,9 @@ impl Peek for ComponentValType<'_> { /// This variation does not parse type indexes. #[allow(missing_docs)] #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct InlineComponentValType<'a>( - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] ComponentDefinedType<'a> ); @@ -389,11 +386,10 @@ impl<'a> Parse<'a> for InlineComponentValType<'a> { // A component defined type. #[allow(missing_docs)] #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ComponentDefinedType<'a> { Primitive(PrimitiveValType), - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Record(Record<'a>), Variant(Variant<'a>), List(List<'a>), @@ -474,10 +470,10 @@ impl Peek for ComponentDefinedType<'_> { /// A record defined type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Record<'a> { /// The fields of the record. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub fields: Vec>, } @@ -494,7 +490,7 @@ impl<'a> Parse<'a> for Record<'a> { /// A record type field. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct RecordField<'a> { /// The name of the field. pub name: &'a str, @@ -514,10 +510,10 @@ impl<'a> Parse<'a> for RecordField<'a> { /// A variant defined type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Variant<'a> { /// The cases of the variant type. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub cases: Vec>, } @@ -534,7 +530,7 @@ impl<'a> Parse<'a> for Variant<'a> { /// A case of a variant type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct VariantCase<'a> { /// Where this `case` was defined pub span: Span, @@ -572,13 +568,12 @@ impl<'a> Parse<'a> for VariantCase<'a> { /// A refinement for a variant case. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum Refinement<'a> { /// The refinement is referenced by index. Index( Span, - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Index<'a> ), /// The refinement has been resolved to an index into @@ -598,10 +593,10 @@ impl<'a> Parse<'a> for Refinement<'a> { /// A list type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct List<'a> { /// The element type of the array. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub element: Box>, } @@ -616,10 +611,10 @@ impl<'a> Parse<'a> for List<'a> { /// A tuple type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Tuple<'a> { /// The types of the fields of the tuple. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub fields: Vec>, } @@ -636,10 +631,10 @@ impl<'a> Parse<'a> for Tuple<'a> { /// A flags type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Flags<'a> { /// The names of the individual flags. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub names: Vec<&'a str>, } @@ -656,10 +651,10 @@ impl<'a> Parse<'a> for Flags<'a> { /// An enum type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Enum<'a> { /// The tag names of the enum. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub names: Vec<&'a str>, } @@ -676,10 +671,10 @@ impl<'a> Parse<'a> for Enum<'a> { /// An optional type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct OptionType<'a> { /// The type of the value, when a value is present. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub element: Box>, } @@ -694,10 +689,10 @@ impl<'a> Parse<'a> for OptionType<'a> { /// A result type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ResultType<'a> { /// The type on success. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub ok: Option>>, /// The type on failure. pub err: Option>>, @@ -726,11 +721,11 @@ impl<'a> Parse<'a> for ResultType<'a> { /// A component function type with parameters and result. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ComponentFunctionType<'a> { /// The parameters of a function, optionally each having an identifier for /// name resolution and a name for the custom `name` section. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub params: Box<[ComponentFunctionParam<'a>]>, /// The result of a function, optionally each having an identifier for /// name resolution and a name for the custom `name` section. @@ -758,7 +753,7 @@ impl<'a> Parse<'a> for ComponentFunctionType<'a> { /// A parameter of a [`ComponentFunctionType`]. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ComponentFunctionParam<'a> { /// The name of the parameter pub name: &'a str, @@ -778,7 +773,7 @@ impl<'a> Parse<'a> for ComponentFunctionParam<'a> { /// A result of a [`ComponentFunctionType`]. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ComponentFunctionResult<'a> { /// An optionally-specified name of this result pub name: Option<&'a str>, @@ -798,12 +793,12 @@ impl<'a> Parse<'a> for ComponentFunctionResult<'a> { /// The type of an exported item from an component or instance type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ComponentExportType<'a> { /// Where this export was defined. pub span: Span, /// The name of this export. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub name: ComponentExternName<'a>, /// The signature of the item. pub item: ItemSig<'a>, @@ -827,10 +822,10 @@ impl<'a> Parse<'a> for ComponentExportType<'a> { /// A type definition for a component type. #[derive(Debug, Default)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ComponentType<'a> { /// The declarations of the component type. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub decls: Vec>, } @@ -845,11 +840,10 @@ impl<'a> Parse<'a> for ComponentType<'a> { /// A declaration of a component type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ComponentTypeDecl<'a> { /// A core type definition local to the component type. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] CoreType(CoreType<'a>), /// A type definition local to the component type. Type(Type<'a>), @@ -892,10 +886,10 @@ impl<'a> Parse<'a> for Vec> { /// A type definition for an instance type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct InstanceType<'a> { /// The declarations of the instance type. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub decls: Vec>, } @@ -910,11 +904,10 @@ impl<'a> Parse<'a> for InstanceType<'a> { /// A declaration of an instance type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum InstanceTypeDecl<'a> { /// A core type definition local to the component type. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] CoreType(CoreType<'a>), /// A type definition local to the instance type. Type(Type<'a>), @@ -953,10 +946,10 @@ impl<'a> Parse<'a> for Vec> { /// A type definition for an instance type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ResourceType<'a> { /// Representation, in core WebAssembly, of this resource. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub rep: core::ValType<'a>, /// The declarations of the instance type. pub dtor: Option>, @@ -982,9 +975,9 @@ impl<'a> Parse<'a> for ResourceType<'a> { /// A value type declaration used for values in import signatures. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ComponentValTypeUse<'a>( - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub ComponentValType<'a> ); @@ -1002,14 +995,13 @@ impl<'a> Parse<'a> for ComponentValTypeUse<'a> { /// This is the same as `TypeUse`, but accepts `$T` as shorthand for /// `(type $T)`. #[derive(Debug, Clone)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum CoreTypeUse<'a, #[cfg(feature = "serde")] T: SerializeT, #[cfg(not(feature = "serde"))] T, > { /// The type that we're referencing. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Ref(CoreItemRef<'a, kw::r#type>), /// The inline type. Inline(T), @@ -1048,14 +1040,13 @@ impl< /// This is the same as `TypeUse`, but accepts `$T` as shorthand for /// `(type $T)`. #[derive(Debug, Clone)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ComponentTypeUse<'a, #[cfg(feature = "serde")] T: SerializeT, #[cfg(not(feature = "serde"))] T, > { /// The type that we're referencing. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Ref(ItemRef<'a, kw::r#type>), /// The inline type. Inline(T), diff --git a/crates/wast/src/core/custom.rs b/crates/wast/src/core/custom.rs index 640f690033..f28dcee30c 100644 --- a/crates/wast/src/core/custom.rs +++ b/crates/wast/src/core/custom.rs @@ -1,15 +1,15 @@ use crate::parser::{Parse, Parser, Result}; use crate::token::{self, Span}; use crate::{annotation, kw}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A custom section within a wasm module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum Custom<'a> { /// A raw custom section with the manual placement and bytes specified. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Raw(RawCustomSection<'a>), /// A producers custom section. Producers(Producers<'a>), @@ -51,7 +51,7 @@ impl<'a> Parse<'a> for Custom<'a> { /// A wasm custom section within a module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct RawCustomSection<'a> { /// Where this `@custom` was defined. pub span: Span, @@ -68,8 +68,7 @@ pub struct RawCustomSection<'a> { /// Possible locations to place a custom section within a module. #[derive(Debug, PartialEq, Copy, Clone)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum CustomPlace { /// This custom section will appear before the first section in the module. BeforeFirst, @@ -84,8 +83,7 @@ pub enum CustomPlace { /// Known sections that custom sections can be placed relative to. #[derive(Debug, PartialEq, Eq, Copy, Clone)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum CustomPlaceAnchor { Type, Import, @@ -205,9 +203,9 @@ impl<'a> Parse<'a> for CustomPlaceAnchor { /// A producers custom section #[allow(missing_docs)] #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Producers<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub fields: Vec<(&'a str, Vec<(&'a str, &'a str)>)>, } @@ -255,17 +253,16 @@ impl<'a> Parse<'a> for Producers<'a> { /// A `dylink.0` custom section #[allow(missing_docs)] #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Dylink0<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub subsections: Vec>, } /// Possible subsections of the `dylink.0` custom section #[derive(Debug)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum Dylink0Subsection<'a> { MemInfo { memory_size: u32, @@ -273,7 +270,7 @@ pub enum Dylink0Subsection<'a> { table_size: u32, table_align: u32, }, - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Needed(Vec<&'a str>), ExportInfo(Vec<(&'a str, u32)>), ImportInfo(Vec<(&'a str, &'a str, u32)>), diff --git a/crates/wast/src/core/export.rs b/crates/wast/src/core/export.rs index eb4b65db3d..4479201d96 100644 --- a/crates/wast/src/core/export.rs +++ b/crates/wast/src/core/export.rs @@ -1,11 +1,12 @@ use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Index, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A entry in a WebAssembly module's export section. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Export<'a> { /// Where this export was defined. pub span: Span, @@ -21,8 +22,7 @@ pub struct Export<'a> { /// contained in an [`Export`]. #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ExportKind { Func, Table, @@ -109,10 +109,10 @@ kw_conversions! { /// A listing of inline `(export "foo")` statements on a WebAssembly item in /// its textual format. #[derive(Debug, Default)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct InlineExport<'a> { /// The extra names to export an item as, if any. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub names: Vec<&'a str>, } diff --git a/crates/wast/src/core/expr.rs b/crates/wast/src/core/expr.rs index 6439cedc29..3750e67c5a 100644 --- a/crates/wast/src/core/expr.rs +++ b/crates/wast/src/core/expr.rs @@ -5,6 +5,7 @@ use crate::kw; use crate::parser::{Cursor, Parse, Parser, Result}; use crate::token::*; use std::mem; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// An expression, or a list of instructions, in the WebAssembly text format. @@ -14,9 +15,9 @@ use serde_derive::{Serialize, Deserialize}; /// at the end of an expression is not included in the `instrs` field. #[derive(Debug)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Expression<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub instrs: Box<[Instruction<'a>]>, pub branch_hints: Vec, } @@ -26,7 +27,7 @@ pub struct Expression<'a> { /// is to store the offset of the following instruction and check that /// it's followed by `br_if` or `if`. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct BranchHint { /// Index of instructions in `instrs` field of `Expression` that this hint /// appplies to. @@ -356,8 +357,7 @@ macro_rules! instructions { /// that this crate currently parses. #[derive(Debug)] #[allow(missing_docs)] - #[derive(Serialize, Deserialize)] - #[serde(tag = "type", content = "val")] + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum Instruction<'a> { $( $(#[$doc])* @@ -452,7 +452,7 @@ macro_rules! instructions { instructions! { pub enum Instruction<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Block(Box>) : [0x02] : "block", If(Box>) : [0x04] : "if", Else(Option>) : [0x05] : "else", @@ -1132,9 +1132,9 @@ impl<'a> Instruction<'a> { /// the block. #[derive(Debug)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct BlockType<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub label: Option>, pub label_name: Option>, pub ty: TypeUse<'a, FunctionType<'a>>, @@ -1154,9 +1154,9 @@ impl<'a> Parse<'a> for BlockType<'a> { #[derive(Debug)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct TryTable<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub block: Box>, pub catches: Vec>, } @@ -1200,11 +1200,10 @@ impl<'a> Parse<'a> for TryTable<'a> { #[derive(Debug)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum TryTableCatchKind<'a> { // Catch a tagged exception, do not capture an exnref. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Catch(Index<'a>), // Catch a tagged exception, and capture the exnref. CatchRef(Index<'a>), @@ -1226,9 +1225,9 @@ impl<'a> TryTableCatchKind<'a> { #[derive(Debug)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct TryTableCatch<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub kind: TryTableCatchKind<'a>, pub label: Index<'a>, } @@ -1236,9 +1235,9 @@ pub struct TryTableCatch<'a> { /// Extra information associated with the func.bind instruction. #[derive(Debug)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct FuncBindType<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub ty: TypeUse<'a, FunctionType<'a>>, } @@ -1255,9 +1254,9 @@ impl<'a> Parse<'a> for FuncBindType<'a> { /// Extra information associated with the let instruction. #[derive(Debug)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct LetType<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub block: Box>, pub locals: Box<[Local<'a>]>, } @@ -1274,9 +1273,9 @@ impl<'a> Parse<'a> for LetType<'a> { /// Extra information associated with the `br_table` instruction. #[allow(missing_docs)] #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct BrTableIndices<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub labels: Vec>, pub default: Index<'a>, } @@ -1294,7 +1293,7 @@ impl<'a> Parse<'a> for BrTableIndices<'a> { /// Payload for lane-related instructions. Unsigned with no + prefix. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct LaneArg { /// The lane argument. pub lane: u8, @@ -1323,7 +1322,7 @@ impl<'a> Parse<'a> for LaneArg { /// Payload for memory-related instructions indicating offset/alignment of /// memory accesses. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct MemArg<'a> { /// The alignment of this access. /// @@ -1333,7 +1332,7 @@ pub struct MemArg<'a> { /// The offset, in bytes of this access. pub offset: u64, /// The memory index we're accessing - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub memory: Index<'a>, } @@ -1400,10 +1399,10 @@ impl<'a> MemArg<'a> { /// Extra data associated with the `loadN_lane` and `storeN_lane` instructions. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct LoadOrStoreLane<'a> { /// The memory argument for this instruction. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub memarg: MemArg<'a>, /// The lane argument for this instruction. pub lane: LaneArg, @@ -1456,10 +1455,10 @@ impl<'a> LoadOrStoreLane<'a> { /// Extra data associated with the `call_indirect` instruction. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CallIndirect<'a> { /// The table that this call is going to be indexing. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub table: Index<'a>, /// Type type signature that this `call_indirect` instruction is using. pub ty: TypeUse<'a, FunctionType<'a>>, @@ -1479,10 +1478,10 @@ impl<'a> Parse<'a> for CallIndirect<'a> { /// Extra data associated with the `table.init` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct TableInit<'a> { /// The index of the table we're copying into. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub table: Index<'a>, /// The index of the element segment we're copying into a table. pub elem: Index<'a>, @@ -1503,10 +1502,10 @@ impl<'a> Parse<'a> for TableInit<'a> { /// Extra data associated with the `table.copy` instruction. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct TableCopy<'a> { /// The index of the destination table to copy into. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub dst: Index<'a>, /// The index of the source table to copy from. pub src: Index<'a>, @@ -1527,10 +1526,10 @@ impl<'a> Parse<'a> for TableCopy<'a> { /// Extra data associated with unary table instructions. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct TableArg<'a> { /// The index of the table argument. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub dst: Index<'a>, } @@ -1547,10 +1546,10 @@ impl<'a> Parse<'a> for TableArg<'a> { /// Extra data associated with unary memory instructions. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct MemoryArg<'a> { /// The index of the memory space. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub mem: Index<'a>, } @@ -1567,10 +1566,10 @@ impl<'a> Parse<'a> for MemoryArg<'a> { /// Extra data associated with the `memory.init` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct MemoryInit<'a> { /// The index of the data segment we're copying into memory. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub data: Index<'a>, /// The index of the memory we're copying into, pub mem: Index<'a>, @@ -1591,10 +1590,10 @@ impl<'a> Parse<'a> for MemoryInit<'a> { /// Extra data associated with the `memory.copy` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct MemoryCopy<'a> { /// The index of the memory we're copying from. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub src: Index<'a>, /// The index of the memory we're copying to. pub dst: Index<'a>, @@ -1615,10 +1614,10 @@ impl<'a> Parse<'a> for MemoryCopy<'a> { /// Extra data associated with the `struct.get/set` instructions #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct StructAccess<'a> { /// The index of the struct type we're accessing. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub r#struct: Index<'a>, /// The index of the field of the struct we're accessing pub field: Index<'a>, @@ -1635,10 +1634,10 @@ impl<'a> Parse<'a> for StructAccess<'a> { /// Extra data associated with the `array.fill` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ArrayFill<'a> { /// The index of the array type we're filling. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub array: Index<'a>, } @@ -1652,10 +1651,10 @@ impl<'a> Parse<'a> for ArrayFill<'a> { /// Extra data associated with the `array.copy` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ArrayCopy<'a> { /// The index of the array type we're copying to. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub dest_array: Index<'a>, /// The index of the array type we're copying from. pub src_array: Index<'a>, @@ -1672,10 +1671,10 @@ impl<'a> Parse<'a> for ArrayCopy<'a> { /// Extra data associated with the `array.init_[data/elem]` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ArrayInit<'a> { /// The index of the array type we're initializing. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub array: Index<'a>, /// The index of the data or elem segment we're reading from. pub segment: Index<'a>, @@ -1692,10 +1691,10 @@ impl<'a> Parse<'a> for ArrayInit<'a> { /// Extra data associated with the `array.new_fixed` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ArrayNewFixed<'a> { /// The index of the array type we're accessing. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub array: Index<'a>, /// The amount of values to initialize the array with. pub length: u32, @@ -1712,10 +1711,10 @@ impl<'a> Parse<'a> for ArrayNewFixed<'a> { /// Extra data associated with the `array.new_data` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ArrayNewData<'a> { /// The index of the array type we're accessing. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub array: Index<'a>, /// The data segment to initialize from. pub data_idx: Index<'a>, @@ -1732,10 +1731,10 @@ impl<'a> Parse<'a> for ArrayNewData<'a> { /// Extra data associated with the `array.new_elem` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ArrayNewElem<'a> { /// The index of the array type we're accessing. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub array: Index<'a>, /// The elem segment to initialize from. pub elem_idx: Index<'a>, @@ -1752,10 +1751,10 @@ impl<'a> Parse<'a> for ArrayNewElem<'a> { /// Extra data associated with the `ref.cast` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct RefCast<'a> { /// The type to cast to. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub r#type: RefType<'a>, } @@ -1769,10 +1768,10 @@ impl<'a> Parse<'a> for RefCast<'a> { /// Extra data associated with the `ref.test` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct RefTest<'a> { /// The type to test for. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub r#type: RefType<'a>, } @@ -1786,10 +1785,10 @@ impl<'a> Parse<'a> for RefTest<'a> { /// Extra data associated with the `br_on_cast` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct BrOnCast<'a> { /// The label to branch to. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub label: Index<'a>, /// The type we're casting from. pub from_type: RefType<'a>, @@ -1809,10 +1808,10 @@ impl<'a> Parse<'a> for BrOnCast<'a> { /// Extra data associated with the `br_on_cast_fail` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct BrOnCastFail<'a> { /// The label to branch to. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub label: Index<'a>, /// The type we're casting from. pub from_type: RefType<'a>, @@ -1833,8 +1832,7 @@ impl<'a> Parse<'a> for BrOnCastFail<'a> { /// Different ways to specify a `v128.const` instruction #[derive(Debug)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum V128Const { I8x16([i8; 16]), I16x8([i16; 8]), @@ -2000,7 +1998,7 @@ impl<'a> Parse<'a> for V128Const { /// Lanes being shuffled in the `i8x16.shuffle` instruction #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct I8x16Shuffle { #[allow(missing_docs)] pub lanes: [u8; 16], @@ -2033,10 +2031,10 @@ impl<'a> Parse<'a> for I8x16Shuffle { /// Payload of the `select` instructions #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct SelectTypes<'a> { #[allow(missing_docs)] - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub tys: Option>>, } diff --git a/crates/wast/src/core/func.rs b/crates/wast/src/core/func.rs index b1a1400ddf..14f35e8fe2 100644 --- a/crates/wast/src/core/func.rs +++ b/crates/wast/src/core/func.rs @@ -2,19 +2,20 @@ use crate::core::*; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, NameAnnotation, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A WebAssembly function to be inserted into a module. /// /// This is a member of both the function and code sections. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Func<'a> { /// Where this `func` was defined. pub span: Span, /// An identifier that this function is resolved with (optionally) for name /// resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -30,15 +31,14 @@ pub struct Func<'a> { /// Possible ways to define a function in the text format. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum FuncKind<'a> { /// A function which is actually defined as an import, such as: /// /// ```text /// (func (type 3) (import "foo" "bar")) /// ``` - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Import(InlineImport<'a>), /// Almost all functions, those defined inline in a wasm module. @@ -88,11 +88,11 @@ impl<'a> Parse<'a> for Func<'a> { /// Each local has an optional identifier for name resolution, an optional name /// for the custom `name` section, and a value type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Local<'a> { /// An identifier that this local is resolved with (optionally) for name /// resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this local stored in the custom `name` section. pub name: Option>, diff --git a/crates/wast/src/core/global.rs b/crates/wast/src/core/global.rs index 05441658c6..bf2dd944c6 100644 --- a/crates/wast/src/core/global.rs +++ b/crates/wast/src/core/global.rs @@ -2,16 +2,17 @@ use crate::core::*; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, NameAnnotation, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A WebAssembly global in a module #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Global<'a> { /// Where this `global` was defined. pub span: Span, /// An optional name to reference this global by - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -26,15 +27,14 @@ pub struct Global<'a> { /// Different kinds of globals that can be defined in a module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum GlobalKind<'a> { /// A global which is actually defined as an import, such as: /// /// ```text /// (global i32 (import "foo" "bar")) /// ``` - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Import(InlineImport<'a>), /// A global defined inline in the module itself diff --git a/crates/wast/src/core/import.rs b/crates/wast/src/core/import.rs index 0aafe28512..7552f8f50c 100644 --- a/crates/wast/src/core/import.rs +++ b/crates/wast/src/core/import.rs @@ -2,11 +2,12 @@ use crate::core::*; use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Id, NameAnnotation, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// An `import` statement and entry in a WebAssembly module. #[derive(Debug, Clone)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Import<'a> { /// Where this `import` was defined pub span: Span, @@ -35,13 +36,13 @@ impl<'a> Parse<'a> for Import<'a> { #[derive(Debug, Clone)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ItemSig<'a> { /// Where this item is defined in the source. pub span: Span, /// An optional identifier used during name resolution to refer to this item /// from the rest of the module. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name which, for functions, will be stored in the /// custom `name` section. @@ -52,10 +53,9 @@ pub struct ItemSig<'a> { #[derive(Debug, Clone)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ItemKind<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Func(TypeUse<'a, FunctionType<'a>>), Table(TableType<'a>), Memory(MemoryType), @@ -120,7 +120,7 @@ impl<'a> Parse<'a> for ItemSig<'a> { /// `Peek` rather than `Option`. #[derive(Debug, Copy, Clone)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct InlineImport<'a> { pub module: &'a str, pub field: &'a str, diff --git a/crates/wast/src/core/memory.rs b/crates/wast/src/core/memory.rs index 81be9434b9..5b406b1446 100644 --- a/crates/wast/src/core/memory.rs +++ b/crates/wast/src/core/memory.rs @@ -2,16 +2,17 @@ use crate::core::*; use crate::kw; use crate::parser::{Lookahead1, Parse, Parser, Peek, Result}; use crate::token::*; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A defined WebAssembly memory instance inside of a module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Memory<'a> { /// Where this `memory` was defined pub span: Span, /// An optional name to refer to this memory by. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -24,13 +25,12 @@ pub struct Memory<'a> { /// Different syntactical ways a memory can be defined in a module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum MemoryKind<'a> { /// This memory is actually an inlined import definition. #[allow(missing_docs)] Import { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] import: InlineImport<'a>, ty: MemoryType, }, @@ -97,13 +97,13 @@ impl<'a> Parse<'a> for Memory<'a> { /// A `data` directive in a WebAssembly module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Data<'a> { /// Where this `data` was defined pub span: Span, /// The optional name of this data segment - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this data stored in the custom `name` section. @@ -119,8 +119,7 @@ pub struct Data<'a> { /// Different kinds of data segments, either passive or active. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum DataKind<'a> { /// A passive data segment which isn't associated with a memory and is /// referenced from various instructions. @@ -130,7 +129,7 @@ pub enum DataKind<'a> { /// memory on module instantiation. Active { /// The memory that this `Data` will be associated with. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] memory: Index<'a>, /// Initial offset to load this data segment at @@ -220,8 +219,7 @@ impl<'a> Parse<'a> for Data<'a> { /// Differnet ways the value of a data segment can be defined. #[derive(Debug)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum DataVal<'a> { String(&'a [u8]), Integral(Vec), diff --git a/crates/wast/src/core/module.rs b/crates/wast/src/core/module.rs index 7fe142c505..21c2d5f197 100644 --- a/crates/wast/src/core/module.rs +++ b/crates/wast/src/core/module.rs @@ -2,18 +2,19 @@ use crate::core::*; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, Index, NameAnnotation, Span}; use crate::{annotation, kw}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; pub use crate::core::resolve::Names; /// A parsed WebAssembly core module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Module<'a> { /// Where this `module` was defined pub span: Span, /// An optional identifier this module is known by - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional `@name` annotation for this module pub name: Option>, @@ -23,11 +24,10 @@ pub struct Module<'a> { /// The different kinds of ways to define a module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ModuleKind<'a> { /// A module defined in the textual s-expression format. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Text(Vec>), /// A module that had its raw binary bytes defined via the `binary` /// directive. @@ -148,10 +148,9 @@ impl<'a> Parse<'a> for Module<'a> { /// A listing of all possible fields that can make up a WebAssembly module. #[allow(missing_docs)] #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ModuleField<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Type(Type<'a>), Rec(Rec<'a>), Import(Import<'a>), diff --git a/crates/wast/src/core/table.rs b/crates/wast/src/core/table.rs index c412fc3572..1479f5d8fa 100644 --- a/crates/wast/src/core/table.rs +++ b/crates/wast/src/core/table.rs @@ -2,16 +2,17 @@ use crate::core::*; use crate::kw; use crate::parser::{Parse, Parser, Peek, Result}; use crate::token::{Id, Index, LParen, NameAnnotation, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A WebAssembly `table` directive in a module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Table<'a> { /// Where this table was defined. pub span: Span, /// An optional name to refer to this table by. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -24,13 +25,12 @@ pub struct Table<'a> { /// Different ways to textually define a table. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum TableKind<'a> { /// This table is actually an inlined import definition. #[allow(missing_docs)] Import { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] import: InlineImport<'a>, ty: TableType<'a>, }, @@ -106,12 +106,12 @@ impl<'a> Parse<'a> for Table<'a> { /// An `elem` segment in a WebAssembly module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Elem<'a> { /// Where this `elem` was defined. pub span: Span, /// An optional name by which to refer to this segment. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this element stored in the custom `name` section. pub name: Option>, @@ -123,8 +123,7 @@ pub struct Elem<'a> { /// Different ways to define an element segment in an mdoule. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ElemKind<'a> { /// A passive segment that isn't associated with a table and can be used in /// various bulk-memory instructions. @@ -137,7 +136,7 @@ pub enum ElemKind<'a> { /// An active segment associated with a table. Active { /// The table this `elem` is initializing. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] table: Index<'a>, /// The offset within `table` that we'll initialize at. offset: Expression<'a>, @@ -146,11 +145,10 @@ pub enum ElemKind<'a> { /// Different ways to define the element segment payload in a module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ElemPayload<'a> { /// This element segment has a contiguous list of function indices - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Indices(Vec>), /// This element segment has a list of optional function indices, diff --git a/crates/wast/src/core/tag.rs b/crates/wast/src/core/tag.rs index d34636915a..1aaf111240 100644 --- a/crates/wast/src/core/tag.rs +++ b/crates/wast/src/core/tag.rs @@ -2,16 +2,17 @@ use crate::core::*; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, NameAnnotation, Span}; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A WebAssembly tag directive, part of the exception handling proposal. #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Tag<'a> { /// Where this tag was defined pub span: Span, /// An optional name by which to refer to this tag in name resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -25,26 +26,24 @@ pub struct Tag<'a> { /// Listing of various types of tags that can be defined in a wasm module. #[derive(Clone, Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum TagType<'a> { /// An exception tag, where the payload is the type signature of the tag /// (constructor parameters, etc). - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Exception(TypeUse<'a, FunctionType<'a>>), } /// Different kinds of tags that can be defined in a module. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum TagKind<'a> { /// An tag which is actually defined as an import, such as: /// /// ```text /// (tag (type 0) (import "foo" "bar")) /// ``` - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Import(InlineImport<'a>), /// A tag defined inline in the module itself diff --git a/crates/wast/src/core/types.rs b/crates/wast/src/core/types.rs index e0d8b4fb27..58a949f3b7 100644 --- a/crates/wast/src/core/types.rs +++ b/crates/wast/src/core/types.rs @@ -3,20 +3,20 @@ use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Id, Index, LParen, NameAnnotation, Span}; use std::mem; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// The value types for a wasm module. #[allow(missing_docs)] #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum ValType<'a> { I32, I64, F32, F64, V128, - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Ref(RefType<'a>), } @@ -63,8 +63,7 @@ impl<'a> Peek for ValType<'a> { /// A heap type for a reference type #[allow(missing_docs)] #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum HeapType<'a> { /// An untyped function reference: funcref. This is part of the reference /// types proposal. @@ -94,7 +93,7 @@ pub enum HeapType<'a> { None, /// A reference to a concrete function, struct, or array type defined by /// Wasm: `ref T`. This is part of the function references and GC proposals. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Concrete(Index<'a>), } @@ -165,10 +164,10 @@ impl<'a> Peek for HeapType<'a> { /// A reference type in a wasm module. #[allow(missing_docs)] #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct RefType<'a> { pub nullable: bool, - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub heap: HeapType<'a>, } @@ -347,12 +346,11 @@ impl<'a> Peek for RefType<'a> { /// The types of values that may be used in a struct or array. #[allow(missing_docs)] #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum StorageType<'a> { I8, I16, - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Val(ValType<'a>), } @@ -375,10 +373,10 @@ impl<'a> Parse<'a> for StorageType<'a> { /// Type for a `global` in a wasm module #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct GlobalType<'a> { /// The element type of this `global` - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub ty: ValType<'a>, /// Whether or not the global is mutable or not. pub mutable: bool, @@ -405,7 +403,7 @@ impl<'a> Parse<'a> for GlobalType<'a> { /// Min/max limits used for tables/memories. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Limits { /// The minimum number of units for this type. pub min: u32, @@ -427,7 +425,7 @@ impl<'a> Parse<'a> for Limits { /// Min/max limits used for 64-bit memories #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Limits64 { /// The minimum number of units for this type. pub min: u64, @@ -449,12 +447,12 @@ impl<'a> Parse<'a> for Limits64 { /// Configuration for a table of a wasm mdoule #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct TableType<'a> { /// Limits on the element sizes of this table pub limits: Limits, /// The type of element stored in this table - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub elem: RefType<'a>, } @@ -469,8 +467,7 @@ impl<'a> Parse<'a> for TableType<'a> { /// Configuration for a memory of a wasm module #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum MemoryType { /// A 32-bit memory B32 { @@ -506,11 +503,11 @@ impl<'a> Parse<'a> for MemoryType { /// A function type with parameters and results. #[derive(Clone, Debug, Default)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct FunctionType<'a> { /// The parameters of a function, optionally each having an identifier for /// name resolution and a name for the custom `name` section. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub params: Box<[(Option>, Option>, ValType<'a>)]>, /// The results types of a function. pub results: Box<[ValType<'a>]>, @@ -623,10 +620,10 @@ impl<'a> From> for FunctionType<'a> { /// A struct type with fields. #[derive(Clone, Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct StructType<'a> { /// The fields of the struct - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub fields: Vec>, } @@ -654,10 +651,10 @@ impl<'a> Parse<'a> for StructType<'a> { /// A field of a struct type. #[derive(Clone, Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct StructField<'a> { /// An optional identifier for name resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// Whether this field may be mutated or not. pub mutable: bool, @@ -683,12 +680,12 @@ impl<'a> StructField<'a> { /// An array type with fields. #[derive(Clone, Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ArrayType<'a> { /// Whether this field may be mutated or not. pub mutable: bool, /// The storage type stored in this field. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub ty: StorageType<'a>, } @@ -729,11 +726,10 @@ impl<'a> Parse<'a> for ExportType<'a> { /// A definition of a type. #[derive(Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum TypeDef<'a> { /// A function type definition. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Func(FunctionType<'a>), /// A struct type definition. Struct(StructType<'a>), @@ -761,13 +757,13 @@ impl<'a> Parse<'a> for TypeDef<'a> { /// A type declaration in a module #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Type<'a> { /// Where this type was defined. pub span: Span, /// An optional identifier to refer to this `type` by as part of name /// resolution. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub id: Option>, /// An optional name for this function stored in the custom `name` section. pub name: Option>, @@ -830,12 +826,12 @@ impl<'a> Parse<'a> for Type<'a> { /// A recursion group declaration in a module #[derive(Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Rec<'a> { /// Where this recursion group was defined. pub span: Span, /// The types that we're defining in this group. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub types: Vec>, } @@ -852,10 +848,10 @@ impl<'a> Parse<'a> for Rec<'a> { /// A reference to a type defined in this module. #[derive(Clone, Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct TypeUse<'a, T> { /// The type that we're referencing, if it was present. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] pub index: Option>, /// The inline type, if present. pub inline: Option, diff --git a/crates/wast/src/lib.rs b/crates/wast/src/lib.rs index 1fb9677242..367251c19e 100644 --- a/crates/wast/src/lib.rs +++ b/crates/wast/src/lib.rs @@ -96,7 +96,7 @@ macro_rules! custom_keyword { #[allow(non_camel_case_types)] #[allow(missing_docs)] #[derive(Debug, Copy, Clone)] - #[derive(Serialize, Deserialize)] + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct $name(pub $crate::token::Span); impl<'a> $crate::parser::Parse<'a> for $name { @@ -374,7 +374,8 @@ id! { /// Common keyword used to parse WebAssembly text files. pub mod kw { - use serde_derive::{Serialize, Deserialize}; + #[cfg(feature = "serde")] +use serde_derive::{Serialize, Deserialize}; custom_keyword!(after); custom_keyword!(alias); diff --git a/crates/wast/src/token.rs b/crates/wast/src/token.rs index 82c7c3533f..78134553df 100644 --- a/crates/wast/src/token.rs +++ b/crates/wast/src/token.rs @@ -8,11 +8,12 @@ use crate::{annotation, Error}; use std::fmt; use std::hash::{Hash, Hasher}; use std::str; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A position in the original source stream, used to render errors. #[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Span { pub(crate) offset: usize, } @@ -53,7 +54,7 @@ impl Span { /// An identifier is used to symbolically refer to items in a a wasm module, /// typically via the [`Index`] type. #[derive(Copy, Clone)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Id<'a> { name: &'a str, gen: u32, @@ -165,15 +166,14 @@ impl Peek for Id<'_> { /// The emission phase of a module will ensure that `Index::Id` is never used /// and switch them all to `Index::Num`. #[derive(Copy, Clone, Debug)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum Index<'a> { /// A numerical index that this references. The index space this is /// referencing is implicit based on where this [`Index`] is stored. Num(u32, Span), /// A human-readable identifier this references. Like `Num`, the namespace /// this references is based on where this is stored. - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Id(Id<'a>), } @@ -282,7 +282,7 @@ impl<'a, K: Peek> Peek for ItemRef<'a, K> { /// An `@name` annotation in source, currently of the form `@name "foo"` #[derive(Copy, Clone, PartialEq, Eq, Debug)] -#[derive(Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct NameAnnotation<'a> { /// The name specified for the item pub name: &'a str, @@ -407,7 +407,7 @@ macro_rules! float { })*) => ($( /// A parsed floating-point type #[derive(Debug, Copy, Clone)] - #[derive(Serialize, Deserialize)] + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct $name { /// The raw bits that this floating point number represents. pub bits: $int, diff --git a/crates/wast/src/wat.rs b/crates/wast/src/wat.rs index 1d66422b1a..9b4c6cd79a 100644 --- a/crates/wast/src/wat.rs +++ b/crates/wast/src/wat.rs @@ -3,6 +3,7 @@ use crate::core::{Module, ModuleField, ModuleKind}; use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::Span; +#[cfg(feature = "serde")] use serde_derive::{Serialize, Deserialize}; /// A `*.wat` file parser, or a parser for one parenthesized module. @@ -12,10 +13,9 @@ use serde_derive::{Serialize, Deserialize}; /// of s-expressions that are module fields. #[derive(Debug)] #[allow(missing_docs)] -#[derive(Serialize, Deserialize)] -#[serde(tag = "type", content = "val")] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] pub enum Wat<'a> { - #[serde(borrow)] + #[cfg_attr(feature = "serde", serde(borrow))] Module(Module<'a>), Component(Component<'a>), } From fe96b094d89afda17f9e3ca2aaeca205fa5a5b11 Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Thu, 29 Feb 2024 21:06:59 -0800 Subject: [PATCH 5/7] fmt --- crates/wast/src/lib.rs | 2 +- crates/wast/src/token.rs | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/wast/src/lib.rs b/crates/wast/src/lib.rs index 367251c19e..b1fc97f7d7 100644 --- a/crates/wast/src/lib.rs +++ b/crates/wast/src/lib.rs @@ -375,7 +375,7 @@ id! { /// Common keyword used to parse WebAssembly text files. pub mod kw { #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; + use serde_derive::{Deserialize, Serialize}; custom_keyword!(after); custom_keyword!(alias); diff --git a/crates/wast/src/token.rs b/crates/wast/src/token.rs index 78134553df..26a61c72ef 100644 --- a/crates/wast/src/token.rs +++ b/crates/wast/src/token.rs @@ -5,11 +5,11 @@ use crate::lexer::{Float, Lexer, TokenKind}; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::{annotation, Error}; +#[cfg(feature = "serde")] +use serde_derive::{Deserialize, Serialize}; use std::fmt; use std::hash::{Hash, Hasher}; use std::str; -#[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; /// A position in the original source stream, used to render errors. #[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)] @@ -166,7 +166,11 @@ impl Peek for Id<'_> { /// The emission phase of a module will ensure that `Index::Id` is never used /// and switch them all to `Index::Num`. #[derive(Copy, Clone, Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum Index<'a> { /// A numerical index that this references. The index space this is /// referencing is implicit based on where this [`Index`] is stored. From b0206cddcbafafbb25205e80d908fc981a481d9e Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Thu, 29 Feb 2024 21:48:51 -0800 Subject: [PATCH 6/7] fmt again, maybe --- crates/wast/src/component/alias.rs | 20 +++- crates/wast/src/component/binary.rs | 25 +++-- crates/wast/src/component/component.rs | 20 +++- crates/wast/src/component/custom.rs | 2 +- crates/wast/src/component/expand.rs | 4 +- crates/wast/src/component/export.rs | 8 +- crates/wast/src/component/func.rs | 33 +++++-- crates/wast/src/component/import.rs | 19 ++-- crates/wast/src/component/instance.rs | 26 +++++- crates/wast/src/component/item_ref.rs | 72 ++++++++------- crates/wast/src/component/module.rs | 8 +- crates/wast/src/component/resolve.rs | 26 ++++-- crates/wast/src/component/types.rs | 122 +++++++++++++++++-------- crates/wast/src/core/custom.rs | 26 +++++- crates/wast/src/core/export.rs | 8 +- crates/wast/src/core/expr.rs | 16 +++- crates/wast/src/core/func.rs | 8 +- crates/wast/src/core/global.rs | 8 +- crates/wast/src/core/import.rs | 8 +- crates/wast/src/core/memory.rs | 20 +++- crates/wast/src/core/module.rs | 14 ++- crates/wast/src/core/table.rs | 20 +++- crates/wast/src/core/tag.rs | 14 ++- crates/wast/src/core/types.rs | 34 +++++-- crates/wast/src/wat.rs | 8 +- 25 files changed, 396 insertions(+), 173 deletions(-) diff --git a/crates/wast/src/component/alias.rs b/crates/wast/src/component/alias.rs index 46bc140cf7..b58534bc92 100644 --- a/crates/wast/src/component/alias.rs +++ b/crates/wast/src/component/alias.rs @@ -3,7 +3,7 @@ use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, Index, NameAnnotation, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// A inline alias for component exported items. /// @@ -142,7 +142,11 @@ impl<'a> Parse<'a> for Alias<'a> { /// Represents the kind of instance export alias. #[derive(Debug, Copy, Clone, PartialEq, Eq)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ComponentExportAliasKind { /// The alias is to a core module export. CoreModule, @@ -193,7 +197,11 @@ impl<'a> Parse<'a> for ComponentExportAliasKind { /// Represents the kind of outer alias. #[derive(Debug, Copy, Clone, PartialEq, Eq)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ComponentOuterAliasKind { /// The alias is to an outer core module. CoreModule, @@ -234,7 +242,11 @@ impl<'a> Parse<'a> for ComponentOuterAliasKind { /// The target of a component alias. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum AliasTarget<'a> { /// The alias is to an export of a component instance. Export { diff --git a/crates/wast/src/component/binary.rs b/crates/wast/src/component/binary.rs index ce44a23e85..b419a14acc 100644 --- a/crates/wast/src/component/binary.rs +++ b/crates/wast/src/component/binary.rs @@ -1,6 +1,8 @@ use crate::component::*; use crate::core; use crate::token::{Id, Index, NameAnnotation, Span}; +#[cfg(feature = "serde")] +use serde::Serialize as SerializeT; use wasm_encoder::{ CanonicalFunctionSection, ComponentAliasSection, ComponentDefinedTypeEncoder, ComponentExportSection, ComponentImportSection, ComponentInstanceSection, ComponentNameSection, @@ -8,8 +10,6 @@ use wasm_encoder::{ ComponentTypeSection, CoreTypeEncoder, CoreTypeSection, InstanceSection, NameMap, NestedComponentSection, RawSection, SectionId, }; -#[cfg(feature = "serde")] -use serde::Serialize as SerializeT; pub fn encode(component: &Component<'_>) -> Vec { match &component.kind { @@ -733,20 +733,18 @@ impl From> for u32 { } } -impl< - #[cfg(feature = "serde")] T: SerializeT, - #[cfg(not(feature = "serde"))] T, -> From<&ItemRef<'_, T>> for u32 { +impl<#[cfg(feature = "serde")] T: SerializeT, #[cfg(not(feature = "serde"))] T> + From<&ItemRef<'_, T>> for u32 +{ fn from(i: &ItemRef<'_, T>) -> Self { assert!(i.export_names.is_empty()); i.idx.into() } } -impl< - #[cfg(feature = "serde")] T: SerializeT, - #[cfg(not(feature = "serde"))] T, -> From<&CoreTypeUse<'_, T>> for u32 { +impl<#[cfg(feature = "serde")] T: SerializeT, #[cfg(not(feature = "serde"))] T> + From<&CoreTypeUse<'_, T>> for u32 +{ fn from(u: &CoreTypeUse<'_, T>) -> Self { match u { CoreTypeUse::Inline(_) => unreachable!("should be expanded already"), @@ -755,10 +753,9 @@ impl< } } -impl< - #[cfg(feature = "serde")] T: SerializeT, - #[cfg(not(feature = "serde"))] T, -> From<&ComponentTypeUse<'_, T>> for u32 { +impl<#[cfg(feature = "serde")] T: SerializeT, #[cfg(not(feature = "serde"))] T> + From<&ComponentTypeUse<'_, T>> for u32 +{ fn from(u: &ComponentTypeUse<'_, T>) -> Self { match u { ComponentTypeUse::Inline(_) => unreachable!("should be expanded already"), diff --git a/crates/wast/src/component/component.rs b/crates/wast/src/component/component.rs index 64bd75f87f..153f3305dd 100644 --- a/crates/wast/src/component/component.rs +++ b/crates/wast/src/component/component.rs @@ -6,7 +6,7 @@ use crate::parser::{Parse, Parser, Result}; use crate::token::Index; use crate::token::{Id, NameAnnotation, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// A parsed WebAssembly component module. #[derive(Debug)] @@ -25,7 +25,11 @@ pub struct Component<'a> { /// The different kinds of ways to define a component. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ComponentKind<'a> { /// A component defined in the textual s-expression format. #[cfg_attr(feature = "serde", serde(borrow))] @@ -145,7 +149,11 @@ impl<'a> Parse<'a> for Component<'a> { /// A listing of all possible fields that can make up a WebAssembly component. #[allow(missing_docs)] #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ComponentField<'a> { #[cfg_attr(feature = "serde", serde(borrow))] CoreModule(CoreModule<'a>), @@ -287,7 +295,11 @@ pub struct NestedComponent<'a> { /// The different kinds of ways to define a nested component. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum NestedComponentKind<'a> { /// This is actually an inline import of a component Import { diff --git a/crates/wast/src/component/custom.rs b/crates/wast/src/component/custom.rs index 708e5421b9..5b6a3bf56e 100644 --- a/crates/wast/src/component/custom.rs +++ b/crates/wast/src/component/custom.rs @@ -2,7 +2,7 @@ use crate::annotation; use crate::parser::{Parse, Parser, Result}; use crate::token::Span; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// A custom section within a component. #[derive(Debug)] diff --git a/crates/wast/src/component/expand.rs b/crates/wast/src/component/expand.rs index e94e06fffa..690d958188 100644 --- a/crates/wast/src/component/expand.rs +++ b/crates/wast/src/component/expand.rs @@ -4,10 +4,10 @@ use crate::gensym; use crate::kw; use crate::token::Id; use crate::token::{Index, Span}; -use std::collections::HashMap; -use std::mem; #[cfg(feature = "serde")] use serde::Serialize as SerializeT; +use std::collections::HashMap; +use std::mem; /// Performs an AST "expansion" pass over the component fields provided. /// diff --git a/crates/wast/src/component/export.rs b/crates/wast/src/component/export.rs index cf97bacebb..349a75ca5f 100644 --- a/crates/wast/src/component/export.rs +++ b/crates/wast/src/component/export.rs @@ -3,7 +3,7 @@ use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Id, Index, NameAnnotation, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// An entry in a WebAssembly component's export section. #[derive(Debug)] @@ -59,7 +59,11 @@ impl<'a> Parse<'a> for Vec> { /// The kind of exported item. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ComponentExportKind<'a> { /// The export is a core module. /// diff --git a/crates/wast/src/component/func.rs b/crates/wast/src/component/func.rs index 9b7e6b4cb9..a89b957864 100644 --- a/crates/wast/src/component/func.rs +++ b/crates/wast/src/component/func.rs @@ -3,9 +3,9 @@ use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, Index, LParen, NameAnnotation, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; -#[cfg(feature = "serde")] use serde::Serialize as SerializeT; +#[cfg(feature = "serde")] +use serde_derive::{Deserialize, Serialize}; /// A declared core function. /// @@ -45,7 +45,11 @@ impl<'a> Parse<'a> for CoreFunc<'a> { /// Represents the kind of core functions. #[derive(Debug)] #[allow(missing_docs)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum CoreFuncKind<'a> { /// The core function is defined in terms of lowering a component function. /// @@ -129,7 +133,11 @@ impl<'a> Parse<'a> for Func<'a> { /// Represents the kind of component functions. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum FuncKind<'a> { /// A function which is actually defined as an import, such as: /// @@ -261,7 +269,11 @@ impl<'a> CanonicalFunc<'a> { /// Possible ways to define a canonical function in the text format. #[derive(Debug)] #[allow(missing_docs)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum CanonicalFuncKind<'a> { /// A canonical function that is defined in terms of lifting a core function. Lift { @@ -437,7 +449,11 @@ impl Default for CanonResourceRep<'_> { #[derive(Debug)] /// Canonical ABI options. -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum CanonOpt<'a> { /// Encode strings as UTF-8. StringUtf8, @@ -498,7 +514,10 @@ impl<'a> Parse<'a> for CanonOpt<'a> { fn parse_trailing_item_ref< #[cfg(feature = "serde")] T: SerializeT, #[cfg(not(feature = "serde"))] T, ->(kind: T, parser: Parser) -> Result> { +>( + kind: T, + parser: Parser, +) -> Result> { Ok(CoreItemRef { kind, idx: parser.parse()?, diff --git a/crates/wast/src/component/import.rs b/crates/wast/src/component/import.rs index ce116174c3..f2b9f8efe6 100644 --- a/crates/wast/src/component/import.rs +++ b/crates/wast/src/component/import.rs @@ -3,7 +3,7 @@ use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Id, Index, LParen, NameAnnotation, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// An `import` statement and entry in a WebAssembly component. #[derive(Debug)] @@ -78,10 +78,7 @@ impl<'a> Parse<'a> for ItemSig<'a> { /// An item signature for imported items. #[derive(Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct ItemSigNoName<'a>( - #[cfg_attr(feature = "serde", serde(borrow))] - pub ItemSig<'a> -); +pub struct ItemSigNoName<'a>(#[cfg_attr(feature = "serde", serde(borrow))] pub ItemSig<'a>); impl<'a> Parse<'a> for ItemSigNoName<'a> { fn parse(parser: Parser<'a>) -> Result { @@ -125,7 +122,11 @@ fn parse_item_sig<'a>(parser: Parser<'a>, name: bool) -> Result> { /// The kind of signatures for imported items. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ItemSigKind<'a> { /// The item signature is for a core module. #[cfg_attr(feature = "serde", serde(borrow))] @@ -144,7 +145,11 @@ pub enum ItemSigKind<'a> { /// Represents the bounds applied to types being imported. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum TypeBounds<'a> { /// The equality type bounds. #[cfg_attr(feature = "serde", serde(borrow))] diff --git a/crates/wast/src/component/instance.rs b/crates/wast/src/component/instance.rs index 8a7a564ac6..f2caa08528 100644 --- a/crates/wast/src/component/instance.rs +++ b/crates/wast/src/component/instance.rs @@ -4,7 +4,7 @@ use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, LParen, NameAnnotation, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// A core instance defined by instantiation or exporting core items. #[derive(Debug)] @@ -41,7 +41,11 @@ impl<'a> Parse<'a> for CoreInstance<'a> { /// The kinds of core instances in the text format. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum CoreInstanceKind<'a> { /// Instantiate a core module. Instantiate { @@ -109,7 +113,11 @@ impl<'a> Parse<'a> for Vec> { /// The kind of core instantiation argument. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum CoreInstantiationArgKind<'a> { /// The argument is a reference to an instance. #[cfg_attr(feature = "serde", serde(borrow))] @@ -205,7 +213,11 @@ impl<'a> Parse<'a> for Instance<'a> { /// The kinds of instances in the text format. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum InstanceKind<'a> { /// The `(instance (import "x"))` sugar syntax Import { @@ -287,7 +299,11 @@ impl<'a> Parse<'a> for Vec> { /// The kind of instantiation argument. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum InstantiationArgKind<'a> { /// The argument is a reference to a component item. #[cfg_attr(feature = "serde", serde(borrow))] diff --git a/crates/wast/src/component/item_ref.rs b/crates/wast/src/component/item_ref.rs index 94f0ad1af5..6871a606ac 100644 --- a/crates/wast/src/component/item_ref.rs +++ b/crates/wast/src/component/item_ref.rs @@ -3,7 +3,7 @@ use crate::token::Index; #[cfg(feature = "serde")] use serde::Serialize as SerializeT; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; fn peek(cursor: Cursor) -> Result { // This is a little fancy because when parsing something like: @@ -44,7 +44,8 @@ fn peek(cursor: Cursor) -> Result { /// Parses core item references. #[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct CoreItemRef<'a, +pub struct CoreItemRef< + 'a, #[cfg(feature = "serde")] K: SerializeT, #[cfg(not(feature = "serde"))] K, > { @@ -56,10 +57,12 @@ pub struct CoreItemRef<'a, pub export_name: Option<&'a str>, } -impl<'a, - #[cfg(feature = "serde")] K: Parse<'a> + SerializeT, - #[cfg(not(feature = "serde"))] K: Parse<'a>, -> Parse<'a> for CoreItemRef<'a, K> { +impl< + 'a, + #[cfg(feature = "serde")] K: Parse<'a> + SerializeT, + #[cfg(not(feature = "serde"))] K: Parse<'a>, + > Parse<'a> for CoreItemRef<'a, K> +{ fn parse(parser: Parser<'a>) -> Result { // This does not parse the surrounding `(` and `)` because // core prefix is context dependent and only the caller knows if it should be @@ -75,10 +78,12 @@ impl<'a, } } -impl<'a, - #[cfg(feature = "serde")] K: Peek + SerializeT, - #[cfg(not(feature = "serde"))] K: Peek, -> Peek for CoreItemRef<'a, K> { +impl< + 'a, + #[cfg(feature = "serde")] K: Peek + SerializeT, + #[cfg(not(feature = "serde"))] K: Peek, + > Peek for CoreItemRef<'a, K> +{ fn peek(cursor: Cursor<'_>) -> Result { peek::(cursor) } @@ -91,10 +96,7 @@ impl<'a, /// Parses component item references. #[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct ItemRef<'a, - #[cfg(feature = "serde")] K: SerializeT, - #[cfg(not(feature = "serde"))] K, -> { +pub struct ItemRef<'a, #[cfg(feature = "serde")] K: SerializeT, #[cfg(not(feature = "serde"))] K> { /// The item kind being parsed. pub kind: K, /// The item or instance reference. @@ -104,10 +106,12 @@ pub struct ItemRef<'a, pub export_names: Vec<&'a str>, } -impl<'a, - #[cfg(feature = "serde")] K: Parse<'a> + SerializeT, - #[cfg(not(feature = "serde"))] K: Parse<'a> +, -> Parse<'a> for ItemRef<'a, K> { +impl< + 'a, + #[cfg(feature = "serde")] K: Parse<'a> + SerializeT, + #[cfg(not(feature = "serde"))] K: Parse<'a>, + > Parse<'a> for ItemRef<'a, K> +{ fn parse(parser: Parser<'a>) -> Result { let kind = parser.parse::()?; let idx = parser.parse()?; @@ -123,10 +127,12 @@ impl<'a, } } -impl<'a, - #[cfg(feature = "serde")] K: Peek + SerializeT, - #[cfg(not(feature = "serde"))] K: Peek, -> Peek for ItemRef<'a, K> { +impl< + 'a, + #[cfg(feature = "serde")] K: Peek + SerializeT, + #[cfg(not(feature = "serde"))] K: Peek, + > Peek for ItemRef<'a, K> +{ fn peek(cursor: Cursor<'_>) -> Result { peek::(cursor) } @@ -138,15 +144,12 @@ impl<'a, /// Convenience structure to parse `$f` or `(item $f)`. #[derive(Clone, Debug)] -pub struct IndexOrRef<'a, - #[cfg(feature = "serde")] K: SerializeT, - #[cfg(not(feature = "serde"))] K, ->(pub ItemRef<'a, K>); +pub struct IndexOrRef<'a, #[cfg(feature = "serde")] K: SerializeT, #[cfg(not(feature = "serde"))] K>( + pub ItemRef<'a, K>, +); -impl<'a, - #[cfg(feature = "serde")] K: SerializeT, - #[cfg(not(feature = "serde"))] K, -> Parse<'a> for IndexOrRef<'a, K> +impl<'a, #[cfg(feature = "serde")] K: SerializeT, #[cfg(not(feature = "serde"))] K> Parse<'a> + for IndexOrRef<'a, K> where K: Parse<'a> + Default, { @@ -165,15 +168,14 @@ where /// Convenience structure to parse `$f` or `(item $f)`. #[derive(Clone, Debug)] -pub struct IndexOrCoreRef<'a, +pub struct IndexOrCoreRef< + 'a, #[cfg(feature = "serde")] K: SerializeT, #[cfg(not(feature = "serde"))] K, >(pub CoreItemRef<'a, K>); -impl<'a, - #[cfg(feature = "serde")] K: SerializeT, - #[cfg(not(feature = "serde"))] K, -> Parse<'a> for IndexOrCoreRef<'a, K> +impl<'a, #[cfg(feature = "serde")] K: SerializeT, #[cfg(not(feature = "serde"))] K> Parse<'a> + for IndexOrCoreRef<'a, K> where K: Parse<'a> + Default, { diff --git a/crates/wast/src/component/module.rs b/crates/wast/src/component/module.rs index d5f40cd2ff..c1082157a5 100644 --- a/crates/wast/src/component/module.rs +++ b/crates/wast/src/component/module.rs @@ -4,7 +4,7 @@ use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, NameAnnotation, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// A core WebAssembly module to be created as part of a component. /// @@ -29,7 +29,11 @@ pub struct CoreModule<'a> { /// Possible ways to define a core module in the text format. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum CoreModuleKind<'a> { /// A core module which is actually defined as an import Import { diff --git a/crates/wast/src/component/resolve.rs b/crates/wast/src/component/resolve.rs index 3d1fed0848..f157903830 100644 --- a/crates/wast/src/component/resolve.rs +++ b/crates/wast/src/component/resolve.rs @@ -393,10 +393,10 @@ impl<'a> Resolver<'a> { Ok(()) } - fn core_type_use< - #[cfg(feature = "serde")] T: SerializeT, - #[cfg(not(feature = "serde"))] T, - >(&mut self, ty: &mut CoreTypeUse<'a, T>) -> Result<(), Error> { + fn core_type_use<#[cfg(feature = "serde")] T: SerializeT, #[cfg(not(feature = "serde"))] T>( + &mut self, + ty: &mut CoreTypeUse<'a, T>, + ) -> Result<(), Error> { let item = match ty { CoreTypeUse::Ref(r) => r, CoreTypeUse::Inline(_) => { @@ -409,7 +409,10 @@ impl<'a> Resolver<'a> { fn component_type_use< #[cfg(feature = "serde")] T: SerializeT, #[cfg(not(feature = "serde"))] T, - >(&mut self, ty: &mut ComponentTypeUse<'a, T>) -> Result<(), Error> { + >( + &mut self, + ty: &mut ComponentTypeUse<'a, T>, + ) -> Result<(), Error> { let item = match ty { ComponentTypeUse::Ref(r) => r, ComponentTypeUse::Inline(_) => { @@ -615,10 +618,10 @@ impl<'a> Resolver<'a> { ) } - fn core_item_ref< - #[cfg(feature = "serde")] K: SerializeT, - #[cfg(not(feature = "serde"))] K, - >(&mut self, item: &mut CoreItemRef<'a, K>) -> Result<(), Error> + fn core_item_ref<#[cfg(feature = "serde")] K: SerializeT, #[cfg(not(feature = "serde"))] K>( + &mut self, + item: &mut CoreItemRef<'a, K>, + ) -> Result<(), Error> where K: CoreItem + Copy, { @@ -657,7 +660,10 @@ impl<'a> Resolver<'a> { fn component_item_ref< #[cfg(feature = "serde")] K: SerializeT, #[cfg(not(feature = "serde"))] K, - >(&mut self, item: &mut ItemRef<'a, K>) -> Result<(), Error> + >( + &mut self, + item: &mut ItemRef<'a, K>, + ) -> Result<(), Error> where K: ComponentItem + Copy, { diff --git a/crates/wast/src/component/types.rs b/crates/wast/src/component/types.rs index 0a71a13534..b82683befc 100644 --- a/crates/wast/src/component/types.rs +++ b/crates/wast/src/component/types.rs @@ -8,9 +8,9 @@ use crate::token::Index; use crate::token::LParen; use crate::token::{Id, NameAnnotation, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; -#[cfg(feature = "serde")] use serde::Serialize as SerializeT; +#[cfg(feature = "serde")] +use serde_derive::{Deserialize, Serialize}; /// A core type declaration. #[derive(Debug)] @@ -50,7 +50,11 @@ impl<'a> Parse<'a> for CoreType<'a> { /// In the future this may be removed when module types are a part of /// a core module. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum CoreTypeDef<'a> { /// The type definition is one of the core types. #[cfg_attr(feature = "serde", serde(borrow))] @@ -90,7 +94,11 @@ impl<'a> Parse<'a> for ModuleType<'a> { /// The declarations of a [`ModuleType`]. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ModuleTypeDecl<'a> { /// A core type. Type(core::Type<'a>), @@ -185,7 +193,11 @@ impl<'a> Type<'a> { /// A definition of a component type. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum TypeDef<'a> { /// A defined value type. #[cfg_attr(feature = "serde", serde(borrow))] @@ -235,7 +247,11 @@ impl<'a> Parse<'a> for TypeDef<'a> { /// A primitive value type. #[allow(missing_docs)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum PrimitiveValType { Bool, S8, @@ -328,7 +344,11 @@ impl Peek for PrimitiveValType { /// A component value type. #[allow(missing_docs)] #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ComponentValType<'a> { /// The value type is an inline defined type. #[cfg_attr(feature = "serde", serde(borrow))] @@ -364,8 +384,7 @@ impl Peek for ComponentValType<'_> { #[derive(Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct InlineComponentValType<'a>( - #[cfg_attr(feature = "serde", serde(borrow))] - ComponentDefinedType<'a> + #[cfg_attr(feature = "serde", serde(borrow))] ComponentDefinedType<'a>, ); impl<'a> Parse<'a> for InlineComponentValType<'a> { @@ -386,7 +405,11 @@ impl<'a> Parse<'a> for InlineComponentValType<'a> { // A component defined type. #[allow(missing_docs)] #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ComponentDefinedType<'a> { Primitive(PrimitiveValType), #[cfg_attr(feature = "serde", serde(borrow))] @@ -568,13 +591,16 @@ impl<'a> Parse<'a> for VariantCase<'a> { /// A refinement for a variant case. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum Refinement<'a> { /// The refinement is referenced by index. Index( Span, - #[cfg_attr(feature = "serde", serde(borrow))] - Index<'a> + #[cfg_attr(feature = "serde", serde(borrow))] Index<'a>, ), /// The refinement has been resolved to an index into /// the cases of the variant. @@ -840,7 +866,11 @@ impl<'a> Parse<'a> for ComponentType<'a> { /// A declaration of a component type. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ComponentTypeDecl<'a> { /// A core type definition local to the component type. #[cfg_attr(feature = "serde", serde(borrow))] @@ -904,7 +934,11 @@ impl<'a> Parse<'a> for InstanceType<'a> { /// A declaration of an instance type. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum InstanceTypeDecl<'a> { /// A core type definition local to the component type. #[cfg_attr(feature = "serde", serde(borrow))] @@ -977,8 +1011,7 @@ impl<'a> Parse<'a> for ResourceType<'a> { #[derive(Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ComponentValTypeUse<'a>( - #[cfg_attr(feature = "serde", serde(borrow))] - pub ComponentValType<'a> + #[cfg_attr(feature = "serde", serde(borrow))] pub ComponentValType<'a>, ); impl<'a> Parse<'a> for ComponentValTypeUse<'a> { @@ -995,11 +1028,13 @@ impl<'a> Parse<'a> for ComponentValTypeUse<'a> { /// This is the same as `TypeUse`, but accepts `$T` as shorthand for /// `(type $T)`. #[derive(Debug, Clone)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] -pub enum CoreTypeUse<'a, - #[cfg(feature = "serde")] T: SerializeT, - #[cfg(not(feature = "serde"))] T, -> { +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] +pub enum CoreTypeUse<'a, #[cfg(feature = "serde")] T: SerializeT, #[cfg(not(feature = "serde"))] T> +{ /// The type that we're referencing. #[cfg_attr(feature = "serde", serde(borrow))] Ref(CoreItemRef<'a, kw::r#type>), @@ -1007,10 +1042,12 @@ pub enum CoreTypeUse<'a, Inline(T), } -impl<'a, - #[cfg(feature = "serde")] T: Parse<'a> + SerializeT, - #[cfg(not(feature = "serde"))] T: Parse<'a>, -> Parse<'a> for CoreTypeUse<'a, T> { +impl< + 'a, + #[cfg(feature = "serde")] T: Parse<'a> + SerializeT, + #[cfg(not(feature = "serde"))] T: Parse<'a>, + > Parse<'a> for CoreTypeUse<'a, T> +{ fn parse(parser: Parser<'a>) -> Result { // Here the core context is assumed, so no core prefix is expected if parser.peek::()? && parser.peek2::>()? { @@ -1021,10 +1058,9 @@ impl<'a, } } -impl< - #[cfg(feature = "serde")] T: SerializeT, - #[cfg(not(feature = "serde"))] T, -> Default for CoreTypeUse<'_, T> { +impl<#[cfg(feature = "serde")] T: SerializeT, #[cfg(not(feature = "serde"))] T> Default + for CoreTypeUse<'_, T> +{ fn default() -> Self { let span = Span::from_offset(0); Self::Ref(CoreItemRef { @@ -1040,8 +1076,13 @@ impl< /// This is the same as `TypeUse`, but accepts `$T` as shorthand for /// `(type $T)`. #[derive(Debug, Clone)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] -pub enum ComponentTypeUse<'a, +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] +pub enum ComponentTypeUse< + 'a, #[cfg(feature = "serde")] T: SerializeT, #[cfg(not(feature = "serde"))] T, > { @@ -1052,10 +1093,12 @@ pub enum ComponentTypeUse<'a, Inline(T), } -impl<'a, - #[cfg(feature = "serde")] T: Parse<'a> + SerializeT, - #[cfg(not(feature = "serde"))] T: Parse<'a>, -> Parse<'a> for ComponentTypeUse<'a, T> { +impl< + 'a, + #[cfg(feature = "serde")] T: Parse<'a> + SerializeT, + #[cfg(not(feature = "serde"))] T: Parse<'a>, + > Parse<'a> for ComponentTypeUse<'a, T> +{ fn parse(parser: Parser<'a>) -> Result { if parser.peek::()? && parser.peek2::>()? { Ok(Self::Ref(parser.parens(|parser| parser.parse())?)) @@ -1065,10 +1108,9 @@ impl<'a, } } -impl< - #[cfg(feature = "serde")] T: SerializeT, - #[cfg(not(feature = "serde"))] T, -> Default for ComponentTypeUse<'_, T> { +impl<#[cfg(feature = "serde")] T: SerializeT, #[cfg(not(feature = "serde"))] T> Default + for ComponentTypeUse<'_, T> +{ fn default() -> Self { let span = Span::from_offset(0); Self::Ref(ItemRef { diff --git a/crates/wast/src/core/custom.rs b/crates/wast/src/core/custom.rs index f28dcee30c..dacf5a0877 100644 --- a/crates/wast/src/core/custom.rs +++ b/crates/wast/src/core/custom.rs @@ -2,11 +2,15 @@ use crate::parser::{Parse, Parser, Result}; use crate::token::{self, Span}; use crate::{annotation, kw}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// A custom section within a wasm module. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum Custom<'a> { /// A raw custom section with the manual placement and bytes specified. #[cfg_attr(feature = "serde", serde(borrow))] @@ -68,7 +72,11 @@ pub struct RawCustomSection<'a> { /// Possible locations to place a custom section within a module. #[derive(Debug, PartialEq, Copy, Clone)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum CustomPlace { /// This custom section will appear before the first section in the module. BeforeFirst, @@ -83,7 +91,11 @@ pub enum CustomPlace { /// Known sections that custom sections can be placed relative to. #[derive(Debug, PartialEq, Eq, Copy, Clone)] #[allow(missing_docs)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum CustomPlaceAnchor { Type, Import, @@ -262,7 +274,11 @@ pub struct Dylink0<'a> { /// Possible subsections of the `dylink.0` custom section #[derive(Debug)] #[allow(missing_docs)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum Dylink0Subsection<'a> { MemInfo { memory_size: u32, diff --git a/crates/wast/src/core/export.rs b/crates/wast/src/core/export.rs index 4479201d96..7a6916b276 100644 --- a/crates/wast/src/core/export.rs +++ b/crates/wast/src/core/export.rs @@ -2,7 +2,7 @@ use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Index, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// A entry in a WebAssembly module's export section. #[derive(Debug)] @@ -22,7 +22,11 @@ pub struct Export<'a> { /// contained in an [`Export`]. #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] #[allow(missing_docs)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ExportKind { Func, Table, diff --git a/crates/wast/src/core/expr.rs b/crates/wast/src/core/expr.rs index 3750e67c5a..95464be588 100644 --- a/crates/wast/src/core/expr.rs +++ b/crates/wast/src/core/expr.rs @@ -4,9 +4,9 @@ use crate::encode::Encode; use crate::kw; use crate::parser::{Cursor, Parse, Parser, Result}; use crate::token::*; -use std::mem; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; +use std::mem; /// An expression, or a list of instructions, in the WebAssembly text format. /// @@ -1200,7 +1200,11 @@ impl<'a> Parse<'a> for TryTable<'a> { #[derive(Debug)] #[allow(missing_docs)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum TryTableCatchKind<'a> { // Catch a tagged exception, do not capture an exnref. #[cfg_attr(feature = "serde", serde(borrow))] @@ -1832,7 +1836,11 @@ impl<'a> Parse<'a> for BrOnCastFail<'a> { /// Different ways to specify a `v128.const` instruction #[derive(Debug)] #[allow(missing_docs)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum V128Const { I8x16([i8; 16]), I16x8([i16; 8]), diff --git a/crates/wast/src/core/func.rs b/crates/wast/src/core/func.rs index 14f35e8fe2..1a716e19e0 100644 --- a/crates/wast/src/core/func.rs +++ b/crates/wast/src/core/func.rs @@ -3,7 +3,7 @@ use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, NameAnnotation, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// A WebAssembly function to be inserted into a module. /// @@ -31,7 +31,11 @@ pub struct Func<'a> { /// Possible ways to define a function in the text format. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum FuncKind<'a> { /// A function which is actually defined as an import, such as: /// diff --git a/crates/wast/src/core/global.rs b/crates/wast/src/core/global.rs index bf2dd944c6..add4a93c58 100644 --- a/crates/wast/src/core/global.rs +++ b/crates/wast/src/core/global.rs @@ -3,7 +3,7 @@ use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, NameAnnotation, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// A WebAssembly global in a module #[derive(Debug)] @@ -27,7 +27,11 @@ pub struct Global<'a> { /// Different kinds of globals that can be defined in a module. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum GlobalKind<'a> { /// A global which is actually defined as an import, such as: /// diff --git a/crates/wast/src/core/import.rs b/crates/wast/src/core/import.rs index 7552f8f50c..6f1a0b9b45 100644 --- a/crates/wast/src/core/import.rs +++ b/crates/wast/src/core/import.rs @@ -3,7 +3,7 @@ use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Id, NameAnnotation, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// An `import` statement and entry in a WebAssembly module. #[derive(Debug, Clone)] @@ -53,7 +53,11 @@ pub struct ItemSig<'a> { #[derive(Debug, Clone)] #[allow(missing_docs)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ItemKind<'a> { #[cfg_attr(feature = "serde", serde(borrow))] Func(TypeUse<'a, FunctionType<'a>>), diff --git a/crates/wast/src/core/memory.rs b/crates/wast/src/core/memory.rs index 5b406b1446..0ad782b849 100644 --- a/crates/wast/src/core/memory.rs +++ b/crates/wast/src/core/memory.rs @@ -3,7 +3,7 @@ use crate::kw; use crate::parser::{Lookahead1, Parse, Parser, Peek, Result}; use crate::token::*; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// A defined WebAssembly memory instance inside of a module. #[derive(Debug)] @@ -25,7 +25,11 @@ pub struct Memory<'a> { /// Different syntactical ways a memory can be defined in a module. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum MemoryKind<'a> { /// This memory is actually an inlined import definition. #[allow(missing_docs)] @@ -119,7 +123,11 @@ pub struct Data<'a> { /// Different kinds of data segments, either passive or active. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum DataKind<'a> { /// A passive data segment which isn't associated with a memory and is /// referenced from various instructions. @@ -219,7 +227,11 @@ impl<'a> Parse<'a> for Data<'a> { /// Differnet ways the value of a data segment can be defined. #[derive(Debug)] #[allow(missing_docs)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum DataVal<'a> { String(&'a [u8]), Integral(Vec), diff --git a/crates/wast/src/core/module.rs b/crates/wast/src/core/module.rs index 21c2d5f197..fcecd0f53f 100644 --- a/crates/wast/src/core/module.rs +++ b/crates/wast/src/core/module.rs @@ -3,7 +3,7 @@ use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, Index, NameAnnotation, Span}; use crate::{annotation, kw}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; pub use crate::core::resolve::Names; @@ -24,7 +24,11 @@ pub struct Module<'a> { /// The different kinds of ways to define a module. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ModuleKind<'a> { /// A module defined in the textual s-expression format. #[cfg_attr(feature = "serde", serde(borrow))] @@ -148,7 +152,11 @@ impl<'a> Parse<'a> for Module<'a> { /// A listing of all possible fields that can make up a WebAssembly module. #[allow(missing_docs)] #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ModuleField<'a> { #[cfg_attr(feature = "serde", serde(borrow))] Type(Type<'a>), diff --git a/crates/wast/src/core/table.rs b/crates/wast/src/core/table.rs index 1479f5d8fa..85847bf244 100644 --- a/crates/wast/src/core/table.rs +++ b/crates/wast/src/core/table.rs @@ -3,7 +3,7 @@ use crate::kw; use crate::parser::{Parse, Parser, Peek, Result}; use crate::token::{Id, Index, LParen, NameAnnotation, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// A WebAssembly `table` directive in a module. #[derive(Debug)] @@ -25,7 +25,11 @@ pub struct Table<'a> { /// Different ways to textually define a table. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum TableKind<'a> { /// This table is actually an inlined import definition. #[allow(missing_docs)] @@ -123,7 +127,11 @@ pub struct Elem<'a> { /// Different ways to define an element segment in an mdoule. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ElemKind<'a> { /// A passive segment that isn't associated with a table and can be used in /// various bulk-memory instructions. @@ -145,7 +153,11 @@ pub enum ElemKind<'a> { /// Different ways to define the element segment payload in a module. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ElemPayload<'a> { /// This element segment has a contiguous list of function indices #[cfg_attr(feature = "serde", serde(borrow))] diff --git a/crates/wast/src/core/tag.rs b/crates/wast/src/core/tag.rs index 1aaf111240..faa3adc7d1 100644 --- a/crates/wast/src/core/tag.rs +++ b/crates/wast/src/core/tag.rs @@ -3,7 +3,7 @@ use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::{Id, NameAnnotation, Span}; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// A WebAssembly tag directive, part of the exception handling proposal. #[derive(Debug)] @@ -26,7 +26,11 @@ pub struct Tag<'a> { /// Listing of various types of tags that can be defined in a wasm module. #[derive(Clone, Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum TagType<'a> { /// An exception tag, where the payload is the type signature of the tag /// (constructor parameters, etc). @@ -36,7 +40,11 @@ pub enum TagType<'a> { /// Different kinds of tags that can be defined in a module. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum TagKind<'a> { /// An tag which is actually defined as an import, such as: /// diff --git a/crates/wast/src/core/types.rs b/crates/wast/src/core/types.rs index 58a949f3b7..434363b70a 100644 --- a/crates/wast/src/core/types.rs +++ b/crates/wast/src/core/types.rs @@ -2,14 +2,18 @@ use crate::core::*; use crate::kw; use crate::parser::{Cursor, Parse, Parser, Peek, Result}; use crate::token::{Id, Index, LParen, NameAnnotation, Span}; -use std::mem; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; +use std::mem; /// The value types for a wasm module. #[allow(missing_docs)] #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum ValType<'a> { I32, I64, @@ -63,7 +67,11 @@ impl<'a> Peek for ValType<'a> { /// A heap type for a reference type #[allow(missing_docs)] #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum HeapType<'a> { /// An untyped function reference: funcref. This is part of the reference /// types proposal. @@ -346,7 +354,11 @@ impl<'a> Peek for RefType<'a> { /// The types of values that may be used in a struct or array. #[allow(missing_docs)] #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum StorageType<'a> { I8, I16, @@ -467,7 +479,11 @@ impl<'a> Parse<'a> for TableType<'a> { /// Configuration for a memory of a wasm module #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum MemoryType { /// A 32-bit memory B32 { @@ -726,7 +742,11 @@ impl<'a> Parse<'a> for ExportType<'a> { /// A definition of a type. #[derive(Debug)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum TypeDef<'a> { /// A function type definition. #[cfg_attr(feature = "serde", serde(borrow))] diff --git a/crates/wast/src/wat.rs b/crates/wast/src/wat.rs index 9b4c6cd79a..4ad576f9fe 100644 --- a/crates/wast/src/wat.rs +++ b/crates/wast/src/wat.rs @@ -4,7 +4,7 @@ use crate::kw; use crate::parser::{Parse, Parser, Result}; use crate::token::Span; #[cfg(feature = "serde")] -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; /// A `*.wat` file parser, or a parser for one parenthesized module. /// @@ -13,7 +13,11 @@ use serde_derive::{Serialize, Deserialize}; /// of s-expressions that are module fields. #[derive(Debug)] #[allow(missing_docs)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(tag = "type", content = "val"))] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(tag = "type", content = "val") +)] pub enum Wat<'a> { #[cfg_attr(feature = "serde", serde(borrow))] Module(Module<'a>), From 67ba4964b782bd14c74aa00bd158bb11529b355a Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Fri, 1 Mar 2024 08:15:29 -0800 Subject: [PATCH 7/7] add cargo check --- .github/workflows/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c4e3a61521..db9c850b02 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -162,6 +162,7 @@ jobs: - run: cargo check --no-default-features -p wit-parser --features serde - run: cargo check --no-default-features -p wit-parser --features decoding - run: cargo check --no-default-features -p wit-parser --features serde,decoding,wat + - run: cargo check --no-default-features -p wast --features serde,wasm-module - run: | if cargo tree -p wasm-smith --no-default-features -e no-dev | grep wasmparser; then echo wasm-smith without default features should not depend on wasmparser