diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2019-08-21 20:42:48 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-21 20:42:48 -0400 |
commit | bdc97b3976786bb744a27e59b0f4f28554a682df (patch) | |
tree | bf4635ad903de542c10620e95adb72eee03d9204 /cli/ops/utils.rs | |
parent | b764d1b8ffc4bf5e2ab89bdbd978d708a6da0f24 (diff) |
Organize dispatch a bit (#2796)
Just some clean up reorganization around flatbuffer/minimal dispatch
code. This is prep for adding a JSON dispatcher.
Diffstat (limited to 'cli/ops/utils.rs')
-rw-r--r-- | cli/ops/utils.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/cli/ops/utils.rs b/cli/ops/utils.rs new file mode 100644 index 000000000..a9b0b442c --- /dev/null +++ b/cli/ops/utils.rs @@ -0,0 +1,48 @@ +use crate::tokio_util; +use deno::Buf; +use deno::ErrBox; +use deno::Op; +use deno::OpResult; +use futures::Poll; + +pub type CliOpResult = OpResult<ErrBox>; + +#[inline] +pub fn ok_buf(buf: Buf) -> CliOpResult { + Ok(Op::Sync(buf)) +} + +#[inline] +pub fn empty_buf() -> Buf { + Box::new([]) +} + +// This is just type conversion. Implement From trait? +// See https://github.com/tokio-rs/tokio/blob/ffd73a64e7ec497622b7f939e38017afe7124dc4/tokio-fs/src/lib.rs#L76-L85 +fn convert_blocking<F>(f: F) -> Poll<Buf, ErrBox> +where + F: FnOnce() -> Result<Buf, ErrBox>, +{ + use futures::Async::*; + match tokio_threadpool::blocking(f) { + Ok(Ready(Ok(v))) => Ok(v.into()), + Ok(Ready(Err(err))) => Err(err), + Ok(NotReady) => Ok(NotReady), + Err(err) => panic!("blocking error {}", err), + } +} + +pub fn blocking<F>(is_sync: bool, f: F) -> CliOpResult +where + F: 'static + Send + FnOnce() -> Result<Buf, ErrBox>, +{ + if is_sync { + let result_buf = f()?; + Ok(Op::Sync(result_buf)) + } else { + Ok(Op::Async(Box::new(futures::sync::oneshot::spawn( + tokio_util::poll_fn(move || convert_blocking(f)), + &tokio_executor::DefaultExecutor::current(), + )))) + } +} |