From 90bd0f0589ef54a79ed00dc8be6dfa2088914421 Mon Sep 17 00:00:00 2001 From: Arsenii Lyashenko Date: Sat, 7 Dec 2024 04:09:45 +0300 Subject: [PATCH] Add `no_std` support to `wasm-encoder` (#1938) * Add `no_std` support to `wasm-encoder` * Manual fmt * Implement `Error` only with `std` enabled * Fix error implementation * Update MSRV comment --- Cargo.toml | 9 ++++--- crates/wasm-encoder/Cargo.toml | 5 +++- crates/wasm-encoder/build.rs | 27 +++++++++++++++++++ crates/wasm-encoder/src/component.rs | 1 + crates/wasm-encoder/src/component/aliases.rs | 1 + crates/wasm-encoder/src/component/builder.rs | 3 ++- .../wasm-encoder/src/component/canonicals.rs | 1 + .../wasm-encoder/src/component/components.rs | 1 + crates/wasm-encoder/src/component/exports.rs | 1 + crates/wasm-encoder/src/component/imports.rs | 1 + .../wasm-encoder/src/component/instances.rs | 1 + crates/wasm-encoder/src/component/modules.rs | 1 + crates/wasm-encoder/src/component/names.rs | 3 ++- crates/wasm-encoder/src/component/start.rs | 1 + crates/wasm-encoder/src/component/types.rs | 1 + crates/wasm-encoder/src/core.rs | 1 + crates/wasm-encoder/src/core/branch_hints.rs | 3 ++- crates/wasm-encoder/src/core/code.rs | 4 ++- crates/wasm-encoder/src/core/custom.rs | 5 ++-- crates/wasm-encoder/src/core/data.rs | 1 + crates/wasm-encoder/src/core/dump.rs | 6 +++-- crates/wasm-encoder/src/core/elements.rs | 3 ++- crates/wasm-encoder/src/core/exports.rs | 1 + crates/wasm-encoder/src/core/functions.rs | 1 + crates/wasm-encoder/src/core/globals.rs | 1 + crates/wasm-encoder/src/core/imports.rs | 1 + crates/wasm-encoder/src/core/linking.rs | 5 ++-- crates/wasm-encoder/src/core/memories.rs | 1 + crates/wasm-encoder/src/core/names.rs | 5 ++-- crates/wasm-encoder/src/core/producers.rs | 4 +-- crates/wasm-encoder/src/core/start.rs | 1 + crates/wasm-encoder/src/core/tables.rs | 1 + crates/wasm-encoder/src/core/tags.rs | 1 + crates/wasm-encoder/src/core/types.rs | 2 ++ crates/wasm-encoder/src/lib.rs | 8 ++++++ crates/wasm-encoder/src/raw.rs | 1 + crates/wasm-encoder/src/reencode.rs | 21 ++++++++++----- crates/wasm-encoder/src/reencode/component.rs | 3 +++ 38 files changed, 110 insertions(+), 27 deletions(-) create mode 100644 crates/wasm-encoder/build.rs diff --git a/Cargo.toml b/Cargo.toml index 8f1e1e93bc..40fcf796bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,8 +64,9 @@ version = "0.221.2" # Current policy for wasm-tools is the same as Wasmtime which is that this # number can be no larger than the current stable release of Rust minus 2. # -# NB: if this number increases to 1.81-or-later delete the -# `crates/wasmparser/build.rs` script as it's no longer necessary. +# NB: if this number increases to 1.81-or-later delete +# `crates/wasmparser/build.rs` and `crates/wasm-encoder/build.rs` scripts +# as they are no longer necessary. rust-version = "1.76.0" [workspace.dependencies] @@ -100,12 +101,12 @@ gimli = "0.30.0" id-arena = "2" wasm-compose = { version = "0.221.2", path = "crates/wasm-compose" } -wasm-encoder = { version = "0.221.2", path = "crates/wasm-encoder", default-features = false } +wasm-encoder = { version = "0.221.2", path = "crates/wasm-encoder", default-features = false, features = ["std"] } wasm-metadata = { version = "0.221.2", path = "crates/wasm-metadata" } wasm-mutate = { version = "0.221.2", path = "crates/wasm-mutate" } wasm-shrink = { version = "0.221.2", path = "crates/wasm-shrink" } wasm-smith = { version = "0.221.2", path = "crates/wasm-smith" } -wasmparser = { version = "0.221.2", path = "crates/wasmparser", default-features = false, features = ['std','simd'] } +wasmparser = { version = "0.221.2", path = "crates/wasmparser", default-features = false, features = ['std', 'simd'] } wasmprinter = { version = "0.221.2", path = "crates/wasmprinter", default-features = false } wast = { version = "221.0.2", path = "crates/wast", default-features = false } wat = { version = "1.221.2", path = "crates/wat", default-features = false } diff --git a/crates/wasm-encoder/Cargo.toml b/crates/wasm-encoder/Cargo.toml index eea73ba858..9332255e09 100644 --- a/crates/wasm-encoder/Cargo.toml +++ b/crates/wasm-encoder/Cargo.toml @@ -33,7 +33,10 @@ wasmparser = { path = "../wasmparser" } wasmprinter = { workspace = true } [features] -default = ['component-model'] +default = ['std', 'component-model'] + +# A feature which enables implementations of `std::error::Error` as appropriate. +std = [] # On-by-default: conditional support for emitting components in addition to # core modules. diff --git a/crates/wasm-encoder/build.rs b/crates/wasm-encoder/build.rs new file mode 100644 index 0000000000..305853b591 --- /dev/null +++ b/crates/wasm-encoder/build.rs @@ -0,0 +1,27 @@ +use std::process::Command; +use std::str; + +fn main() { + // Temporary check to see if the rustc version >= 1.81 in which case the + // `Error` trait is always available. This is temporary because in the + // future the MSRV of this crate will be beyond 1.81 in which case this + // build script can be deleted. + let minor = rustc_minor_version().unwrap_or(0); + if minor >= 81 { + println!("cargo:rustc-cfg=core_error"); + } + if minor >= 80 { + println!("cargo:rustc-check-cfg=cfg(core_error)"); + } +} + +fn rustc_minor_version() -> Option { + let rustc = std::env::var("RUSTC").unwrap(); + let output = Command::new(rustc).arg("--version").output().ok()?; + let version = str::from_utf8(&output.stdout).ok()?; + let mut pieces = version.split('.'); + if pieces.next() != Some("rustc 1") { + return None; + } + pieces.next()?.parse().ok() +} diff --git a/crates/wasm-encoder/src/component.rs b/crates/wasm-encoder/src/component.rs index e3f36a88a2..03e441bdaa 100644 --- a/crates/wasm-encoder/src/component.rs +++ b/crates/wasm-encoder/src/component.rs @@ -23,6 +23,7 @@ pub use self::start::*; pub use self::types::*; use crate::{CustomSection, Encode, ProducersSection, RawCustomSection}; +use alloc::vec::Vec; // Core sorts extended by the component model const CORE_TYPE_SORT: u8 = 0x10; diff --git a/crates/wasm-encoder/src/component/aliases.rs b/crates/wasm-encoder/src/component/aliases.rs index 1e317fb0e2..07a93d0737 100644 --- a/crates/wasm-encoder/src/component/aliases.rs +++ b/crates/wasm-encoder/src/component/aliases.rs @@ -2,6 +2,7 @@ use super::{COMPONENT_SORT, CORE_MODULE_SORT, CORE_SORT, CORE_TYPE_SORT, TYPE_SO use crate::{ encode_section, ComponentExportKind, ComponentSection, ComponentSectionId, Encode, ExportKind, }; +use alloc::vec::Vec; /// Represents the kinds of outer aliasable items in a component. #[derive(Clone, Copy, Debug, Eq, PartialEq)] diff --git a/crates/wasm-encoder/src/component/builder.rs b/crates/wasm-encoder/src/component/builder.rs index d587ae99f5..c5d2deebf4 100644 --- a/crates/wasm-encoder/src/component/builder.rs +++ b/crates/wasm-encoder/src/component/builder.rs @@ -1,6 +1,7 @@ use crate::component::*; use crate::{ExportKind, Module, RawSection, ValType}; -use std::mem; +use alloc::vec::Vec; +use core::mem; /// Convenience type to build a component incrementally and automatically keep /// track of index spaces. diff --git a/crates/wasm-encoder/src/component/canonicals.rs b/crates/wasm-encoder/src/component/canonicals.rs index 8bb4ad2c89..3407d5dd89 100644 --- a/crates/wasm-encoder/src/component/canonicals.rs +++ b/crates/wasm-encoder/src/component/canonicals.rs @@ -1,4 +1,5 @@ use crate::{encode_section, ComponentSection, ComponentSectionId, Encode}; +use alloc::vec::Vec; /// Represents options for canonical function definitions. #[derive(Debug, Clone, Copy, PartialEq, Eq)] diff --git a/crates/wasm-encoder/src/component/components.rs b/crates/wasm-encoder/src/component/components.rs index c08645e3b9..ad072a3a83 100644 --- a/crates/wasm-encoder/src/component/components.rs +++ b/crates/wasm-encoder/src/component/components.rs @@ -1,4 +1,5 @@ use crate::{Component, ComponentSection, ComponentSectionId, Encode}; +use alloc::vec::Vec; /// An encoder for the component section of WebAssembly components. /// diff --git a/crates/wasm-encoder/src/component/exports.rs b/crates/wasm-encoder/src/component/exports.rs index 4d988e574b..314f9b31ed 100644 --- a/crates/wasm-encoder/src/component/exports.rs +++ b/crates/wasm-encoder/src/component/exports.rs @@ -3,6 +3,7 @@ use super::{ VALUE_SORT, }; use crate::{encode_section, ComponentSection, ComponentSectionId, ComponentTypeRef, Encode}; +use alloc::vec::Vec; /// Represents the kind of an export from a WebAssembly component. #[derive(Clone, Copy, Debug, Eq, PartialEq)] diff --git a/crates/wasm-encoder/src/component/imports.rs b/crates/wasm-encoder/src/component/imports.rs index b818a17814..9bd6716895 100644 --- a/crates/wasm-encoder/src/component/imports.rs +++ b/crates/wasm-encoder/src/component/imports.rs @@ -2,6 +2,7 @@ use crate::{ encode_section, ComponentExportKind, ComponentSection, ComponentSectionId, ComponentValType, Encode, }; +use alloc::vec::Vec; /// Represents the possible type bounds for type references. #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] diff --git a/crates/wasm-encoder/src/component/instances.rs b/crates/wasm-encoder/src/component/instances.rs index fd3fc3c3af..c7f26987bf 100644 --- a/crates/wasm-encoder/src/component/instances.rs +++ b/crates/wasm-encoder/src/component/instances.rs @@ -2,6 +2,7 @@ use super::CORE_INSTANCE_SORT; use crate::{ encode_section, ComponentExportKind, ComponentSection, ComponentSectionId, Encode, ExportKind, }; +use alloc::vec::Vec; /// Represents an argument to a module instantiation. #[derive(Clone, Copy, Debug, Eq, PartialEq)] diff --git a/crates/wasm-encoder/src/component/modules.rs b/crates/wasm-encoder/src/component/modules.rs index 437cd353af..ee43e486f7 100644 --- a/crates/wasm-encoder/src/component/modules.rs +++ b/crates/wasm-encoder/src/component/modules.rs @@ -1,4 +1,5 @@ use crate::{ComponentSection, ComponentSectionId, Encode, Module}; +use alloc::vec::Vec; /// An encoder for the module section of WebAssembly components. /// diff --git a/crates/wasm-encoder/src/component/names.rs b/crates/wasm-encoder/src/component/names.rs index f09b2152b1..bd2df42c01 100644 --- a/crates/wasm-encoder/src/component/names.rs +++ b/crates/wasm-encoder/src/component/names.rs @@ -1,4 +1,5 @@ -use std::borrow::Cow; +use alloc::borrow::Cow; +use alloc::vec::Vec; use super::*; use crate::{encoding_size, ExportKind, NameMap, SectionId}; diff --git a/crates/wasm-encoder/src/component/start.rs b/crates/wasm-encoder/src/component/start.rs index d4b60e1dfc..7a68de9ea0 100644 --- a/crates/wasm-encoder/src/component/start.rs +++ b/crates/wasm-encoder/src/component/start.rs @@ -1,4 +1,5 @@ use crate::{ComponentSection, ComponentSectionId, Encode}; +use alloc::vec::Vec; /// An encoder for the start section of WebAssembly components. /// diff --git a/crates/wasm-encoder/src/component/types.rs b/crates/wasm-encoder/src/component/types.rs index 39853d5f7b..ccb45447c4 100644 --- a/crates/wasm-encoder/src/component/types.rs +++ b/crates/wasm-encoder/src/component/types.rs @@ -3,6 +3,7 @@ use crate::{ encode_section, Alias, ComponentExportKind, ComponentOuterAliasKind, ComponentSection, ComponentSectionId, ComponentTypeRef, CoreTypeEncoder, Encode, EntityType, ValType, }; +use alloc::vec::Vec; /// Represents the type of a core module. #[derive(Debug, Clone, Default)] diff --git a/crates/wasm-encoder/src/core.rs b/crates/wasm-encoder/src/core.rs index f671a05925..48ce15e7b8 100644 --- a/crates/wasm-encoder/src/core.rs +++ b/crates/wasm-encoder/src/core.rs @@ -37,6 +37,7 @@ pub use tags::*; pub use types::*; use crate::Encode; +use alloc::vec::Vec; pub(crate) const CORE_FUNCTION_SORT: u8 = 0x00; pub(crate) const CORE_TABLE_SORT: u8 = 0x01; diff --git a/crates/wasm-encoder/src/core/branch_hints.rs b/crates/wasm-encoder/src/core/branch_hints.rs index 0f325377ef..96231b11e9 100644 --- a/crates/wasm-encoder/src/core/branch_hints.rs +++ b/crates/wasm-encoder/src/core/branch_hints.rs @@ -1,5 +1,6 @@ use crate::{CustomSection, Encode, Section, SectionId}; -use std::borrow::Cow; +use alloc::borrow::Cow; +use alloc::vec::Vec; /// Helper structure to encode the `metadata.code.branch_hint` custom section. /// diff --git a/crates/wasm-encoder/src/core/code.rs b/crates/wasm-encoder/src/core/code.rs index c2bcdafd6a..0b544fe914 100644 --- a/crates/wasm-encoder/src/core/code.rs +++ b/crates/wasm-encoder/src/core/code.rs @@ -1,5 +1,7 @@ use crate::{encode_section, Encode, HeapType, RefType, Section, SectionId, ValType}; -use std::borrow::Cow; +use alloc::borrow::Cow; +use alloc::vec; +use alloc::vec::Vec; /// An encoder for the code section. /// diff --git a/crates/wasm-encoder/src/core/custom.rs b/crates/wasm-encoder/src/core/custom.rs index 1fbeb10223..552011550a 100644 --- a/crates/wasm-encoder/src/core/custom.rs +++ b/crates/wasm-encoder/src/core/custom.rs @@ -1,6 +1,6 @@ -use std::borrow::Cow; - use crate::{encoding_size, Encode, Section, SectionId}; +use alloc::borrow::Cow; +use alloc::vec::Vec; /// A custom section holding arbitrary data. #[derive(Clone, Debug, PartialEq)] @@ -47,6 +47,7 @@ impl Section for RawCustomSection<'_> { #[cfg(test)] mod tests { use super::*; + use alloc::vec; #[test] fn test_custom_section() { diff --git a/crates/wasm-encoder/src/core/data.rs b/crates/wasm-encoder/src/core/data.rs index 6f408befe5..9d7ac752a3 100644 --- a/crates/wasm-encoder/src/core/data.rs +++ b/crates/wasm-encoder/src/core/data.rs @@ -1,4 +1,5 @@ use crate::{encode_section, encoding_size, ConstExpr, Encode, Section, SectionId}; +use alloc::vec::Vec; /// An encoder for the data section. /// diff --git a/crates/wasm-encoder/src/core/dump.rs b/crates/wasm-encoder/src/core/dump.rs index f9c01d9e1e..5eec139f39 100644 --- a/crates/wasm-encoder/src/core/dump.rs +++ b/crates/wasm-encoder/src/core/dump.rs @@ -1,6 +1,8 @@ -use std::borrow::Cow; - use crate::{CustomSection, Encode, Section}; +use alloc::borrow::Cow; +use alloc::string::String; +use alloc::vec; +use alloc::vec::Vec; /// The "core" custom section for coredumps, as described in the /// [tool-conventions diff --git a/crates/wasm-encoder/src/core/elements.rs b/crates/wasm-encoder/src/core/elements.rs index e44022e7fd..b3a3b5139d 100644 --- a/crates/wasm-encoder/src/core/elements.rs +++ b/crates/wasm-encoder/src/core/elements.rs @@ -1,5 +1,6 @@ use crate::{encode_section, ConstExpr, Encode, RefType, Section, SectionId}; -use std::borrow::Cow; +use alloc::borrow::Cow; +use alloc::vec::Vec; /// An encoder for the element section. /// diff --git a/crates/wasm-encoder/src/core/exports.rs b/crates/wasm-encoder/src/core/exports.rs index 7d78220e28..a9422952a1 100644 --- a/crates/wasm-encoder/src/core/exports.rs +++ b/crates/wasm-encoder/src/core/exports.rs @@ -2,6 +2,7 @@ use super::{ CORE_FUNCTION_SORT, CORE_GLOBAL_SORT, CORE_MEMORY_SORT, CORE_TABLE_SORT, CORE_TAG_SORT, }; use crate::{encode_section, Encode, Section, SectionId}; +use alloc::vec::Vec; /// Represents the kind of an export from a WebAssembly module. #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] diff --git a/crates/wasm-encoder/src/core/functions.rs b/crates/wasm-encoder/src/core/functions.rs index e21d8c10a7..063b48953e 100644 --- a/crates/wasm-encoder/src/core/functions.rs +++ b/crates/wasm-encoder/src/core/functions.rs @@ -1,4 +1,5 @@ use crate::{encode_section, Encode, Section, SectionId}; +use alloc::vec::Vec; /// An encoder for the function section of WebAssembly modules. /// diff --git a/crates/wasm-encoder/src/core/globals.rs b/crates/wasm-encoder/src/core/globals.rs index dcd96ac33f..ae1ba8789c 100644 --- a/crates/wasm-encoder/src/core/globals.rs +++ b/crates/wasm-encoder/src/core/globals.rs @@ -1,4 +1,5 @@ use crate::{encode_section, ConstExpr, Encode, Section, SectionId, ValType}; +use alloc::vec::Vec; /// An encoder for the global section. /// diff --git a/crates/wasm-encoder/src/core/imports.rs b/crates/wasm-encoder/src/core/imports.rs index b3a0879da1..7db71ba4d0 100644 --- a/crates/wasm-encoder/src/core/imports.rs +++ b/crates/wasm-encoder/src/core/imports.rs @@ -2,6 +2,7 @@ use crate::{ encode_section, Encode, GlobalType, MemoryType, Section, SectionId, TableType, TagType, CORE_FUNCTION_SORT, CORE_GLOBAL_SORT, CORE_MEMORY_SORT, CORE_TABLE_SORT, CORE_TAG_SORT, }; +use alloc::vec::Vec; /// The type of an entity. #[derive(Debug, Clone, Copy, PartialEq, Eq)] diff --git a/crates/wasm-encoder/src/core/linking.rs b/crates/wasm-encoder/src/core/linking.rs index 9a2995721f..e75034c929 100644 --- a/crates/wasm-encoder/src/core/linking.rs +++ b/crates/wasm-encoder/src/core/linking.rs @@ -1,6 +1,7 @@ -use std::borrow::Cow; - use crate::{encode_section, CustomSection, Encode, Section, SectionId}; +use alloc::borrow::Cow; +use alloc::vec; +use alloc::vec::Vec; const VERSION: u32 = 2; diff --git a/crates/wasm-encoder/src/core/memories.rs b/crates/wasm-encoder/src/core/memories.rs index d0c9087fff..40395306d0 100644 --- a/crates/wasm-encoder/src/core/memories.rs +++ b/crates/wasm-encoder/src/core/memories.rs @@ -1,4 +1,5 @@ use crate::{encode_section, Encode, Section, SectionId}; +use alloc::vec::Vec; /// An encoder for the memory section. /// diff --git a/crates/wasm-encoder/src/core/names.rs b/crates/wasm-encoder/src/core/names.rs index 37f390d2e4..b3b96c8189 100644 --- a/crates/wasm-encoder/src/core/names.rs +++ b/crates/wasm-encoder/src/core/names.rs @@ -1,6 +1,7 @@ -use std::borrow::Cow; - use crate::{encoding_size, CustomSection, Encode, Section, SectionId}; +use alloc::borrow::Cow; +use alloc::vec; +use alloc::vec::Vec; /// An encoder for the custom `name` section. /// diff --git a/crates/wasm-encoder/src/core/producers.rs b/crates/wasm-encoder/src/core/producers.rs index 00b3853f9c..38e7f0dab0 100644 --- a/crates/wasm-encoder/src/core/producers.rs +++ b/crates/wasm-encoder/src/core/producers.rs @@ -1,6 +1,6 @@ -use std::borrow::Cow; - use crate::{CustomSection, Encode, Section, SectionId}; +use alloc::borrow::Cow; +use alloc::vec::Vec; /// An encoder for the [producers custom /// section](https://github.com/WebAssembly/tool-conventions/blob/main/ProducersSection.md). diff --git a/crates/wasm-encoder/src/core/start.rs b/crates/wasm-encoder/src/core/start.rs index 8f12c0d0ef..22294a1952 100644 --- a/crates/wasm-encoder/src/core/start.rs +++ b/crates/wasm-encoder/src/core/start.rs @@ -1,4 +1,5 @@ use crate::{encoding_size, Encode, Section, SectionId}; +use alloc::vec::Vec; /// An encoder for the start section of WebAssembly modules. /// diff --git a/crates/wasm-encoder/src/core/tables.rs b/crates/wasm-encoder/src/core/tables.rs index 3bb2d58e3a..6df54bc145 100644 --- a/crates/wasm-encoder/src/core/tables.rs +++ b/crates/wasm-encoder/src/core/tables.rs @@ -1,4 +1,5 @@ use crate::{encode_section, ConstExpr, Encode, RefType, Section, SectionId, ValType}; +use alloc::vec::Vec; /// An encoder for the table section. /// diff --git a/crates/wasm-encoder/src/core/tags.rs b/crates/wasm-encoder/src/core/tags.rs index 413055f2af..008d40ebc2 100644 --- a/crates/wasm-encoder/src/core/tags.rs +++ b/crates/wasm-encoder/src/core/tags.rs @@ -1,4 +1,5 @@ use crate::{encode_section, Encode, Section, SectionId}; +use alloc::vec::Vec; /// An encoder for the tag section. /// diff --git a/crates/wasm-encoder/src/core/types.rs b/crates/wasm-encoder/src/core/types.rs index 5dda818f37..e9611849d5 100644 --- a/crates/wasm-encoder/src/core/types.rs +++ b/crates/wasm-encoder/src/core/types.rs @@ -1,4 +1,6 @@ use crate::{encode_section, Encode, Section, SectionId}; +use alloc::boxed::Box; +use alloc::vec::Vec; /// Represents a subtype of possible other types in a WebAssembly module. #[derive(Debug, Clone)] diff --git a/crates/wasm-encoder/src/lib.rs b/crates/wasm-encoder/src/lib.rs index 5b436b3f8b..395e5cff55 100644 --- a/crates/wasm-encoder/src/lib.rs +++ b/crates/wasm-encoder/src/lib.rs @@ -69,8 +69,14 @@ //! ``` #![cfg_attr(docsrs, feature(doc_auto_cfg))] +#![no_std] #![deny(missing_docs, missing_debug_implementations)] +extern crate alloc; +#[cfg(feature = "std")] +#[macro_use] +extern crate std; + #[cfg(feature = "component-model")] mod component; mod core; @@ -83,6 +89,8 @@ pub use self::component::*; pub use self::core::*; pub use self::raw::*; +use alloc::vec::Vec; + /// Implemented by types that can be encoded into a byte sink. pub trait Encode { /// Encode the type into the given byte sink. diff --git a/crates/wasm-encoder/src/raw.rs b/crates/wasm-encoder/src/raw.rs index e7b4a5b5b8..ae761d4eb9 100644 --- a/crates/wasm-encoder/src/raw.rs +++ b/crates/wasm-encoder/src/raw.rs @@ -1,4 +1,5 @@ use crate::{Encode, Section}; +use alloc::vec::Vec; /// A section made up of uninterpreted, raw bytes. /// diff --git a/crates/wasm-encoder/src/reencode.rs b/crates/wasm-encoder/src/reencode.rs index b85c406166..1d64d8f7be 100644 --- a/crates/wasm-encoder/src/reencode.rs +++ b/crates/wasm-encoder/src/reencode.rs @@ -3,8 +3,13 @@ //! The [`RoundtripReencoder`] allows encoding identical wasm to the parsed //! input. +#[cfg(all(not(feature = "std"), core_error))] +use core::error::Error as StdError; +#[cfg(feature = "std")] +use std::error::Error as StdError; + use crate::CoreTypeEncoder; -use std::convert::Infallible; +use core::convert::Infallible; #[cfg(feature = "component-model")] mod component; @@ -546,8 +551,8 @@ impl From for Error { } } -impl std::fmt::Display for Error { - fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { +impl core::fmt::Display for Error { + fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result { match self { Self::ParseError(_e) => { write!(fmt, "There was an error when parsing") @@ -574,8 +579,9 @@ impl std::fmt::Display for Error { } } -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { +#[cfg(any(feature = "std", core_error))] +impl StdError for Error { + fn source(&self) -> Option<&(dyn StdError + 'static)> { match self { Self::ParseError(e) => Some(e), Self::UserError(e) => Some(e), @@ -602,7 +608,8 @@ impl Reencode for RoundtripReencoder { pub mod utils { use super::{Error, Reencode}; use crate::{CoreTypeEncoder, Encode}; - use std::ops::Range; + use alloc::vec::Vec; + use core::ops::Range; pub fn parse_core_module( reencoder: &mut T, @@ -616,7 +623,7 @@ pub mod utils { last_section: &mut Option, next_section: Option, ) -> Result<(), Error> { - let after = std::mem::replace(last_section, next_section); + let after = core::mem::replace(last_section, next_section); let before = next_section; reencoder.intersperse_section_hook(module, after, before) } diff --git a/crates/wasm-encoder/src/reencode/component.rs b/crates/wasm-encoder/src/reencode/component.rs index d1c4d369e9..f8135e6822 100644 --- a/crates/wasm-encoder/src/reencode/component.rs +++ b/crates/wasm-encoder/src/reencode/component.rs @@ -1,4 +1,5 @@ use crate::reencode::{Error, Reencode, RoundtripReencoder}; +use alloc::boxed::Box; #[allow(missing_docs)] // FIXME pub trait ReencodeComponent: Reencode { @@ -386,6 +387,8 @@ pub mod component_utils { use super::super::utils::name_map; use super::ReencodeComponent; use crate::reencode::Error; + use alloc::boxed::Box; + use alloc::vec::Vec; pub fn parse_component( reencoder: &mut T,