summaryrefslogtreecommitdiff
path: root/cli/ops/io.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-02-08 20:34:31 +0100
committerGitHub <noreply@github.com>2020-02-08 20:34:31 +0100
commitcdba5ab6fc633606aaa6f95d0825832c3ac6fe5c (patch)
treee8dee2801e14b65b2da6aca62e39cd3d3ac2a786 /cli/ops/io.rs
parent619a24390ff15d5ea5e577a4d0391823f94e8592 (diff)
refactor: rename ThreadSafeState, use RefCell for mutable state (#3931)
* rename ThreadSafeState to State * State stores InnerState wrapped in Rc and RefCell
Diffstat (limited to 'cli/ops/io.rs')
-rw-r--r--cli/ops/io.rs31
1 files changed, 17 insertions, 14 deletions
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<T>(state: &ThreadSafeState, rid: ResourceId, buf: T) -> Read<T>
+pub fn read<T>(state: &State, rid: ResourceId, buf: T) -> Read<T>
where
T: AsMut<[u8]>,
{
@@ -151,7 +151,7 @@ pub struct Read<T> {
rid: ResourceId,
buf: T,
io_state: IoState,
- state: ThreadSafeState,
+ state: State,
}
impl<T> Future for Read<T>
@@ -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::<StreamResource>(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<ZeroCopyBuf>,
) -> Pin<Box<MinimalOp>> {
@@ -257,7 +258,7 @@ pub struct Write<T> {
rid: ResourceId,
buf: T,
io_state: IoState,
- state: ThreadSafeState,
+ state: State,
nwritten: i32,
}
@@ -266,7 +267,7 @@ pub struct Write<T> {
///
/// Any error which happens during writing will cause both the stream and the
/// buffer to get destroyed.
-pub fn write<T>(state: &ThreadSafeState, rid: ResourceId, buf: T) -> Write<T>
+pub fn write<T>(state: &State, rid: ResourceId, buf: T) -> Write<T>
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::<StreamResource>(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::<StreamResource>(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<ZeroCopyBuf>,
) -> Pin<Box<MinimalOp>> {