Skip to content

Commit

Permalink
Result alias for convienient use of anyhow::Error without depending o…
Browse files Browse the repository at this point in the history
…n anyhow (#5853)

* Add a Result type alias

* Refer to the type in top-level docs

* Use this inside the documentation for the bindgen! macro

* Fix tests

* Address small PR feedback

* Simply re-export anyhow types
  • Loading branch information
rylev authored Feb 24, 2023
1 parent 7d790fc commit 6d6bd0e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
1 change: 0 additions & 1 deletion crates/fuzzing/src/oracles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,6 @@ pub fn set_fuel<T>(store: &mut Store<T>, fuel: u64) {
/// arbitrary types and values.
pub fn dynamic_component_api_target(input: &mut arbitrary::Unstructured) -> arbitrary::Result<()> {
use crate::generators::component_types;
use anyhow::Result;
use component_fuzz_util::{TestCase, EXPORT_FUNCTION, IMPORT_FUNCTION};
use component_test_util::FuncExt;
use wasmtime::component::{Component, Linker, Val};
Expand Down
12 changes: 5 additions & 7 deletions crates/wasmtime/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ pub(crate) use self::store::ComponentStoreData;
/// Then you can interact with the generated bindings like so:
///
/// ```rust,ignore
/// use anyhow::Result;
/// use wasmtime::component::*;
/// use wasmtime::{Config, Engine, Store};
///
Expand All @@ -97,12 +96,12 @@ pub(crate) use self::store::ComponentStoreData;
/// impl HelloWorldImports for MyState {
/// // Note the `Result` return value here where `Ok` is returned back to
/// // the component and `Err` will raise a trap.
/// fn name(&mut self) -> Result<String> {
/// fn name(&mut self) -> wasmtime::Result<String> {
/// Ok(self.name.clone())
/// }
/// }
///
/// fn main() -> Result<()> {
/// fn main() -> wasmtime::Result<()> {
/// // Configure an `Engine` and compile the `Component` that is being run for
/// // the application.
/// let mut config = Config::new();
Expand Down Expand Up @@ -168,7 +167,6 @@ pub(crate) use self::store::ComponentStoreData;
/// Then you can interact with the generated bindings like so:
///
/// ```rust,ignore
/// use anyhow::Result;
/// use wasmtime::component::*;
/// use wasmtime::{Config, Engine, Store};
///
Expand All @@ -180,16 +178,16 @@ pub(crate) use self::store::ComponentStoreData;
///
/// // Note that the trait here is per-interface and within a submodule now.
/// impl host::Host for MyState {
/// fn gen_random_integer(&mut self) -> Result<u32> {
/// fn gen_random_integer(&mut self) -> wasmtime::Result<u32> {
/// Ok(rand::thread_rng().gen())
/// }
///
/// fn sha256(&mut self, bytes: Vec<u8>) -> Result<String> {
/// fn sha256(&mut self, bytes: Vec<u8>) -> wasmtime::Result<String> {
/// // ...
/// }
/// }
///
/// fn main() -> Result<()> {
/// fn main() -> wasmtime::Result<()> {
/// let mut config = Config::new();
/// config.wasm_component_model(true);
/// let engine = Engine::new(&config)?;
Expand Down
18 changes: 11 additions & 7 deletions crates/wasmtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
//! An example of using Wasmtime looks like:
//!
//! ```
//! use anyhow::Result;
//! use wasmtime::*;
//!
//! fn main() -> Result<()> {
//! fn main() -> wasmtime::Result<()> {
//! // Modules can be compiled through either the text or binary format
//! let engine = Engine::default();
//! let wat = r#"
Expand Down Expand Up @@ -141,10 +140,9 @@
//! For example we can reimplement the above example with a `Linker`:
//!
//! ```
//! use anyhow::Result;
//! use wasmtime::*;
//!
//! fn main() -> Result<()> {
//! fn main() -> wasmtime::Result<()> {
//! let engine = Engine::default();
//! let wat = r#"
//! (module
Expand Down Expand Up @@ -301,11 +299,10 @@
//! An example of using WASI looks like:
//!
//! ```no_run
//! # use anyhow::Result;
//! # use wasmtime::*;
//! use wasmtime_wasi::sync::WasiCtxBuilder;
//!
//! # fn main() -> Result<()> {
//! # fn main() -> wasmtime::Result<()> {
//! // Compile our module and create a `Linker` which has WASI functions defined
//! // within it.
//! let engine = Engine::default();
Expand Down Expand Up @@ -333,7 +330,7 @@
//! use std::str;
//!
//! # use wasmtime::*;
//! # fn main() -> anyhow::Result<()> {
//! # fn main() -> wasmtime::Result<()> {
//! let mut store = Store::default();
//! let log_str = Func::wrap(&mut store, |mut caller: Caller<'_, ()>, ptr: i32, len: i32| {
//! // Use our `caller` context to learn about the memory export of the
Expand Down Expand Up @@ -427,6 +424,13 @@ pub use crate::trap::*;
pub use crate::types::*;
pub use crate::values::*;

/// A convenience wrapper for `Result<T, anyhow::Error>`.
///
/// 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`.
pub use anyhow::{Error, Result};

#[cfg(feature = "component-model")]
pub mod component;

Expand Down
20 changes: 10 additions & 10 deletions crates/wit-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl Wasmtime {
"
pub fn new(
__exports: &mut wasmtime::component::ExportInstance<'_, '_>,
) -> anyhow::Result<{camel}> {{
) -> wasmtime::Result<{camel}> {{
"
);
let mut fields = Vec::new();
Expand Down Expand Up @@ -267,7 +267,7 @@ impl Wasmtime {
mut store: impl wasmtime::AsContextMut<Data = T>,
component: &wasmtime::component::Component,
linker: &wasmtime::component::Linker<T>,
) -> anyhow::Result<(Self, wasmtime::component::Instance)> {{
) -> wasmtime::Result<(Self, wasmtime::component::Instance)> {{
let instance = linker.instantiate{async__}(&mut store, component){await_}?;
Ok((Self::new(store, &instance)?, instance))
}}
Expand All @@ -278,7 +278,7 @@ impl Wasmtime {
pub {async_} fn instantiate_pre<T {send}>(
mut store: impl wasmtime::AsContextMut<Data = T>,
instance_pre: &wasmtime::component::InstancePre<T>,
) -> anyhow::Result<(Self, wasmtime::component::Instance)> {{
) -> wasmtime::Result<(Self, wasmtime::component::Instance)> {{
let instance = instance_pre.instantiate{async__}(&mut store){await_}?;
Ok((Self::new(store, &instance)?, instance))
}}
Expand All @@ -294,7 +294,7 @@ impl Wasmtime {
pub fn new(
mut store: impl wasmtime::AsContextMut,
instance: &wasmtime::component::Instance,
) -> anyhow::Result<Self> {{
) -> wasmtime::Result<Self> {{
let mut store = store.as_context_mut();
let mut exports = instance.exports(&mut store);
let mut __exports = exports.root();
Expand Down Expand Up @@ -397,7 +397,7 @@ impl Wasmtime {
pub fn add_to_linker<T, U>(
linker: &mut wasmtime::component::Linker<T>,
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
) -> anyhow::Result<()>
) -> wasmtime::Result<()>
where U: \
"
);
Expand Down Expand Up @@ -442,7 +442,7 @@ impl Wasmtime {
pub fn add_root_to_linker<T, U>(
linker: &mut wasmtime::component::Linker<T>,
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
) -> anyhow::Result<()>
) -> wasmtime::Result<()>
where U: {world_trait}{maybe_send}
{{
let mut linker = linker.root();
Expand Down Expand Up @@ -978,7 +978,7 @@ impl<'a> InterfaceGenerator<'a> {
pub fn add_to_linker<T, U>(
linker: &mut wasmtime::component::Linker<T>,
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
) -> anyhow::Result<()>
) -> wasmtime::Result<()>
where {where_clause},
{{
"
Expand Down Expand Up @@ -1124,9 +1124,9 @@ impl<'a> InterfaceGenerator<'a> {
self.push_str(&error_typename);
self.push_str(">");
} else {
// All other functions get their return values wrapped in an anyhow::Result.
// All other functions get their return values wrapped in an wasmtime::Result.
// Returning the anyhow::Error case can be used to trap.
self.push_str("anyhow::Result<");
self.push_str("wasmtime::Result<");
self.print_result_ty(&func.results, TypeMode::Owned);
self.push_str(">");
}
Expand Down Expand Up @@ -1174,7 +1174,7 @@ impl<'a> InterfaceGenerator<'a> {
self.print_ty(&param.1, TypeMode::AllBorrowed("'_"));
self.push_str(",");
}
self.src.push_str(") -> anyhow::Result<");
self.src.push_str(") -> wasmtime::Result<");
self.print_result_ty(&func.results, TypeMode::Owned);

if self.gen.opts.async_ {
Expand Down
2 changes: 1 addition & 1 deletion tests/all/memory_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mod not_for_windows {
Some(self.size - self.guard_size)
}

fn grow_to(&mut self, new_size: usize) -> Result<(), anyhow::Error> {
fn grow_to(&mut self, new_size: usize) -> wasmtime::Result<()> {
println!("grow to {:x}", new_size);
let delta = new_size - self.used_wasm_bytes;
unsafe {
Expand Down

0 comments on commit 6d6bd0e

Please sign in to comment.