diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-06-22 23:37:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-22 23:37:56 +0200 |
commit | dda0f1c343bfb3196ce6a7c7e8c2acccfd5c2e5b (patch) | |
tree | 10fc273a620949ccf63826363499f8f39056896d /serde_v8/magic/string_or_buffer.rs | |
parent | b319fa7f4965af3d3d576ea528248a31c96a4053 (diff) |
refactor(serde_v8): split ZeroCopyBuf into JsBuffer and ToJsBuffer (#19566)
`ZeroCopyBuf` was convenient to use, but sometimes it did hide details
that some copies were necessary in certain cases. Also it made it way to easy
for the caller to pass around and convert into different values. This commit
splits `ZeroCopyBuf` into `JsBuffer` (an array buffer coming from V8) and
`ToJsBuffer` (a Rust buffer that will be converted into a V8 array buffer).
As a result some magical conversions were removed (they were never used)
limiting the API surface and preparing for changes in #19534.
Diffstat (limited to 'serde_v8/magic/string_or_buffer.rs')
-rw-r--r-- | serde_v8/magic/string_or_buffer.rs | 35 |
1 files changed, 3 insertions, 32 deletions
diff --git a/serde_v8/magic/string_or_buffer.rs b/serde_v8/magic/string_or_buffer.rs index 02571f669..986f7d32a 100644 --- a/serde_v8/magic/string_or_buffer.rs +++ b/serde_v8/magic/string_or_buffer.rs @@ -1,7 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use super::buffer::ZeroCopyBuf; +use super::buffer::JsBuffer; use super::transl8::FromV8; -use super::transl8::ToV8; use crate::error::value_to_type_str; use crate::magic::transl8::impl_magic; use crate::Error; @@ -9,7 +8,7 @@ use std::ops::Deref; #[derive(Debug)] pub enum StringOrBuffer { - Buffer(ZeroCopyBuf), + Buffer(JsBuffer), String(String), } @@ -35,40 +34,12 @@ impl<'a> TryFrom<&'a StringOrBuffer> for &'a str { } } -impl ToV8 for StringOrBuffer { - fn to_v8<'a>( - &mut self, - scope: &mut v8::HandleScope<'a>, - ) -> Result<v8::Local<'a, v8::Value>, crate::Error> { - match self { - Self::Buffer(buf) => { - let buf: Box<[u8]> = match buf { - ZeroCopyBuf::FromV8(buf) => { - let value: &[u8] = buf; - value.into() - } - ZeroCopyBuf::ToV8(ref mut x) => { - x.take().expect("ZeroCopyBuf was empty") - } - }; - let backing_store = - v8::ArrayBuffer::new_backing_store_from_boxed_slice(buf); - Ok( - v8::ArrayBuffer::with_backing_store(scope, &backing_store.into()) - .into(), - ) - } - Self::String(s) => crate::to_v8(scope, s), - } - } -} - impl FromV8 for StringOrBuffer { fn from_v8( scope: &mut v8::HandleScope, value: v8::Local<v8::Value>, ) -> Result<Self, crate::Error> { - if let Ok(buf) = ZeroCopyBuf::from_v8(scope, value) { + if let Ok(buf) = JsBuffer::from_v8(scope, value) { return Ok(Self::Buffer(buf)); } else if let Ok(s) = crate::from_v8(scope, value) { return Ok(Self::String(s)); |