summaryrefslogtreecommitdiff
path: root/cli/ops/dispatch_json.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/ops/dispatch_json.rs')
-rw-r--r--cli/ops/dispatch_json.rs28
1 files changed, 25 insertions, 3 deletions
diff --git a/cli/ops/dispatch_json.rs b/cli/ops/dispatch_json.rs
index 30a3a4433..c3a1c3b3f 100644
--- a/cli/ops/dispatch_json.rs
+++ b/cli/ops/dispatch_json.rs
@@ -45,14 +45,36 @@ struct AsyncArgs {
promise_id: Option<u64>,
}
-pub fn json_op<D>(d: D) -> impl OpDispatcher
+/// Like OpDispatcher but with additional json `Value` parameter
+/// and return a result of `JsonOp` instead of `Op`.
+pub trait JsonOpDispatcher {
+ fn dispatch(
+ &self,
+ isolate_state: &mut CoreIsolateState,
+ json: Value,
+ zero_copy: &mut [ZeroCopyBuf],
+ ) -> Result<JsonOp, OpError>;
+}
+
+impl<F> JsonOpDispatcher for F
where
- D: Fn(
+ F: Fn(
&mut CoreIsolateState,
Value,
&mut [ZeroCopyBuf],
) -> Result<JsonOp, OpError>,
{
+ fn dispatch(
+ &self,
+ isolate_state: &mut CoreIsolateState,
+ json: Value,
+ zero_copy: &mut [ZeroCopyBuf],
+ ) -> Result<JsonOp, OpError> {
+ self(isolate_state, json, zero_copy)
+ }
+}
+
+pub fn json_op(d: impl JsonOpDispatcher) -> impl OpDispatcher {
move |isolate_state: &mut CoreIsolateState, zero_copy: &mut [ZeroCopyBuf]| {
assert!(!zero_copy.is_empty(), "Expected JSON string at position 0");
let async_args: AsyncArgs = match serde_json::from_slice(&zero_copy[0]) {
@@ -67,7 +89,7 @@ where
let result = serde_json::from_slice(&zero_copy[0])
.map_err(OpError::from)
- .and_then(|args| d(isolate_state, args, &mut zero_copy[1..]));
+ .and_then(|args| d.dispatch(isolate_state, args, &mut zero_copy[1..]));
// Convert to Op
match result {