Remove explicit S type parameters (#5275)

* Remove explicit `S` type parameters

This commit removes the explicit `S` type parameter on `Func::typed` and
`Instance::get_typed_func`. Historical versions of Rust required that
this be a type parameter but recent rustcs support a mixture of explicit
type parameters and `impl Trait`. This removes, at callsites, a
superfluous `, _` argument which otherwise never needs specification.

* Fix mdbook examples
This commit is contained in:
Alex Crichton
2022-11-15 23:04:26 -06:00
committed by GitHub
parent 8426904129
commit b0939f6626
50 changed files with 223 additions and 238 deletions

View File

@@ -53,7 +53,7 @@ pub fn check_stacks(stacks: Stacks) -> usize {
.expect("should instantiate okay");
let run = instance
.get_typed_func::<(u32,), (), _>(&mut store, "run")
.get_typed_func::<(u32,), ()>(&mut store, "run")
.expect("should export `run` function");
let mut max_stack_depth = 0;
@@ -62,7 +62,7 @@ pub fn check_stacks(stacks: Stacks) -> usize {
if let Err(trap) = run.call(&mut store, (input.into(),)) {
log::debug!("trap: {:?}", trap);
let get_stack = instance
.get_typed_func::<(), (u32, u32), _>(&mut store, "get_stack")
.get_typed_func::<(), (u32, u32)>(&mut store, "get_stack")
.expect("should export `get_stack` function as expected");
let (ptr, len) = get_stack

View File

@@ -59,7 +59,7 @@ fn run(
let mut store = Store::new(&engine, builder.build());
let instance = linker.instantiate(&mut store, &module)?;
let start = instance.get_typed_func::<(), (), _>(&mut store, "_start")?;
let start = instance.get_typed_func::<(), ()>(&mut store, "_start")?;
start.call(&mut store, ()).map_err(anyhow::Error::from)
};

View File

@@ -67,7 +67,7 @@ fn run(
let mut store = Store::new(&engine, builder.build());
let instance = linker.instantiate_async(&mut store, &module).await?;
let start = instance.get_typed_func::<(), (), _>(&mut store, "_start")?;
let start = instance.get_typed_func::<(), ()>(&mut store, "_start")?;
start
.call_async(&mut store, ())
.await

View File

@@ -95,7 +95,7 @@ use wasmtime_runtime::{
/// // ... or we can make a static assertion about its signature and call it.
/// // Our first call here can fail if the signatures don't match, and then the
/// // second call can fail if the function traps (like the `match` above).
/// let foo = foo.typed::<(), (), _>(&store)?;
/// let foo = foo.typed::<(), ()>(&store)?;
/// foo.call(&mut store, ())?;
/// # Ok(())
/// # }
@@ -130,7 +130,7 @@ use wasmtime_runtime::{
/// "#,
/// )?;
/// let instance = Instance::new(&mut store, &module, &[add.into()])?;
/// let call_add_twice = instance.get_typed_func::<(), i32, _>(&mut store, "call_add_twice")?;
/// let call_add_twice = instance.get_typed_func::<(), i32>(&mut store, "call_add_twice")?;
///
/// assert_eq!(call_add_twice.call(&mut store, ())?, 10);
/// # Ok(())
@@ -603,7 +603,7 @@ impl Func {
/// "#,
/// )?;
/// let instance = Instance::new(&mut store, &module, &[add.into()])?;
/// let foo = instance.get_typed_func::<(i32, i32), i32, _>(&mut store, "foo")?;
/// let foo = instance.get_typed_func::<(i32, i32), i32>(&mut store, "foo")?;
/// assert_eq!(foo.call(&mut store, (1, 2))?, 3);
/// # Ok(())
/// # }
@@ -634,7 +634,7 @@ impl Func {
/// "#,
/// )?;
/// let instance = Instance::new(&mut store, &module, &[add.into()])?;
/// let foo = instance.get_typed_func::<(i32, i32), i32, _>(&mut store, "foo")?;
/// let foo = instance.get_typed_func::<(i32, i32), i32>(&mut store, "foo")?;
/// assert_eq!(foo.call(&mut store, (1, 2))?, 3);
/// assert!(foo.call(&mut store, (i32::max_value(), 1)).is_err());
/// # Ok(())
@@ -672,7 +672,7 @@ impl Func {
/// "#,
/// )?;
/// let instance = Instance::new(&mut store, &module, &[debug.into()])?;
/// let foo = instance.get_typed_func::<(), (), _>(&mut store, "foo")?;
/// let foo = instance.get_typed_func::<(), ()>(&mut store, "foo")?;
/// foo.call(&mut store, ())?;
/// # Ok(())
/// # }
@@ -720,7 +720,7 @@ impl Func {
/// "#,
/// )?;
/// let instance = Instance::new(&mut store, &module, &[log_str.into()])?;
/// let foo = instance.get_typed_func::<(), (), _>(&mut store, "foo")?;
/// let foo = instance.get_typed_func::<(), ()>(&mut store, "foo")?;
/// foo.call(&mut store, ())?;
/// # Ok(())
/// # }
@@ -1178,10 +1178,6 @@ impl Func {
/// function. This behaves the same way as `Params`, but just for the
/// results of the function.
///
/// The `S` type parameter represents the method of passing in the store
/// context, and can typically be specified as simply `_` when calling this
/// function.
///
/// Translation between Rust types and WebAssembly types looks like:
///
/// | WebAssembly | Rust |
@@ -1232,7 +1228,7 @@ impl Func {
/// // Note that this call can fail due to the typecheck not passing, but
/// // in our case we statically know the module so we know this should
/// // pass.
/// let typed = foo.typed::<(), (), _>(&store)?;
/// let typed = foo.typed::<(), ()>(&store)?;
///
/// // Note that this can fail if the wasm traps at runtime.
/// typed.call(&mut store, ())?;
@@ -1245,7 +1241,7 @@ impl Func {
/// ```
/// # use wasmtime::*;
/// # fn foo(add: &Func, mut store: Store<()>) -> anyhow::Result<()> {
/// let typed = add.typed::<(i32, i64), f32, _>(&store)?;
/// let typed = add.typed::<(i32, i64), f32>(&store)?;
/// assert_eq!(typed.call(&mut store, (1, 2))?, 3.0);
/// # Ok(())
/// # }
@@ -1256,18 +1252,20 @@ impl Func {
/// ```
/// # use wasmtime::*;
/// # fn foo(add_with_overflow: &Func, mut store: Store<()>) -> anyhow::Result<()> {
/// let typed = add_with_overflow.typed::<(u32, u32), (u32, i32), _>(&store)?;
/// let typed = add_with_overflow.typed::<(u32, u32), (u32, i32)>(&store)?;
/// let (result, overflow) = typed.call(&mut store, (u32::max_value(), 2))?;
/// assert_eq!(result, 1);
/// assert_eq!(overflow, 1);
/// # Ok(())
/// # }
/// ```
pub fn typed<Params, Results, S>(&self, store: S) -> Result<TypedFunc<Params, Results>>
pub fn typed<Params, Results>(
&self,
store: impl AsContext,
) -> Result<TypedFunc<Params, Results>>
where
Params: WasmParams,
Results: WasmResults,
S: AsContext,
{
// Type-check that the params/results are all valid
let ty = self.ty(store);

View File

@@ -457,21 +457,20 @@ impl Instance {
/// # Panics
///
/// Panics if `store` does not own this instance.
pub fn get_typed_func<Params, Results, S>(
pub fn get_typed_func<Params, Results>(
&self,
mut store: S,
mut store: impl AsContextMut,
name: &str,
) -> Result<TypedFunc<Params, Results>>
where
Params: crate::WasmParams,
Results: crate::WasmResults,
S: AsContextMut,
{
let f = self
.get_export(store.as_context_mut(), name)
.and_then(|f| f.into_func())
.ok_or_else(|| anyhow!("failed to find function export `{}`", name))?;
Ok(f.typed::<Params, Results, _>(store)
Ok(f.typed::<Params, Results>(store)
.with_context(|| format!("failed to convert function `{}` to given type", name))?)
}

View File

@@ -57,7 +57,7 @@
//! // afterwards we can fetch exports by name, as well as asserting the
//! // type signature of the function with `get_typed_func`.
//! let instance = Instance::new(&mut store, &module, &[host_hello.into()])?;
//! let hello = instance.get_typed_func::<(), (), _>(&mut store, "hello")?;
//! let hello = instance.get_typed_func::<(), ()>(&mut store, "hello")?;
//!
//! // And finally we can call the wasm!
//! hello.call(&mut store, ())?;
@@ -168,7 +168,7 @@
//! // resolve the imports of the module using name-based resolution.
//! let mut store = Store::new(&engine, 0);
//! let instance = linker.instantiate(&mut store, &module)?;
//! let hello = instance.get_typed_func::<(), (), _>(&mut store, "hello")?;
//! let hello = instance.get_typed_func::<(), ()>(&mut store, "hello")?;
//! hello.call(&mut store, ())?;
//!
//! Ok(())
@@ -373,7 +373,7 @@
//! "#,
//! )?;
//! let instance = Instance::new(&mut store, &module, &[log_str.into()])?;
//! let foo = instance.get_typed_func::<(), (), _>(&mut store, "foo")?;
//! let foo = instance.get_typed_func::<(), ()>(&mut store, "foo")?;
//! foo.call(&mut store, ())?;
//! # Ok(())
//! # }

View File

@@ -643,7 +643,7 @@ impl<T> Linker<T> {
/// let module = Module::new(&engine, wat)?;
/// linker.module(&mut store, "commander", &module)?;
/// let run = linker.get_default(&mut store, "")?
/// .typed::<(), (), _>(&store)?
/// .typed::<(), ()>(&store)?
/// .clone();
/// run.call(&mut store, ())?;
/// run.call(&mut store, ())?;
@@ -664,7 +664,7 @@ impl<T> Linker<T> {
/// let module = Module::new(&engine, wat)?;
/// linker.module(&mut store, "", &module)?;
/// let run = linker.get(&mut store, "", "run").unwrap().into_func().unwrap();
/// let count = run.typed::<(), i32, _>(&store)?.call(&mut store, ())?;
/// let count = run.typed::<(), i32>(&store)?.call(&mut store, ())?;
/// assert_eq!(count, 0, "a Command should get a fresh instance on each invocation");
///
/// # Ok(())
@@ -727,7 +727,7 @@ impl<T> Linker<T> {
if let Some(export) = instance.get_export(&mut store, "_initialize") {
if let Extern::Func(func) = export {
func.typed::<(), (), _>(&store)
func.typed::<(), ()>(&store)
.and_then(|f| f.call(&mut store, ()).map_err(Into::into))
.context("calling the Reactor initialization function")?;
}
@@ -793,7 +793,7 @@ impl<T> Linker<T> {
if let Some(export) = instance.get_export(&mut store, "_initialize") {
if let Extern::Func(func) = export {
let func = func
.typed::<(), (), _>(&store)
.typed::<(), ()>(&store)
.context("loading the Reactor initialization function")?;
func.call_async(&mut store, ())
.await

View File

@@ -54,12 +54,12 @@ use wasmtime_jit::{demangle_function_name, demangle_function_name_or_index};
/// let mut store = Store::new(&engine, ());
/// let instance = Instance::new(&mut store, &module, &[])?;
///
/// let trap = instance.get_typed_func::<(), (), _>(&mut store, "trap")?;
/// let trap = instance.get_typed_func::<(), ()>(&mut store, "trap")?;
/// let error = trap.call(&mut store, ()).unwrap_err();
/// assert_eq!(*error.downcast_ref::<Trap>().unwrap(), Trap::UnreachableCodeReached);
/// assert!(error.root_cause().is::<Trap>());
///
/// let overflow = instance.get_typed_func::<(), (), _>(&mut store, "overflow")?;
/// let overflow = instance.get_typed_func::<(), ()>(&mut store, "overflow")?;
/// let error = overflow.call(&mut store, ()).unwrap_err();
/// assert_eq!(*error.downcast_ref::<Trap>().unwrap(), Trap::StackOverflow);
/// # Ok(())
@@ -266,7 +266,7 @@ impl std::error::Error for Trap {}
/// )?;
/// let mut store = Store::new(&engine, ());
/// let instance = Instance::new(&mut store, &module, &[])?;
/// let func = instance.get_typed_func::<(), (), _>(&mut store, "run")?;
/// let func = instance.get_typed_func::<(), ()>(&mut store, "run")?;
/// let error = func.call(&mut store, ()).unwrap_err();
/// let bt = error.downcast_ref::<WasmBacktrace>().unwrap();
/// let frames = bt.frames();