Skip to content

Commit

Permalink
Improve no_std imports for the entire workspace (#944)
Browse files Browse the repository at this point in the history
* improve no_std import structure in the workspace

* re-export alloc as std in no_std mode

* fix new clippy warnings for tests
  • Loading branch information
Robbepop authored Mar 3, 2024
1 parent a2f299a commit d6b27b4
Show file tree
Hide file tree
Showing 56 changed files with 74 additions and 108 deletions.
3 changes: 2 additions & 1 deletion crates/arena/src/component_vec.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::ArenaIndex;
use alloc::vec::Vec;
use core::{
fmt::{self, Debug},
marker::PhantomData,
ops::{Index, IndexMut},
};
use std::vec::Vec;

/// Stores components for entities backed by a [`Vec`].
pub struct ComponentVec<Idx, T> {
Expand Down Expand Up @@ -153,6 +153,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use std::{format, string::String};

/// Add `n` components and perform checks along the way.
fn add_components(vec: &mut ComponentVec<usize, String>, n: usize) {
Expand Down
2 changes: 1 addition & 1 deletion crates/arena/src/dedup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{Arena, ArenaIndex, Iter, IterMut};
use alloc::collections::BTreeMap;
use core::ops::{Index, IndexMut};
use std::collections::BTreeMap;

/// A deduplicating arena allocator with a given index and entity type.
///
Expand Down
12 changes: 7 additions & 5 deletions crates/arena/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! These allocators mainly serve as the backbone for an efficient Wasm store
//! implementation.
#![cfg_attr(not(feature = "std"), no_std)]
#![no_std]
#![warn(
clippy::cast_lossless,
clippy::missing_errors_doc,
Expand All @@ -16,10 +16,12 @@
clippy::map_unwrap_or,
clippy::items_after_statements
)]

#[cfg(not(feature = "std"))]
extern crate alloc;
extern crate alloc as std;

#[cfg(feature = "std")]
extern crate std as alloc;
extern crate std;

mod component_vec;
mod dedup;
Expand All @@ -29,13 +31,13 @@ mod guarded;
mod tests;

pub use self::{component_vec::ComponentVec, dedup::DedupArena, guarded::GuardedEntity};
use alloc::vec::Vec;
use core::{
iter::{DoubleEndedIterator, Enumerate, ExactSizeIterator},
iter::Enumerate,
marker::PhantomData,
ops::{Index, IndexMut},
slice,
};
use std::vec::Vec;

/// Types that can be used as indices for arenas.
pub trait ArenaIndex: Copy {
Expand Down
6 changes: 3 additions & 3 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![no_std]
#![warn(
clippy::cast_lossless,
clippy::missing_errors_doc,
Expand All @@ -19,10 +19,10 @@ mod untyped;
mod value;

#[cfg(not(feature = "std"))]
extern crate alloc;
extern crate alloc as std;

#[cfg(feature = "std")]
extern crate std as alloc;
extern crate std;

use self::value::{
ArithmeticOps,
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/trap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::HostError;
use alloc::{boxed::Box, string::String};
use core::fmt::{self, Display};
use std::{boxed::Box, string::String};

#[cfg(feature = "std")]
use std::error::Error as StdError;
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/bytecode/provider.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{AnyConst32, Register};
use crate::{engine::translator::TranslationError, Error};
use alloc::vec::{Drain, Vec};
use std::vec::{Drain, Vec};
use wasmi_core::UntypedValue;

#[cfg(doc)]
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/code_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::{
store::{Fuel, FuelError},
Error,
};
use alloc::boxed::Box;
use core::{
cell::UnsafeCell,
fmt,
Expand All @@ -23,6 +22,7 @@ use core::{
slice,
sync::atomic::{AtomicU8, Ordering},
};
use std::boxed::Box;
use wasmi_arena::{Arena, ArenaIndex};
use wasmi_core::TrapCode;
use wasmparser::{FuncToValidate, ValidatorResources};
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/executor/stack/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
engine::{bytecode::RegisterSpan, code_map::InstructionPtr},
Instance,
};
use alloc::vec::Vec;
use std::vec::Vec;
use wasmi_core::TrapCode;

#[cfg(doc)]
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/executor/stack/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
core::UntypedValue,
engine::{bytecode::Register, CompiledFuncEntity},
};
use alloc::vec::Vec;
use core::{fmt, fmt::Debug, iter, mem, ptr};
use std::{vec, vec::Vec};
use wasmi_core::TrapCode;

#[cfg(doc)]
Expand Down
6 changes: 3 additions & 3 deletions crates/wasmi/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ use crate::{
FuncType,
StoreContextMut,
};
use alloc::{
use core::sync::atomic::{AtomicU32, Ordering};
use spin::{Mutex, RwLock};
use std::{
sync::{Arc, Weak},
vec::Vec,
};
use core::sync::atomic::{AtomicU32, Ordering};
use spin::{Mutex, RwLock};
use wasmi_arena::{ArenaIndex, GuardedEntity};
use wasmparser::{FuncToValidate, FuncValidatorAllocations, ValidatorResources};

Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/translator/control_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
engine::bytecode::{Provider, ProviderSliceStack},
Error,
};
use alloc::vec::{Drain, Vec};
use std::vec::{Drain, Vec};

/// An acquired branch target.
#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions crates/wasmi/src/engine/translator/instr_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use crate::{
module::ModuleHeader,
Error,
};
use alloc::vec::{Drain, Vec};
use core::mem;
use std::vec::{Drain, Vec};
use wasmi_core::{UntypedValue, ValueType, F32};

/// A reference to an instruction of the partially
Expand Down Expand Up @@ -1404,7 +1404,7 @@ impl Instruction {
#[cfg(test)]
mod tests {
use super::*;
use crate::engine::{bytecode::RegisterSpan, translator::typed_value::TypedValue};
use crate::engine::translator::typed_value::TypedValue;

#[test]
fn has_overlapping_copies_works() {
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/translator/labels.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::Instr;
use crate::{engine::bytecode::BranchOffset, Error};
use alloc::vec::Vec;
use core::{
fmt::{self, Display},
slice::Iter as SliceIter,
};
use std::vec::Vec;

/// A label during the Wasmi compilation process.
#[derive(Debug, Copy, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ use crate::{
Error,
FuncType,
};
use alloc::vec::Vec;
use core::fmt;
use std::vec::Vec;
use wasmi_core::{TrapCode, UntypedValue, ValueType};
use wasmparser::{
BinaryReaderError,
Expand Down
4 changes: 2 additions & 2 deletions crates/wasmi/src/engine/translator/stack/consts.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::Register;
use crate::{core::UntypedValue, engine::TranslationError, Error};
use alloc::{
use core::{iter::Rev, slice::Iter as SliceIter};
use std::{
collections::{btree_map, BTreeMap},
vec::Vec,
};
use core::{iter::Rev, slice::Iter as SliceIter};

/// A pool of deduplicated function local constant values.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/translator/stack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
Error,
FuncType,
};
use alloc::vec::Vec;
use std::vec::Vec;
use wasmi_core::UntypedValue;

/// Typed inputs to Wasmi bytecode instructions.
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/translator/stack/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use ::core::iter;

use super::{RegisterAlloc, TypedValue};
use crate::{engine::bytecode::Register, Error};
use alloc::vec::Vec;
use smallvec::SmallVec;
use std::vec::Vec;

#[cfg(doc)]
use wasmi_core::UntypedValue;
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/translator/stack/register_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use crate::{
},
Error,
};
use alloc::collections::BTreeSet;
use core::{
cmp::{max, min},
num::NonZeroUsize,
};
use multi_stash::{Key, Key as StashKey, MultiStash};
use std::collections::BTreeSet;

#[cfg(doc)]
use crate::engine::translator::InstrEncoder;
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/translator/tests/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
Module,
};
use core::sync::atomic::Ordering;
use std::sync::atomic::AtomicBool;
use std::{boxed::Box, sync::atomic::AtomicBool, vec::Vec};
use wasmi_core::UntypedValue;

/// A test driver for translation tests.
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/translator/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
Engine,
Module,
};
use std::fmt::Display;
use std::{fmt::Display, format, vec::Vec};

/// Converts the `wat` string source into `wasm` encoded byte.
fn wat2wasm(wat: &str) -> Vec<u8> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use core::num::NonZeroU64;
use wasmi_core::TrapCode;

const WASM_OP: WasmOp = WasmOp::binary(WasmType::I64, "rem_u");
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/translator/tests/op/block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::engine::{
bytecode::{BranchOffset, BranchOffset16, RegisterSpan},
translator::tests::{display_wasm::DisplayValueType, wasm_type::WasmType},
translator::tests::wasm_type::WasmType,
};
use std::fmt::Display;

Expand Down
9 changes: 1 addition & 8 deletions crates/wasmi/src/engine/translator/tests/op/br.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
use super::*;
use crate::{
core::UntypedValue,
engine::{
bytecode::BranchOffset,
translator::tests::{
display_wasm::DisplayValueType,
driver::ExpectedFunc,
wasm_type::WasmType,
},
},
engine::{bytecode::BranchOffset, translator::tests::wasm_type::WasmType},
};
use core::fmt::Display;

Expand Down
7 changes: 2 additions & 5 deletions crates/wasmi/src/engine/translator/tests/op/br_if.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use super::*;
use crate::engine::{
bytecode::{BranchOffset, BranchOffset16, RegisterSpan},
translator::tests::{
display_wasm::DisplayValueType,
driver::ExpectedFunc,
wasm_type::WasmType,
},
translator::tests::wasm_type::WasmType,
};
use core::fmt::Display;
use std::vec::Vec;
use wasmi_core::UntypedValue;

#[test]
Expand Down
5 changes: 1 addition & 4 deletions crates/wasmi/src/engine/translator/tests/op/cmp_br.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use super::{wasm_type::WasmType, *};
use crate::{
core::ValueType,
engine::{
bytecode::{BranchOffset, BranchOffset16, GlobalIdx},
translator::tests::display_wasm::DisplayValueType,
},
engine::bytecode::{BranchOffset, BranchOffset16, GlobalIdx},
};
use std::fmt::{Debug, Display};

Expand Down
5 changes: 1 addition & 4 deletions crates/wasmi/src/engine/translator/tests/op/global_set.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use super::*;

use crate::engine::{
bytecode::GlobalIdx,
translator::tests::{display_wasm::DisplayValueType, driver::ExpectedFunc},
};
use crate::engine::bytecode::GlobalIdx;
use core::fmt::Display;
use wasm_type::WasmType;
use wasmi_core::ValueType;
Expand Down
1 change: 1 addition & 0 deletions crates/wasmi/src/engine/translator/tests/op/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use super::{
WasmOp,
WasmType,
};
use std::format;

/// Creates an [`Const32<i32>`] from the given `i32` value.
///
Expand Down
9 changes: 1 addition & 8 deletions crates/wasmi/src/engine/translator/tests/op/return_.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
use super::*;
use crate::engine::{
bytecode::RegisterSpan,
translator::tests::{
display_wasm::DisplayValueType,
driver::ExpectedFunc,
wasm_type::WasmType,
},
};
use crate::engine::{bytecode::RegisterSpan, translator::tests::wasm_type::WasmType};
use core::fmt::Display;

#[test]
Expand Down
9 changes: 1 addition & 8 deletions crates/wasmi/src/engine/translator/tests/op/select.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
use super::*;
use crate::{
core::ValueType,
engine::translator::tests::{
display_wasm::DisplayValueType,
driver::ExpectedFunc,
wasm_type::WasmType,
},
};
use crate::{core::ValueType, engine::translator::tests::wasm_type::WasmType};
use core::{fmt, fmt::Display};

/// Tells which kind of `select` instruction to test.
Expand Down
3 changes: 1 addition & 2 deletions crates/wasmi/src/engine/translator/tests/op/unary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ mod conversion;
mod op;

use super::*;
use crate::engine::translator::tests::driver::{ExpectedFunc, TranslationTest};
use std::fmt::Display;
use std::{fmt::Display, vec::Vec};
use wasm_type::WasmType;
use wasmi_core::{TrapCode, UntypedValue};

Expand Down
2 changes: 0 additions & 2 deletions crates/wasmi/src/engine/translator/tests/op/unary/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ mod f32_trunc {

mod f32_nearest {
use super::*;
use wasmi_core::UntypedValue;

const OP_NAME: &str = "nearest";

Expand Down Expand Up @@ -343,7 +342,6 @@ mod f64_trunc {

mod f64_nearest {
use super::*;
use wasmi_core::UntypedValue;

const OP_NAME: &str = "nearest";

Expand Down
Loading

0 comments on commit d6b27b4

Please sign in to comment.