1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

refactor: remove remaining references to ZeroCopyBuf (#19598)

This commit is contained in:
Bartek Iwańczuk 2023-06-24 09:30:41 +02:00 committed by GitHub
parent f81027ae9f
commit 76bff33a19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 20 deletions

View file

@ -7,7 +7,7 @@ use bytes::Buf;
use serde_v8::JsBuffer;
/// BufView is a wrapper around an underlying contiguous chunk of bytes. It can
/// be created from a [ZeroCopyBuf], [bytes::Bytes], or [Vec<u8>] and implements
/// be created from a [JsBuffer], [bytes::Bytes], or [Vec<u8>] and implements
/// `Deref<[u8]>` and `AsRef<[u8]>`.
///
/// The wrapper has the ability to constrain the exposed view to a sub-region of
@ -22,7 +22,7 @@ pub struct BufView {
enum BufViewInner {
Empty,
Bytes(bytes::Bytes),
ZeroCopy(JsBuffer),
JsBuffer(JsBuffer),
Vec(Vec<u8>),
}
@ -41,7 +41,7 @@ impl BufView {
match &self.inner {
BufViewInner::Empty => 0,
BufViewInner::Bytes(bytes) => bytes.len() - self.cursor,
BufViewInner::ZeroCopy(zero_copy) => zero_copy.len() - self.cursor,
BufViewInner::JsBuffer(js_buf) => js_buf.len() - self.cursor,
BufViewInner::Vec(vec) => vec.len() - self.cursor,
}
}
@ -87,7 +87,7 @@ impl Deref for BufView {
let buf = match &self.inner {
BufViewInner::Empty => &[],
BufViewInner::Bytes(bytes) => bytes.deref(),
BufViewInner::ZeroCopy(zero_copy) => zero_copy.deref(),
BufViewInner::JsBuffer(js_buf) => js_buf.deref(),
BufViewInner::Vec(vec) => vec.deref(),
};
&buf[self.cursor..]
@ -102,7 +102,7 @@ impl AsRef<[u8]> for BufView {
impl From<JsBuffer> for BufView {
fn from(buf: JsBuffer) -> Self {
Self::from_inner(BufViewInner::ZeroCopy(buf))
Self::from_inner(BufViewInner::JsBuffer(buf))
}
}
@ -123,14 +123,14 @@ impl From<BufView> for bytes::Bytes {
match buf.inner {
BufViewInner::Empty => bytes::Bytes::new(),
BufViewInner::Bytes(bytes) => bytes,
BufViewInner::ZeroCopy(zero_copy) => zero_copy.into(),
BufViewInner::JsBuffer(js_buf) => js_buf.into(),
BufViewInner::Vec(vec) => vec.into(),
}
}
}
/// BufMutView is a wrapper around an underlying contiguous chunk of writable
/// bytes. It can be created from a `ZeroCopyBuf` or a `Vec<u8>` and implements
/// bytes. It can be created from a `JsBuffer` or a `Vec<u8>` and implements
/// `DerefMut<[u8]>` and `AsMut<[u8]>`.
///
/// The wrapper has the ability to constrain the exposed view to a sub-region of
@ -145,7 +145,7 @@ pub struct BufMutView {
}
enum BufMutViewInner {
ZeroCopy(JsBuffer),
JsBuffer(JsBuffer),
Vec(Vec<u8>),
}
@ -162,7 +162,7 @@ impl BufMutView {
/// buffer minus the cursor position.
pub fn len(&self) -> usize {
match &self.inner {
BufMutViewInner::ZeroCopy(zero_copy) => zero_copy.len() - self.cursor,
BufMutViewInner::JsBuffer(js_buf) => js_buf.len() - self.cursor,
BufMutViewInner::Vec(vec) => vec.len() - self.cursor,
}
}
@ -189,7 +189,7 @@ impl BufMutView {
/// Turn this `BufMutView` into a `BufView`.
pub fn into_view(self) -> BufView {
let inner = match self.inner {
BufMutViewInner::ZeroCopy(zero_copy) => BufViewInner::ZeroCopy(zero_copy),
BufMutViewInner::JsBuffer(js_buf) => BufViewInner::JsBuffer(js_buf),
BufMutViewInner::Vec(vec) => BufViewInner::Vec(vec),
};
BufView {
@ -201,11 +201,11 @@ impl BufMutView {
/// Unwrap the underlying buffer into a `Vec<u8>`, consuming the `BufMutView`.
///
/// This method panics when called on a `BufMutView` that was created from a
/// `ZeroCopyBuf`.
/// `JsBuffer`.
pub fn unwrap_vec(self) -> Vec<u8> {
match self.inner {
BufMutViewInner::ZeroCopy(_) => {
panic!("Cannot unwrap a ZeroCopyBuf backed BufMutView into a Vec");
BufMutViewInner::JsBuffer(_) => {
panic!("Cannot unwrap a JsBuffer backed BufMutView into a Vec");
}
BufMutViewInner::Vec(vec) => vec,
}
@ -214,11 +214,11 @@ impl BufMutView {
/// Get a mutable reference to an underlying `Vec<u8>`.
///
/// This method panics when called on a `BufMutView` that was created from a
/// `ZeroCopyBuf`.
/// `JsBuffer`.
pub fn get_mut_vec(&mut self) -> &mut Vec<u8> {
match &mut self.inner {
BufMutViewInner::ZeroCopy(_) => {
panic!("Cannot unwrap a ZeroCopyBuf backed BufMutView into a Vec");
BufMutViewInner::JsBuffer(_) => {
panic!("Cannot unwrap a JsBuffer backed BufMutView into a Vec");
}
BufMutViewInner::Vec(vec) => vec,
}
@ -244,7 +244,7 @@ impl Deref for BufMutView {
fn deref(&self) -> &[u8] {
let buf = match &self.inner {
BufMutViewInner::ZeroCopy(zero_copy) => zero_copy.deref(),
BufMutViewInner::JsBuffer(js_buf) => js_buf.deref(),
BufMutViewInner::Vec(vec) => vec.deref(),
};
&buf[self.cursor..]
@ -254,7 +254,7 @@ impl Deref for BufMutView {
impl DerefMut for BufMutView {
fn deref_mut(&mut self) -> &mut [u8] {
let buf = match &mut self.inner {
BufMutViewInner::ZeroCopy(zero_copy) => zero_copy.deref_mut(),
BufMutViewInner::JsBuffer(js_buf) => js_buf.deref_mut(),
BufMutViewInner::Vec(vec) => vec.deref_mut(),
};
&mut buf[self.cursor..]
@ -275,7 +275,7 @@ impl AsMut<[u8]> for BufMutView {
impl From<JsBuffer> for BufMutView {
fn from(buf: JsBuffer) -> Self {
Self::from_inner(BufMutViewInner::ZeroCopy(buf))
Self::from_inner(BufMutViewInner::JsBuffer(buf))
}
}

View file

@ -130,7 +130,7 @@ fn magic_buffer() {
let eq = js_exec(scope, "t3.b[4] === 11");
assert!(eq.is_true());
// ZeroCopyBuf as bytes::Bytes
// JsBuffer as bytes::Bytes
let v8_array = js_exec(scope, "new Uint8Array([1,2,3,4,5])");
let zbuf: serde_v8::JsBuffer = serde_v8::from_v8(scope, v8_array).unwrap();
let buf: bytes::Bytes = zbuf.into();