diff options
author | crowlKats <13135287+crowlKats@users.noreply.github.com> | 2021-02-18 13:54:57 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-18 13:54:57 +0100 |
commit | 666c4b77b07abea8ae4f778d5a7e7fabdc7159a8 (patch) | |
tree | 0b3da2af1f8ad72996d58d65f82ce20228599654 /runtime/ops/mod.rs | |
parent | 2225e83da2d118678e3df1e2801af195166bc65a (diff) |
feat(runtime/ops): strongly typed deserialization of JSON ops (#9532)
Diffstat (limited to 'runtime/ops/mod.rs')
-rw-r--r-- | runtime/ops/mod.rs | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/runtime/ops/mod.rs b/runtime/ops/mod.rs index 0da4b9771..3ead7efa2 100644 --- a/runtime/ops/mod.rs +++ b/runtime/ops/mod.rs @@ -28,7 +28,8 @@ use crate::metrics::metrics_op; use deno_core::error::AnyError; use deno_core::json_op_async; use deno_core::json_op_sync; -use deno_core::serde_json::Value; +use deno_core::serde::de::DeserializeOwned; +use deno_core::serde::Serialize; use deno_core::BufVec; use deno_core::JsRuntime; use deno_core::OpState; @@ -37,18 +38,24 @@ use std::cell::RefCell; use std::future::Future; use std::rc::Rc; -pub fn reg_json_async<F, R>(rt: &mut JsRuntime, name: &'static str, op_fn: F) -where - F: Fn(Rc<RefCell<OpState>>, Value, BufVec) -> R + 'static, - R: Future<Output = Result<Value, AnyError>> + 'static, +pub fn reg_json_async<F, V, R, RV>( + rt: &mut JsRuntime, + name: &'static str, + op_fn: F, +) where + F: Fn(Rc<RefCell<OpState>>, V, BufVec) -> R + 'static, + V: DeserializeOwned, + R: Future<Output = Result<RV, AnyError>> + 'static, + RV: Serialize, { rt.register_op(name, metrics_op(json_op_async(op_fn))); } -pub fn reg_json_sync<F>(rt: &mut JsRuntime, name: &'static str, op_fn: F) +pub fn reg_json_sync<F, V, R>(rt: &mut JsRuntime, name: &'static str, op_fn: F) where - F: Fn(&mut OpState, Value, &mut [ZeroCopyBuf]) -> Result<Value, AnyError> - + 'static, + F: Fn(&mut OpState, V, &mut [ZeroCopyBuf]) -> Result<R, AnyError> + 'static, + V: DeserializeOwned, + R: Serialize, { rt.register_op(name, metrics_op(json_op_sync(op_fn))); } |