rw_deno_core/io/resource.rs
1// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
3// Think of Resources as File Descriptors. They are integers that are allocated
4// by the privileged side of Deno which refer to various rust objects that need
5// to be persisted between various ops. For example, network sockets are
6// resources. Resources may or may not correspond to a real operating system
7// file descriptor (hence the different name).
8
9use crate::error::not_supported;
10use crate::io::AsyncResult;
11use crate::io::BufMutView;
12use crate::io::BufView;
13use crate::io::WriteOutcome;
14use crate::ResourceHandle;
15use crate::ResourceHandleFd;
16use anyhow::Error;
17use std::any::type_name;
18use std::any::Any;
19use std::any::TypeId;
20use std::borrow::Cow;
21use std::rc::Rc;
22
23/// Resources are Rust objects that are attached to a [deno_core::JsRuntime].
24/// They are identified in JS by a numeric ID (the resource ID, or rid).
25/// Resources can be created in ops. Resources can also be retrieved in ops by
26/// their rid. Resources are not thread-safe - they can only be accessed from
27/// the thread that the JsRuntime lives on.
28///
29/// Resources are reference counted in Rust. This means that they can be
30/// cloned and passed around. When the last reference is dropped, the resource
31/// is automatically closed. As long as the resource exists in the resource
32/// table, the reference count is at least 1.
33///
34/// ### Readable
35///
36/// Readable resources are resources that can have data read from. Examples of
37/// this are files, sockets, or HTTP streams.
38///
39/// Readables can be read from from either JS or Rust. In JS one can use
40/// `Deno.core.read()` to read from a single chunk of data from a readable. In
41/// Rust one can directly call `read()` or `read_byob()`. The Rust side code is
42/// used to implement ops like `op_slice`.
43///
44/// A distinction can be made between readables that produce chunks of data
45/// themselves (they allocate the chunks), and readables that fill up
46/// bring-your-own-buffers (BYOBs). The former is often the case for framed
47/// protocols like HTTP, while the latter is often the case for kernel backed
48/// resources like files and sockets.
49///
50/// All readables must implement `read()`. If resources can support an optimized
51/// path for BYOBs, they should also implement `read_byob()`. For kernel backed
52/// resources it often makes sense to implement `read_byob()` first, and then
53/// implement `read()` as an operation that allocates a new chunk with
54/// `len == limit`, then calls `read_byob()`, and then returns a chunk sliced to
55/// the number of bytes read. Kernel backed resources can use the
56/// [deno_core::impl_readable_byob] macro to implement optimized `read_byob()`
57/// and `read()` implementations from a single `Self::read()` method.
58///
59/// ### Writable
60///
61/// Writable resources are resources that can have data written to. Examples of
62/// this are files, sockets, or HTTP streams.
63///
64/// Writables can be written to from either JS or Rust. In JS one can use
65/// `Deno.core.write()` to write to a single chunk of data to a writable. In
66/// Rust one can directly call `write()`. The latter is used to implement ops
67/// like `op_slice`.
68pub trait Resource: Any + 'static {
69 /// Returns a string representation of the resource which is made available
70 /// to JavaScript code through `op_resources`. The default implementation
71 /// returns the Rust type name, but specific resource types may override this
72 /// trait method.
73 fn name(&self) -> Cow<str> {
74 type_name::<Self>().into()
75 }
76
77 /// Read a single chunk of data from the resource. This operation returns a
78 /// `BufView` that represents the data that was read. If a zero length buffer
79 /// is returned, it indicates that the resource has reached EOF.
80 ///
81 /// If this method is not implemented, the default implementation will error
82 /// with a "not supported" error.
83 ///
84 /// If a readable can provide an optimized path for BYOBs, it should also
85 /// implement `read_byob()`.
86 fn read(self: Rc<Self>, limit: usize) -> AsyncResult<BufView> {
87 _ = limit;
88 Box::pin(futures::future::err(not_supported()))
89 }
90
91 /// Read a single chunk of data from the resource into the provided `BufMutView`.
92 ///
93 /// This operation returns the number of bytes read. If zero bytes are read,
94 /// it indicates that the resource has reached EOF.
95 ///
96 /// If this method is not implemented explicitly, the default implementation
97 /// will call `read()` and then copy the data into the provided buffer. For
98 /// readable resources that can provide an optimized path for BYOBs, it is
99 /// strongly recommended to override this method.
100 fn read_byob(
101 self: Rc<Self>,
102 mut buf: BufMutView,
103 ) -> AsyncResult<(usize, BufMutView)> {
104 Box::pin(async move {
105 let read = self.read(buf.len()).await?;
106 let nread = read.len();
107 buf[..nread].copy_from_slice(&read);
108 Ok((nread, buf))
109 })
110 }
111
112 /// Write an error state to this resource, if the resource supports it.
113 fn write_error(self: Rc<Self>, _error: Error) -> AsyncResult<()> {
114 Box::pin(futures::future::err(not_supported()))
115 }
116
117 /// Write a single chunk of data to the resource. The operation may not be
118 /// able to write the entire chunk, in which case it should return the number
119 /// of bytes written. Additionally it should return the `BufView` that was
120 /// passed in.
121 ///
122 /// If this method is not implemented, the default implementation will error
123 /// with a "not supported" error.
124 fn write(self: Rc<Self>, buf: BufView) -> AsyncResult<WriteOutcome> {
125 _ = buf;
126 Box::pin(futures::future::err(not_supported()))
127 }
128
129 /// Write an entire chunk of data to the resource. Unlike `write()`, this will
130 /// ensure the entire chunk is written. If the operation is not able to write
131 /// the entire chunk, an error is to be returned.
132 ///
133 /// By default this method will call `write()` repeatedly until the entire
134 /// chunk is written. Resources that can write the entire chunk in a single
135 /// operation using an optimized path should override this method.
136 fn write_all(self: Rc<Self>, view: BufView) -> AsyncResult<()> {
137 Box::pin(async move {
138 let mut view = view;
139 let this = self;
140 while !view.is_empty() {
141 let resp = this.clone().write(view).await?;
142 match resp {
143 WriteOutcome::Partial {
144 nwritten,
145 view: new_view,
146 } => {
147 view = new_view;
148 view.advance_cursor(nwritten);
149 }
150 WriteOutcome::Full { .. } => break,
151 }
152 }
153 Ok(())
154 })
155 }
156
157 /// The same as [`read_byob()`][Resource::read_byob], but synchronous.
158 fn read_byob_sync(self: Rc<Self>, data: &mut [u8]) -> Result<usize, Error> {
159 _ = data;
160 Err(not_supported())
161 }
162
163 /// The same as [`write()`][Resource::write], but synchronous.
164 fn write_sync(self: Rc<Self>, data: &[u8]) -> Result<usize, Error> {
165 _ = data;
166 Err(not_supported())
167 }
168
169 /// The shutdown method can be used to asynchronously close the resource. It
170 /// is not automatically called when the resource is dropped or closed.
171 ///
172 /// If this method is not implemented, the default implementation will error
173 /// with a "not supported" error.
174 fn shutdown(self: Rc<Self>) -> AsyncResult<()> {
175 Box::pin(futures::future::err(not_supported()))
176 }
177
178 /// Resources may implement the `close()` trait method if they need to do
179 /// resource specific clean-ups, such as cancelling pending futures, after a
180 /// resource has been removed from the resource table.
181 fn close(self: Rc<Self>) {}
182
183 /// Resources backed by a file descriptor or socket handle can let ops know
184 /// to allow for low-level optimizations.
185 fn backing_handle(self: Rc<Self>) -> Option<ResourceHandle> {
186 #[allow(deprecated)]
187 self.backing_fd().map(ResourceHandle::Fd)
188 }
189
190 /// Resources backed by a file descriptor can let ops know to allow for
191 /// low-level optimizations.
192 #[deprecated = "Use backing_handle"]
193 fn backing_fd(self: Rc<Self>) -> Option<ResourceHandleFd> {
194 None
195 }
196
197 fn size_hint(&self) -> (u64, Option<u64>) {
198 (0, None)
199 }
200}
201
202impl dyn Resource {
203 #[inline(always)]
204 fn is<T: Resource>(&self) -> bool {
205 self.type_id() == TypeId::of::<T>()
206 }
207
208 #[inline(always)]
209 #[allow(clippy::needless_lifetimes)]
210 pub fn downcast_rc<'a, T: Resource>(self: &'a Rc<Self>) -> Option<&'a Rc<T>> {
211 if self.is::<T>() {
212 let ptr = self as *const Rc<_> as *const Rc<T>;
213 // TODO(piscisaureus): safety comment
214 #[allow(clippy::undocumented_unsafe_blocks)]
215 Some(unsafe { &*ptr })
216 } else {
217 None
218 }
219 }
220}
221
222#[macro_export]
223macro_rules! impl_readable_byob {
224 () => {
225 fn read(
226 self: ::std::rc::Rc<Self>,
227 limit: ::core::primitive::usize,
228 ) -> AsyncResult<$crate::BufView> {
229 ::std::boxed::Box::pin(async move {
230 let mut vec = ::std::vec![0; limit];
231 let nread = self.read(&mut vec).await?;
232 if nread != vec.len() {
233 vec.truncate(nread);
234 }
235 let view = $crate::BufView::from(vec);
236 ::std::result::Result::Ok(view)
237 })
238 }
239
240 fn read_byob(
241 self: ::std::rc::Rc<Self>,
242 mut buf: $crate::BufMutView,
243 ) -> AsyncResult<(::core::primitive::usize, $crate::BufMutView)> {
244 ::std::boxed::Box::pin(async move {
245 let nread = self.read(buf.as_mut()).await?;
246 ::std::result::Result::Ok((nread, buf))
247 })
248 }
249 };
250}
251
252#[macro_export]
253macro_rules! impl_writable {
254 (__write) => {
255 fn write(
256 self: ::std::rc::Rc<Self>,
257 view: $crate::BufView,
258 ) -> $crate::AsyncResult<$crate::WriteOutcome> {
259 ::std::boxed::Box::pin(async move {
260 let nwritten = self.write(&view).await?;
261 ::std::result::Result::Ok($crate::WriteOutcome::Partial {
262 nwritten,
263 view,
264 })
265 })
266 }
267 };
268 (__write_all) => {
269 fn write_all(
270 self: ::std::rc::Rc<Self>,
271 view: $crate::BufView,
272 ) -> $crate::AsyncResult<()> {
273 ::std::boxed::Box::pin(async move {
274 self.write_all(&view).await?;
275 ::std::result::Result::Ok(())
276 })
277 }
278 };
279 () => {
280 $crate::impl_writable!(__write);
281 };
282 (with_all) => {
283 $crate::impl_writable!(__write);
284 $crate::impl_writable!(__write_all);
285 };
286}