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

rust: add typed value and execution result #705

Merged
merged 2 commits into from
Feb 4, 2021
Merged

rust: add typed value and execution result #705

merged 2 commits into from
Feb 4, 2021

Conversation

axic
Copy link
Member

@axic axic commented Jan 28, 2021

Depends on #652.

U64(u64),
F32(f32),
F64(f64),
Void,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need Void as we should use Option<> instead.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know Rust style, but seems Void will be useful in C/C++ in some cases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Result/Option is more natural than having a special enum value in this case, it seems.

@codecov
Copy link

codecov bot commented Jan 28, 2021

Codecov Report

Merging #705 (07034e0) into master (91f2ba4) will decrease coverage by 0.11%.
The diff coverage is 92.07%.

@@            Coverage Diff             @@
##           master     #705      +/-   ##
==========================================
- Coverage   99.32%   99.20%   -0.12%     
==========================================
  Files          72       73       +1     
  Lines       10660    11110     +450     
==========================================
+ Hits        10588    11022     +434     
- Misses         72       88      +16     
Flag Coverage Δ
rust 96.44% <92.07%> (?)
spectests 90.84% <ø> (ø)
unittests 99.32% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
bindings/rust/src/lib.rs 96.44% <92.07%> (ø)

@axic axic force-pushed the rust-safe-execute branch from bd46cf2 to 8fe90d3 Compare January 28, 2021 19:38
@axic axic force-pushed the rust-typed-value branch from 96e89a5 to 54fe636 Compare January 28, 2021 19:39
@axic
Copy link
Member Author

axic commented Jan 28, 2021

In this we can also consider introducing TypedExecutionResult or just returning Result<Option<TypedValue>,FizzyError> in execute. We would have a special Trap error for traps.

@axic axic force-pushed the rust-safe-execute branch 4 times, most recently from 693ad0d to 5e5e43f Compare January 29, 2021 11:57
Base automatically changed from rust-safe-execute to master January 29, 2021 12:32
@axic axic force-pushed the rust-typed-value branch 2 times, most recently from b4f79c4 to b7a8bb0 Compare February 1, 2021 19:45
@@ -131,6 +131,84 @@ impl From<f64> for Value {
}
}

/// A WebAssembly value i32/i64/f32/f64 with its type specified.
pub enum TypedValue {
U32(u32),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since sys::FizzyValueType only supports an "unsigned" type for 32/64-bit integers, we are matching that here. The naming discrepancy is that I wanted to make clear U32/U64 matches the unsigned Rust type u32/u64.


pub fn as_i32(&self) -> Option<i32> {
match self {
TypedValue::U32(v) => Some(*v as i32),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still a question, whether we want to do this, or use Rust specific helpers for casting between u32 -> i32.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the alternative being x.as_u32() as i32? I think this is fine then.

pub fn value(&self) -> Option<TypedValue> {
if self.result.has_value {
assert!(!self.result.trapped);
assert!(self.value_type != sys::FizzyValueTypeVoid);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can merge these assertions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can stay as they are.

@axic axic marked this pull request as ready for review February 3, 2021 15:39
@axic axic force-pushed the rust-typed-value branch from 39c8b3b to decee82 Compare February 3, 2021 15:39
@axic axic requested review from chfast and gumb0 February 3, 2021 15:40
@axic axic changed the title rust: add typed value rust: add typed value and execution result Feb 3, 2021
@@ -414,7 +631,7 @@ mod tests {
let result = result.unwrap();
assert!(!result.trapped());
assert!(result.value().is_some());
assert_eq!(result.value().unwrap().as_i32(), 42);
assert_eq!(result.value().unwrap().as_u32().unwrap(), 42);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, perhaps could consider a TryInto trait implementation instead, maybe more canonical.

@axic axic force-pushed the rust-typed-value branch from decee82 to 557785f Compare February 3, 2021 15:50
@@ -269,6 +380,113 @@ mod tests {
assert_eq!(v.as_f64(), f64::MAX);
}

#[test]
fn typed_value_conversion() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need tests for wrong type.

Copy link
Member Author

@axic axic Feb 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong input type is a compilation error. I can add checks for not being able to retrieve wrong output.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean the case when .as_u32() is None.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed that 10 minutes ago, just github is slow today showing up changes.

@@ -151,6 +218,36 @@ impl ExecutionResult {
}
}

/// The result of an execution.
pub struct TypedExecutionResult {
result: sys::FizzyExecutionResult,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be more convenient as ExecutionResult type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because it hides the very details we are checking. Basically TypedExecutionResult and ExecutionResult is a bit of a duplication. I think in the future we may want to review this and perhaps get rid of the unsafe execution, once we have benchmarked the overheads.

@axic axic force-pushed the rust-typed-value branch from 7b1d470 to 0c3db09 Compare February 3, 2021 23:31
@axic axic force-pushed the rust-typed-value branch from 0c3db09 to 76f9a5a Compare February 4, 2021 00:25
@axic axic force-pushed the rust-typed-value branch from 76f9a5a to 07034e0 Compare February 4, 2021 00:31
@axic axic merged commit 2b0693c into master Feb 4, 2021
@axic axic deleted the rust-typed-value branch February 4, 2021 01:00
@axic axic mentioned this pull request Feb 4, 2021
16 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants