From fc9c7de94b08049dd04c4ca6016586cdac0dd2ac Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Fri, 30 Apr 2021 16:51:54 +0200 Subject: cleanup(core): replace OpResponse with OpResult (#10434) Drop the Value/Buffer enum since #10432 allows buffers to be serialized rust => v8 --- core/ops.rs | 44 +++++++++----------------------------------- 1 file changed, 9 insertions(+), 35 deletions(-) (limited to 'core/ops.rs') diff --git a/core/ops.rs b/core/ops.rs index 6faffba4b..99bff406c 100644 --- a/core/ops.rs +++ b/core/ops.rs @@ -19,7 +19,7 @@ use std::pin::Pin; use std::rc::Rc; pub type PromiseId = u64; -pub type OpAsyncFuture = Pin>>; +pub type OpAsyncFuture = Pin>>; pub type OpFn = dyn Fn(Rc>, OpPayload, Option) -> Op + 'static; pub type OpId = usize; @@ -58,13 +58,8 @@ impl<'a, 'b, 'c> OpPayload<'a, 'b, 'c> { } } -pub enum OpResponse { - Value(OpResult), - Buffer(Box<[u8]>), -} - pub enum Op { - Sync(OpResponse), + Sync(OpResult), Async(OpAsyncFuture), /// AsyncUnref is the variation of Async, which doesn't block the program /// exiting. @@ -100,14 +95,14 @@ pub struct OpError { pub fn serialize_op_result( result: Result, state: Rc>, -) -> OpResponse { - OpResponse::Value(match result { +) -> OpResult { + match result { Ok(v) => OpResult::Ok(v.into()), Err(err) => OpResult::Err(OpError { class_name: (state.borrow().get_error_class_fn)(&err), message: err.to_string(), }), - }) + } } /// Maintains the resources and ops inside a JS runtime. @@ -205,35 +200,14 @@ mod tests { let bar_id; { let op_table = &mut state.borrow_mut().op_table; - foo_id = op_table.register_op("foo", |_, _, _| { - Op::Sync(OpResponse::Buffer(b"oof!"[..].into())) - }); + foo_id = op_table + .register_op("foo", |_, _, _| Op::Sync(OpResult::Ok(321.into()))); assert_eq!(foo_id, 1); - bar_id = op_table.register_op("bar", |_, _, _| { - Op::Sync(OpResponse::Buffer(b"rab!"[..].into())) - }); + bar_id = op_table + .register_op("bar", |_, _, _| Op::Sync(OpResult::Ok(123.into()))); assert_eq!(bar_id, 2); } - let foo_res = OpTable::route_op( - foo_id, - state.clone(), - OpPayload::empty(), - Default::default(), - ); - assert!( - matches!(foo_res, Op::Sync(OpResponse::Buffer(buf)) if &*buf == b"oof!") - ); - let bar_res = OpTable::route_op( - bar_id, - state.clone(), - OpPayload::empty(), - Default::default(), - ); - assert!( - matches!(bar_res, Op::Sync(OpResponse::Buffer(buf)) if &*buf == b"rab!") - ); - let mut catalog_entries = OpTable::op_entries(state); catalog_entries.sort_by(|(_, id1), (_, id2)| id1.partial_cmp(id2).unwrap()); assert_eq!( -- cgit v1.2.3