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:
Ryan Levick
2023-02-24 16:37:34 +01:00
committed by GitHub
parent 7d790fcdfe
commit 6d6bd0ea1c
5 changed files with 27 additions and 26 deletions

View File

@@ -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};
///
@@ -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();
@@ -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};
///
@@ -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)?;

View File

@@ -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#"
@@ -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
@@ -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();
@@ -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
@@ -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;