Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Result alias for convienient use of anyhow::Error without depending on anyhow #5853

Merged
merged 6 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 10 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,12 @@ pub use crate::trap::*;
pub use crate::types::*;
pub use crate::values::*;

/// A convience wrapper for `Result<T, anyhow::Error>`.
rylev marked this conversation as resolved.
Show resolved Hide resolved
///
/// This type can be used to interact with `wasmtimes`'s extensive use
/// of `anyhow::Error` while still not directly depending on `anyhow`.
rylev marked this conversation as resolved.
Show resolved Hide resolved
pub type Result<T> = std::result::Result<T, anyhow::Error>;
rylev marked this conversation as resolved.
Show resolved Hide resolved

#[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
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -98,7 +98,7 @@ mod not_for_windows {
maximum: Option<usize>,
reserved_size: Option<usize>,
guard_size: usize,
) -> Result<Box<dyn LinearMemory>, String> {
) -> std::result::Result<Box<dyn LinearMemory>, String> {
rylev marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(guard_size, 0);
assert!(reserved_size.is_none());
assert!(!ty.is_64());
Expand Down