From cdba5ab6fc633606aaa6f95d0825832c3ac6fe5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 8 Feb 2020 20:34:31 +0100 Subject: refactor: rename ThreadSafeState, use RefCell for mutable state (#3931) * rename ThreadSafeState to State * State stores InnerState wrapped in Rc and RefCell --- cli/ops/io.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'cli/ops/io.rs') diff --git a/cli/ops/io.rs b/cli/ops/io.rs index 9c074b8ba..4128060f1 100644 --- a/cli/ops/io.rs +++ b/cli/ops/io.rs @@ -3,7 +3,7 @@ use crate::deno_error; use crate::deno_error::bad_resource; use crate::http_util::HttpBody; use crate::ops::minimal_op; -use crate::state::ThreadSafeState; +use crate::state::State; use deno_core::ErrBox; use deno_core::*; use futures::future::FutureExt; @@ -47,7 +47,7 @@ lazy_static! { }; } -pub fn init(i: &mut Isolate, s: &ThreadSafeState) { +pub fn init(i: &mut Isolate, s: &State) { i.register_op( "read", s.core_op(minimal_op(s.stateful_minimal_op(op_read))), @@ -131,7 +131,7 @@ enum IoState { /// /// The returned future will resolve to both the I/O stream and the buffer /// as well as the number of bytes read once the read operation is completed. -pub fn read(state: &ThreadSafeState, rid: ResourceId, buf: T) -> Read +pub fn read(state: &State, rid: ResourceId, buf: T) -> Read where T: AsMut<[u8]>, { @@ -151,7 +151,7 @@ pub struct Read { rid: ResourceId, buf: T, io_state: IoState, - state: ThreadSafeState, + state: State, } impl Future for Read @@ -166,8 +166,9 @@ where panic!("poll a Read after it's done"); } - let mut table = inner.state.lock_resource_table(); - let resource = table + let mut state = inner.state.borrow_mut(); + let resource = state + .resource_table .get_mut::(inner.rid) .ok_or_else(bad_resource)?; let nread = ready!(resource.poll_read(cx, &mut inner.buf.as_mut()[..]))?; @@ -177,7 +178,7 @@ where } pub fn op_read( - state: &ThreadSafeState, + state: &State, rid: i32, zero_copy: Option, ) -> Pin> { @@ -257,7 +258,7 @@ pub struct Write { rid: ResourceId, buf: T, io_state: IoState, - state: ThreadSafeState, + state: State, nwritten: i32, } @@ -266,7 +267,7 @@ pub struct Write { /// /// Any error which happens during writing will cause both the stream and the /// buffer to get destroyed. -pub fn write(state: &ThreadSafeState, rid: ResourceId, buf: T) -> Write +pub fn write(state: &State, rid: ResourceId, buf: T) -> Write where T: AsRef<[u8]>, { @@ -294,8 +295,9 @@ where } if inner.io_state == IoState::Pending { - let mut table = inner.state.lock_resource_table(); - let resource = table + let mut state = inner.state.borrow_mut(); + let resource = state + .resource_table .get_mut::(inner.rid) .ok_or_else(bad_resource)?; @@ -309,8 +311,9 @@ where // Figure out why it's needed and preferably remove it. // https://github.com/denoland/deno/issues/3565 if inner.io_state == IoState::Flush { - let mut table = inner.state.lock_resource_table(); - let resource = table + let mut state = inner.state.borrow_mut(); + let resource = state + .resource_table .get_mut::(inner.rid) .ok_or_else(bad_resource)?; ready!(resource.poll_flush(cx))?; @@ -322,7 +325,7 @@ where } pub fn op_write( - state: &ThreadSafeState, + state: &State, rid: i32, zero_copy: Option, ) -> Pin> { -- cgit v1.2.3