Result alias for convienient use of anyhow::Error without depending on 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
This commit is contained in:
@@ -785,7 +785,6 @@ pub fn set_fuel<T>(store: &mut Store<T>, fuel: u64) {
|
|||||||
/// arbitrary types and values.
|
/// arbitrary types and values.
|
||||||
pub fn dynamic_component_api_target(input: &mut arbitrary::Unstructured) -> arbitrary::Result<()> {
|
pub fn dynamic_component_api_target(input: &mut arbitrary::Unstructured) -> arbitrary::Result<()> {
|
||||||
use crate::generators::component_types;
|
use crate::generators::component_types;
|
||||||
use anyhow::Result;
|
|
||||||
use component_fuzz_util::{TestCase, EXPORT_FUNCTION, IMPORT_FUNCTION};
|
use component_fuzz_util::{TestCase, EXPORT_FUNCTION, IMPORT_FUNCTION};
|
||||||
use component_test_util::FuncExt;
|
use component_test_util::FuncExt;
|
||||||
use wasmtime::component::{Component, Linker, Val};
|
use wasmtime::component::{Component, Linker, Val};
|
||||||
|
|||||||
@@ -82,7 +82,6 @@ pub(crate) use self::store::ComponentStoreData;
|
|||||||
/// Then you can interact with the generated bindings like so:
|
/// Then you can interact with the generated bindings like so:
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```rust,ignore
|
||||||
/// use anyhow::Result;
|
|
||||||
/// use wasmtime::component::*;
|
/// use wasmtime::component::*;
|
||||||
/// use wasmtime::{Config, Engine, Store};
|
/// use wasmtime::{Config, Engine, Store};
|
||||||
///
|
///
|
||||||
@@ -97,12 +96,12 @@ pub(crate) use self::store::ComponentStoreData;
|
|||||||
/// impl HelloWorldImports for MyState {
|
/// impl HelloWorldImports for MyState {
|
||||||
/// // Note the `Result` return value here where `Ok` is returned back to
|
/// // Note the `Result` return value here where `Ok` is returned back to
|
||||||
/// // the component and `Err` will raise a trap.
|
/// // 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())
|
/// Ok(self.name.clone())
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// fn main() -> Result<()> {
|
/// fn main() -> wasmtime::Result<()> {
|
||||||
/// // Configure an `Engine` and compile the `Component` that is being run for
|
/// // Configure an `Engine` and compile the `Component` that is being run for
|
||||||
/// // the application.
|
/// // the application.
|
||||||
/// let mut config = Config::new();
|
/// let mut config = Config::new();
|
||||||
@@ -168,7 +167,6 @@ pub(crate) use self::store::ComponentStoreData;
|
|||||||
/// Then you can interact with the generated bindings like so:
|
/// Then you can interact with the generated bindings like so:
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```rust,ignore
|
||||||
/// use anyhow::Result;
|
|
||||||
/// use wasmtime::component::*;
|
/// use wasmtime::component::*;
|
||||||
/// use wasmtime::{Config, Engine, Store};
|
/// use wasmtime::{Config, Engine, Store};
|
||||||
///
|
///
|
||||||
@@ -180,16 +178,16 @@ pub(crate) use self::store::ComponentStoreData;
|
|||||||
///
|
///
|
||||||
/// // Note that the trait here is per-interface and within a submodule now.
|
/// // Note that the trait here is per-interface and within a submodule now.
|
||||||
/// impl host::Host for MyState {
|
/// 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())
|
/// 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();
|
/// let mut config = Config::new();
|
||||||
/// config.wasm_component_model(true);
|
/// config.wasm_component_model(true);
|
||||||
/// let engine = Engine::new(&config)?;
|
/// let engine = Engine::new(&config)?;
|
||||||
|
|||||||
@@ -27,10 +27,9 @@
|
|||||||
//! An example of using Wasmtime looks like:
|
//! An example of using Wasmtime looks like:
|
||||||
//!
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
//! use anyhow::Result;
|
|
||||||
//! use wasmtime::*;
|
//! use wasmtime::*;
|
||||||
//!
|
//!
|
||||||
//! fn main() -> Result<()> {
|
//! fn main() -> wasmtime::Result<()> {
|
||||||
//! // Modules can be compiled through either the text or binary format
|
//! // Modules can be compiled through either the text or binary format
|
||||||
//! let engine = Engine::default();
|
//! let engine = Engine::default();
|
||||||
//! let wat = r#"
|
//! let wat = r#"
|
||||||
@@ -141,10 +140,9 @@
|
|||||||
//! For example we can reimplement the above example with a `Linker`:
|
//! For example we can reimplement the above example with a `Linker`:
|
||||||
//!
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
//! use anyhow::Result;
|
|
||||||
//! use wasmtime::*;
|
//! use wasmtime::*;
|
||||||
//!
|
//!
|
||||||
//! fn main() -> Result<()> {
|
//! fn main() -> wasmtime::Result<()> {
|
||||||
//! let engine = Engine::default();
|
//! let engine = Engine::default();
|
||||||
//! let wat = r#"
|
//! let wat = r#"
|
||||||
//! (module
|
//! (module
|
||||||
@@ -301,11 +299,10 @@
|
|||||||
//! An example of using WASI looks like:
|
//! An example of using WASI looks like:
|
||||||
//!
|
//!
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! # use anyhow::Result;
|
|
||||||
//! # use wasmtime::*;
|
//! # use wasmtime::*;
|
||||||
//! use wasmtime_wasi::sync::WasiCtxBuilder;
|
//! use wasmtime_wasi::sync::WasiCtxBuilder;
|
||||||
//!
|
//!
|
||||||
//! # fn main() -> Result<()> {
|
//! # fn main() -> wasmtime::Result<()> {
|
||||||
//! // Compile our module and create a `Linker` which has WASI functions defined
|
//! // Compile our module and create a `Linker` which has WASI functions defined
|
||||||
//! // within it.
|
//! // within it.
|
||||||
//! let engine = Engine::default();
|
//! let engine = Engine::default();
|
||||||
@@ -333,7 +330,7 @@
|
|||||||
//! use std::str;
|
//! use std::str;
|
||||||
//!
|
//!
|
||||||
//! # use wasmtime::*;
|
//! # use wasmtime::*;
|
||||||
//! # fn main() -> anyhow::Result<()> {
|
//! # fn main() -> wasmtime::Result<()> {
|
||||||
//! let mut store = Store::default();
|
//! let mut store = Store::default();
|
||||||
//! let log_str = Func::wrap(&mut store, |mut caller: Caller<'_, ()>, ptr: i32, len: i32| {
|
//! 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
|
//! // Use our `caller` context to learn about the memory export of the
|
||||||
@@ -427,6 +424,13 @@ pub use crate::trap::*;
|
|||||||
pub use crate::types::*;
|
pub use crate::types::*;
|
||||||
pub use crate::values::*;
|
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")]
|
#[cfg(feature = "component-model")]
|
||||||
pub mod component;
|
pub mod component;
|
||||||
|
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ impl Wasmtime {
|
|||||||
"
|
"
|
||||||
pub fn new(
|
pub fn new(
|
||||||
__exports: &mut wasmtime::component::ExportInstance<'_, '_>,
|
__exports: &mut wasmtime::component::ExportInstance<'_, '_>,
|
||||||
) -> anyhow::Result<{camel}> {{
|
) -> wasmtime::Result<{camel}> {{
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
let mut fields = Vec::new();
|
let mut fields = Vec::new();
|
||||||
@@ -267,7 +267,7 @@ impl Wasmtime {
|
|||||||
mut store: impl wasmtime::AsContextMut<Data = T>,
|
mut store: impl wasmtime::AsContextMut<Data = T>,
|
||||||
component: &wasmtime::component::Component,
|
component: &wasmtime::component::Component,
|
||||||
linker: &wasmtime::component::Linker<T>,
|
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_}?;
|
let instance = linker.instantiate{async__}(&mut store, component){await_}?;
|
||||||
Ok((Self::new(store, &instance)?, instance))
|
Ok((Self::new(store, &instance)?, instance))
|
||||||
}}
|
}}
|
||||||
@@ -278,7 +278,7 @@ impl Wasmtime {
|
|||||||
pub {async_} fn instantiate_pre<T {send}>(
|
pub {async_} fn instantiate_pre<T {send}>(
|
||||||
mut store: impl wasmtime::AsContextMut<Data = T>,
|
mut store: impl wasmtime::AsContextMut<Data = T>,
|
||||||
instance_pre: &wasmtime::component::InstancePre<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_}?;
|
let instance = instance_pre.instantiate{async__}(&mut store){await_}?;
|
||||||
Ok((Self::new(store, &instance)?, instance))
|
Ok((Self::new(store, &instance)?, instance))
|
||||||
}}
|
}}
|
||||||
@@ -294,7 +294,7 @@ impl Wasmtime {
|
|||||||
pub fn new(
|
pub fn new(
|
||||||
mut store: impl wasmtime::AsContextMut,
|
mut store: impl wasmtime::AsContextMut,
|
||||||
instance: &wasmtime::component::Instance,
|
instance: &wasmtime::component::Instance,
|
||||||
) -> anyhow::Result<Self> {{
|
) -> wasmtime::Result<Self> {{
|
||||||
let mut store = store.as_context_mut();
|
let mut store = store.as_context_mut();
|
||||||
let mut exports = instance.exports(&mut store);
|
let mut exports = instance.exports(&mut store);
|
||||||
let mut __exports = exports.root();
|
let mut __exports = exports.root();
|
||||||
@@ -397,7 +397,7 @@ impl Wasmtime {
|
|||||||
pub fn add_to_linker<T, U>(
|
pub fn add_to_linker<T, U>(
|
||||||
linker: &mut wasmtime::component::Linker<T>,
|
linker: &mut wasmtime::component::Linker<T>,
|
||||||
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
|
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
|
||||||
) -> anyhow::Result<()>
|
) -> wasmtime::Result<()>
|
||||||
where U: \
|
where U: \
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
@@ -442,7 +442,7 @@ impl Wasmtime {
|
|||||||
pub fn add_root_to_linker<T, U>(
|
pub fn add_root_to_linker<T, U>(
|
||||||
linker: &mut wasmtime::component::Linker<T>,
|
linker: &mut wasmtime::component::Linker<T>,
|
||||||
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
|
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
|
||||||
) -> anyhow::Result<()>
|
) -> wasmtime::Result<()>
|
||||||
where U: {world_trait}{maybe_send}
|
where U: {world_trait}{maybe_send}
|
||||||
{{
|
{{
|
||||||
let mut linker = linker.root();
|
let mut linker = linker.root();
|
||||||
@@ -978,7 +978,7 @@ impl<'a> InterfaceGenerator<'a> {
|
|||||||
pub fn add_to_linker<T, U>(
|
pub fn add_to_linker<T, U>(
|
||||||
linker: &mut wasmtime::component::Linker<T>,
|
linker: &mut wasmtime::component::Linker<T>,
|
||||||
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
|
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
|
||||||
) -> anyhow::Result<()>
|
) -> wasmtime::Result<()>
|
||||||
where {where_clause},
|
where {where_clause},
|
||||||
{{
|
{{
|
||||||
"
|
"
|
||||||
@@ -1124,9 +1124,9 @@ impl<'a> InterfaceGenerator<'a> {
|
|||||||
self.push_str(&error_typename);
|
self.push_str(&error_typename);
|
||||||
self.push_str(">");
|
self.push_str(">");
|
||||||
} else {
|
} 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.
|
// 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.print_result_ty(&func.results, TypeMode::Owned);
|
||||||
self.push_str(">");
|
self.push_str(">");
|
||||||
}
|
}
|
||||||
@@ -1174,7 +1174,7 @@ impl<'a> InterfaceGenerator<'a> {
|
|||||||
self.print_ty(¶m.1, TypeMode::AllBorrowed("'_"));
|
self.print_ty(¶m.1, TypeMode::AllBorrowed("'_"));
|
||||||
self.push_str(",");
|
self.push_str(",");
|
||||||
}
|
}
|
||||||
self.src.push_str(") -> anyhow::Result<");
|
self.src.push_str(") -> wasmtime::Result<");
|
||||||
self.print_result_ty(&func.results, TypeMode::Owned);
|
self.print_result_ty(&func.results, TypeMode::Owned);
|
||||||
|
|
||||||
if self.gen.opts.async_ {
|
if self.gen.opts.async_ {
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ mod not_for_windows {
|
|||||||
Some(self.size - self.guard_size)
|
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);
|
println!("grow to {:x}", new_size);
|
||||||
let delta = new_size - self.used_wasm_bytes;
|
let delta = new_size - self.used_wasm_bytes;
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|||||||
Reference in New Issue
Block a user