-
Notifications
You must be signed in to change notification settings - Fork 38
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
Conversation
bindings/rust/src/lib.rs
Outdated
U64(u64), | ||
F32(f32), | ||
F64(f64), | ||
Void, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 Report
@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
|
bd46cf2
to
8fe90d3
Compare
96e89a5
to
54fe636
Compare
In this we can also consider introducing |
693ad0d
to
5e5e43f
Compare
b4f79c4
to
b7a8bb0
Compare
@@ -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), |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can merge these assertions.
There was a problem hiding this comment.
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.
39c8b3b
to
decee82
Compare
@@ -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); |
There was a problem hiding this comment.
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.
decee82
to
557785f
Compare
@@ -269,6 +380,113 @@ mod tests { | |||
assert_eq!(v.as_f64(), f64::MAX); | |||
} | |||
|
|||
#[test] | |||
fn typed_value_conversion() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
7b1d470
to
0c3db09
Compare
0c3db09
to
76f9a5a
Compare
76f9a5a
to
07034e0
Compare
Depends on #652.