Tweak the API of the Val type (#679)

* Tweak the API of the `Val` type

A few updates to the API of the `Val` type:

* Added a payload for `V128`.
* Replace existing accessor methods with `Option`-returning versions.
* Add `unwrap_xxx` family of methods to extract a value and panic.
* Remove `Into` conversions which panic, since panicking in `From` or
  `Into` isn't idiomatic in Rust
* Add documentation to all methods/values/enums/etc.
* Rename `Val::default` to `Val::null`

* Run rustfmt

* Review comments
This commit is contained in:
Alex Crichton
2019-12-06 16:19:37 -06:00
committed by GitHub
parent 2597468b30
commit 3d69e04659
7 changed files with 103 additions and 107 deletions

View File

@@ -9,10 +9,10 @@ struct Callback;
impl Callable for Callback {
fn call(&self, args: &[Val], results: &mut [Val]) -> Result<(), HostRef<Trap>> {
println!("Calling back...");
println!("> {} {}", args[0].i32(), args[1].i64());
println!("> {} {}", args[0].unwrap_i32(), args[1].unwrap_i64());
results[0] = Val::I64(args[1].i64() + 1);
results[1] = Val::I32(args[0].i32() + 1);
results[0] = Val::I64(args[1].unwrap_i64() + 1);
results[1] = Val::I32(args[0].unwrap_i32() + 1);
Ok(())
}
}
@@ -88,10 +88,10 @@ fn main() -> Result<()> {
.map_err(|e| format_err!("> Error calling g! {:?}", e))?;
println!("Printing result...");
println!("> {} {}", results[0].i64(), results[1].i32());
println!("> {} {}", results[0].unwrap_i64(), results[1].unwrap_i32());
debug_assert_eq!(results[0].i64(), 4);
debug_assert_eq!(results[1].i32(), 2);
debug_assert_eq!(results[0].unwrap_i64(), 4);
debug_assert_eq!(results[1].unwrap_i32(), 2);
// Call `$round_trip_many`.
println!("Calling export \"round_trip_many\"...");
@@ -115,7 +115,7 @@ fn main() -> Result<()> {
println!("Printing result...");
print!(">");
for r in results.iter() {
print!(" {}", r.i64());
print!(" {}", r.unwrap_i64());
}
println!();