Skip to content

Commit

Permalink
Add anyhow stuff to our internal wasmtime crate prelude (#8804)
Browse files Browse the repository at this point in the history
* Add `anyhow` stuff to our internal `wasmtime` crate prelude

We use it basically everywhere and it is annoying to have to import.

I also did an audit of existing `use` statements and removed the now-redundant
ones and replaced one-off imports with usage of the prelude, so that the prelude
is available by default in more places.

* Fix `cargo doc`
  • Loading branch information
fitzgen authored Jun 14, 2024
1 parent 3bcbd5e commit 1512a95
Show file tree
Hide file tree
Showing 89 changed files with 40 additions and 113 deletions.
1 change: 0 additions & 1 deletion crates/wasmtime/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use crate::prelude::*;
use crate::Engine;
use anyhow::{Context, Result};
use std::{
any::Any,
borrow::Cow,
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/compile/code_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::prelude::*;
use crate::Engine;
use anyhow::{anyhow, bail, Context, Result};
use std::borrow::Cow;
use std::path::Path;

Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/compile/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::compile::HashedEngineCompileEnv;
#[cfg(feature = "component-model")]
use crate::component::Component;
use crate::prelude::*;
use crate::runtime::vm::MmapVec;
use crate::{CodeBuilder, CodeMemory, Engine, Module};
use anyhow::{Context, Error, Result};
use object::write::WritableBuffer;
use std::sync::Arc;
use wasmtime_environ::{FinishedObject, ObjectBuilder, ObjectKind};
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::prelude::*;
use alloc::sync::Arc;
use anyhow::{bail, ensure, Result};
use core::fmt;
use core::str::FromStr;
use hashbrown::{HashMap, HashSet};
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::runtime::vm::GcRuntime;
use crate::sync::OnceLock;
use crate::Config;
use alloc::sync::Arc;
use anyhow::{Context, Result};
use core::sync::atomic::{AtomicU64, Ordering};
#[cfg(any(feature = "cranelift", feature = "winch"))]
use object::write::{Object, StandardSegment};
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/engine/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use crate::prelude::*;
use crate::{Engine, ModuleVersionStrategy, Precompiled};
use anyhow::{anyhow, bail, ensure, Context, Result};
use core::str::FromStr;
use object::endian::NativeEndian;
#[cfg(any(feature = "cranelift", feature = "winch"))]
Expand Down
7 changes: 6 additions & 1 deletion crates/wasmtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,11 @@
extern crate std;
extern crate alloc;

use wasmtime_environ::prelude;
pub(crate) mod prelude {
pub use crate::{Error, Result};
pub use anyhow::{anyhow, bail, ensure, format_err, Context};
pub use wasmtime_environ::prelude::*;
}

/// A helper macro to safely map `MaybeUninit<T>` to `MaybeUninit<U>` where `U`
/// is a field projection within `T`.
Expand Down Expand Up @@ -375,6 +379,7 @@ use sync_nostd as sync;
///
/// This type can be used to interact with `wasmtimes`'s extensive use
/// of `anyhow::Error` while still not directly depending on `anyhow`.
///
/// This type alias is identical to `anyhow::Result`.
#[doc(no_inline)]
pub use anyhow::{Error, Result};
Expand Down
2 changes: 0 additions & 2 deletions crates/wasmtime/src/profiling_agent.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::prelude::*;
#[allow(unused)]
use anyhow::{bail, Result};

cfg_if::cfg_if! {
if #[cfg(all(feature = "profiling", target_os = "linux"))] {
Expand Down
4 changes: 1 addition & 3 deletions crates/wasmtime/src/profiling_agent/jitdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
use crate::prelude::*;
use crate::profiling_agent::ProfilingAgent;
use anyhow::Result;
use object::elf;
use std::process;
use std::sync::Mutex;
use target_lexicon::Architecture;
use wasmtime_jit_debug::perf_jitdump::*;

use object::elf;

/// Interface for driving the creation of jitdump files
struct JitDumpAgent {
pid: u32,
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/profiling_agent/perfmap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::prelude::*;
use crate::profiling_agent::ProfilingAgent;
use anyhow::Result;
use std::io::{self, BufWriter, Write};
use std::process;
use std::{fs::File, sync::Mutex};
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/profiling_agent/vtune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use crate::prelude::*;
use crate::profiling_agent::ProfilingAgent;
use anyhow::Result;
use ittapi::jit::MethodLoadBuilder;
use std::sync::Mutex;

Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/code_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use crate::prelude::*;
use crate::runtime::vm::{libcalls, MmapVec, UnwindRegistration};
use anyhow::{anyhow, bail, Context, Result};
use core::mem::ManuallyDrop;
use core::ops::Range;
use object::endian::NativeEndian;
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/component/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::{
};
use crate::{FuncType, ValType};
use alloc::sync::Arc;
use anyhow::Result;
use core::any::Any;
use core::mem;
use core::ops::Range;
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/component/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::runtime::vm::{Export, ExportFunction};
use crate::store::{StoreOpaque, Stored};
use crate::{AsContext, AsContextMut, StoreContextMut, ValRaw};
use alloc::sync::Arc;
use anyhow::{bail, Context, Result};
use core::mem::{self, MaybeUninit};
use core::ptr::NonNull;
use wasmtime_environ::component::{
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/component/func/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::runtime::vm::component::{
use crate::runtime::vm::{VMFuncRef, VMMemoryDefinition, VMOpaqueContext};
use crate::{AsContextMut, StoreContextMut, ValRaw};
use alloc::sync::Arc;
use anyhow::{bail, Context, Result};
use core::any::Any;
use core::mem::{self, MaybeUninit};
use core::ptr::NonNull;
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/component/func/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::runtime::vm::{VMFuncRef, VMMemoryDefinition};
use crate::store::{StoreId, StoreOpaque};
use crate::{FuncType, StoreContextMut};
use alloc::sync::Arc;
use anyhow::{bail, Result};
use core::ptr::NonNull;
use wasmtime_environ::component::{ComponentTypes, StringEncoding, TypeResourceTableIndex};

Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/component/func/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::runtime::vm::SendSyncPtr;
use crate::{AsContextMut, StoreContext, StoreContextMut, ValRaw};
use alloc::borrow::Cow;
use alloc::sync::Arc;
use anyhow::{anyhow, bail, Context, Result};
use core::fmt;
use core::marker;
use core::mem::{self, MaybeUninit};
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/component/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::runtime::vm::VMFuncRef;
use crate::store::{StoreOpaque, Stored};
use crate::{AsContextMut, Module, StoreContextMut};
use alloc::sync::Arc;
use anyhow::{anyhow, Context, Result};
use core::marker;
use core::ptr::{self, NonNull};
use wasmtime_environ::{component::*, EngineOrModuleTypeIndex};
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/component/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::component::{
use crate::prelude::*;
use crate::{AsContextMut, Engine, Module, StoreContextMut};
use alloc::sync::Arc;
use anyhow::{bail, Context, Result};
use core::future::Future;
use core::marker;
use core::pin::Pin;
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/component/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::runtime::vm::component::ComponentInstance;
use crate::types::matching;
use crate::Module;
use alloc::sync::Arc;
use anyhow::{anyhow, bail, Context, Result};
use core::any::Any;
use wasmtime_environ::component::{
ComponentTypes, ResourceIndex, TypeComponentInstance, TypeDef, TypeFuncIndex, TypeModule,
Expand Down
6 changes: 6 additions & 0 deletions crates/wasmtime/src/runtime/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,9 @@ pub use wasmtime_component_macro::flags;

#[cfg(any(docsrs, test, doctest))]
pub mod bindgen_examples;

// NB: needed for the links in the docs above to work in all `cargo doc`
// configurations and avoid errors.
#[cfg(not(any(docsrs, test, doctest)))]
#[doc(hidden)]
pub mod bindgen_examples {}
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/component/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::runtime::vm::component::{ComponentInstance, InstanceFlags, ResourceTa
use crate::runtime::vm::{SendSyncPtr, VMFuncRef, ValRaw};
use crate::store::{StoreId, StoreOpaque};
use crate::{AsContextMut, StoreContextMut, Trap};
use anyhow::{bail, ensure, Result};
use core::any::TypeId;
use core::fmt;
use core::marker;
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/component/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::component::func::{desc, Lift, LiftContext, Lower, LowerContext};
use crate::component::ResourceAny;
use crate::prelude::*;
use crate::ValRaw;
use anyhow::{anyhow, bail, Result};
use core::mem::MaybeUninit;
use core::slice::{Iter, IterMut};
use wasmtime_component_util::{DiscriminantSize, FlagsSize};
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/debug.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::prelude::*;
use anyhow::{anyhow, bail, ensure, Error};
use core::mem::size_of;
use object::elf::*;
use object::endian::{BigEndian, Endian, Endianness, LittleEndian};
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/externals/global.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::prelude::*;
use crate::runtime::vm::{GcRootsList, SendSyncPtr};
use crate::{
store::{AutoAssertNoGc, StoreData, StoreOpaque, Stored},
trampoline::generate_global_export,
AnyRef, AsContext, AsContextMut, ExternRef, Func, GlobalType, HeapType, Mutability, Ref,
RootedGcRefImpl, Val, ValType,
};
use anyhow::{bail, Context, Result};
use core::ptr;
use core::ptr::NonNull;
use wasmtime_environ::TypeTrace;
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::runtime::vm::{self as runtime};
use crate::store::{AutoAssertNoGc, StoreData, StoreOpaque, Stored};
use crate::trampoline::generate_table_export;
use crate::{AnyRef, AsContext, AsContextMut, ExternRef, Func, HeapType, Ref, TableType};
use anyhow::{anyhow, bail, Context, Result};
use core::iter;
use core::ptr::NonNull;
use runtime::{GcRootsList, SendSyncPtr};
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
StoreContext, StoreContextMut, Val, ValRaw, ValType,
};
use alloc::sync::Arc;
use anyhow::{bail, Context as _, Error, Result};
use core::ffi::c_void;
use core::future::Future;
use core::mem::{self, MaybeUninit};
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/func/typed.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::invoke_wasm_and_catch_traps;
use crate::prelude::*;
use crate::runtime::vm::{VMFuncRef, VMOpaqueContext};
use crate::store::{AutoAssertNoGc, StoreOpaque};
use crate::{
AsContext, AsContextMut, Engine, Func, FuncType, HeapType, NoFunc, RefType, StoreContextMut,
ValRaw, ValType,
};
use anyhow::{bail, Context, Result};
use core::ffi::c_void;
use core::marker;
use core::mem::{self, MaybeUninit};
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/gc/enabled/externref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
AsContextMut, GcHeapOutOfMemory, GcRefImpl, GcRootIndex, HeapType, ManuallyRooted, RefType,
Result, RootSet, Rooted, StoreContext, StoreContextMut, ValRaw, ValType, WasmTy,
};
use anyhow::Context;
use core::any::Any;
use core::mem;
use core::mem::MaybeUninit;
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/gc/enabled/rooting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ use crate::{
store::{AutoAssertNoGc, StoreId, StoreOpaque},
AsContext, AsContextMut, GcRef, Result, RootedGcRef,
};
use anyhow::anyhow;
use core::any;
use core::marker;
use core::mem;
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
StoreContext, StoreContextMut, Table, TypedFunc,
};
use alloc::sync::Arc;
use anyhow::{anyhow, bail, Context, Result};
use core::ptr::NonNull;
use wasmparser::WasmFeatures;
use wasmtime_environ::{
Expand Down
4 changes: 0 additions & 4 deletions crates/wasmtime/src/runtime/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::prelude::*;
use crate::runtime::vm::{CompiledModuleId, MmapVec};
use crate::{code_memory::CodeMemory, profiling_agent::ProfilingAgent};
use alloc::sync::Arc;
use anyhow::Result;
use core::str;
use wasmtime_environ::{
CompiledFunctionInfo, CompiledModuleInfo, DefinedFuncIndex, FuncIndex, FunctionLoc,
Expand Down Expand Up @@ -70,8 +69,6 @@ impl CompiledModule {
fn register_debug_and_profiling(&mut self, profiler: &dyn ProfilingAgent) -> Result<()> {
#[cfg(feature = "debug-builtins")]
if self.meta.native_debug_info_present {
use anyhow::Context;

let text = self.text();
let bytes = crate::debug::create_gdbjit_image(
self.mmap().to_vec(),
Expand Down Expand Up @@ -263,7 +260,6 @@ impl CompiledModule {
/// what filename and line number a wasm pc comes from.
#[cfg(feature = "addr2line")]
pub fn symbolize_context(&self) -> Result<Option<SymbolizeContext<'_>>> {
use anyhow::Context;
use gimli::EndianSlice;
if !self.meta.has_wasm_debuginfo {
return Ok(None);
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/limits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::prelude::*;
use anyhow::{bail, Result};

/// Value returned by [`ResourceLimiter::instances`] default method
pub const DEFAULT_INSTANCE_LIMIT: usize = 10000;
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
Instance, Module, StoreContextMut, Val, ValRaw, ValType, WasmTyList,
};
use alloc::sync::Arc;
use anyhow::{bail, Context, Result};
use core::fmt;
#[cfg(feature = "async")]
use core::future::Future;
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::store::{StoreData, StoreOpaque, Stored};
use crate::trampoline::generate_memory_export;
use crate::Trap;
use crate::{AsContext, AsContextMut, Engine, MemoryType, StoreContext, StoreContextMut};
use anyhow::{bail, Result};
use core::cell::UnsafeCell;
use core::fmt;
use core::ops::Range;
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
Engine,
};
use alloc::sync::Arc;
use anyhow::{bail, Result};
use core::fmt;
use core::ops::Range;
use core::ptr::NonNull;
Expand Down
3 changes: 0 additions & 3 deletions crates/wasmtime/src/runtime/profiling.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::prelude::*;
use crate::runtime::vm::Backtrace;
use crate::{instantiate::CompiledModule, AsContext, Module};
#[allow(unused_imports)]
use anyhow::bail;
use anyhow::Result;
use fxprof_processed_profile::{
debugid::DebugId, CategoryHandle, Frame, FrameFlags, FrameInfo, LibraryInfo, Profile,
ReferenceTimestamp, Symbol, SymbolTable, Timestamp,
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/stack.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::prelude::*;
use anyhow::Error;
use std::{ops::Range, sync::Arc};
use wasmtime_fiber::{RuntimeFiberStack, RuntimeFiberStackCreator};

Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ use crate::RootSet;
use crate::{module::ModuleRegistry, Engine, Module, Trap, Val, ValRaw};
use crate::{Global, Instance, Memory, RootScope, Table, Uninhabited};
use alloc::sync::Arc;
use anyhow::{anyhow, bail, Result};
use core::cell::UnsafeCell;
use core::fmt;
use core::future::Future;
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/trampoline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::runtime::vm::{
use crate::store::{InstanceId, StoreOpaque};
use crate::{MemoryType, TableType};
use alloc::sync::Arc;
use anyhow::Result;
use core::any::Any;
use wasmtime_environ::{MemoryIndex, Module, TableIndex, VMSharedTypeIndex};

Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/trampoline/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::runtime::vm::{
};
use crate::type_registry::RegisteredType;
use crate::{FuncType, ValRaw};
use anyhow::Result;
use core::ptr;

struct TrampolineState<F> {
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/trampoline/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::runtime::vm::{
use crate::store::{InstanceId, StoreOpaque};
use crate::MemoryType;
use alloc::sync::Arc;
use anyhow::{anyhow, Result};
use core::ops::Range;
use wasmtime_environ::{
DefinedMemoryIndex, DefinedTableIndex, EntityIndex, HostPtr, MemoryPlan, MemoryStyle, Module,
Expand Down
1 change: 0 additions & 1 deletion crates/wasmtime/src/runtime/trampoline/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::prelude::*;
use crate::store::{InstanceId, StoreOpaque};
use crate::trampoline::create_handle;
use crate::TableType;
use anyhow::Result;
use wasmtime_environ::{EntityIndex, Module, TypeTrace};

pub fn create_table(store: &mut StoreOpaque, table: &TableType) -> Result<InstanceId> {
Expand Down
Loading

0 comments on commit 1512a95

Please sign in to comment.