diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-04-01 13:24:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-01 07:24:30 -0400 |
commit | 6eace4de5c825c42ac4efd0d9b912c4911664a6a (patch) | |
tree | ebc2e59c4c67e62125179517345a2f379da9e6dc /core/ops.rs | |
parent | f8aff8edcdb7330a8cc397e2af2ec445533ce970 (diff) |
perf(core): js errors as unions vs tuples to reduce allocs (#9947)
Diffstat (limited to 'core/ops.rs')
-rw-r--r-- | core/ops.rs | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/core/ops.rs b/core/ops.rs index 3af60d072..867bb5210 100644 --- a/core/ops.rs +++ b/core/ops.rs @@ -74,11 +74,16 @@ pub enum Op { } #[derive(Serialize)] -pub struct OpResult<R>(Option<R>, Option<OpError>); +#[serde(untagged)] +pub enum OpResult<R> { + Ok(R), + Err(OpError), +} #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub struct OpError { + #[serde(rename = "$err_class_name")] class_name: &'static str, message: String, } @@ -88,14 +93,11 @@ pub fn serialize_op_result<R: Serialize + 'static>( state: Rc<RefCell<OpState>>, ) -> OpResponse { OpResponse::Value(Box::new(match result { - Ok(v) => OpResult::<R>(Some(v), None), - Err(err) => OpResult::<R>( - None, - Some(OpError { - class_name: (state.borrow().get_error_class_fn)(&err), - message: err.to_string(), - }), - ), + Ok(v) => OpResult::Ok(v), + Err(err) => OpResult::Err(OpError { + class_name: (state.borrow().get_error_class_fn)(&err), + message: err.to_string(), + }), })) } |