diff options
Diffstat (limited to 'serde_v8/magic')
-rw-r--r-- | serde_v8/magic/bytestring.rs | 80 | ||||
-rw-r--r-- | serde_v8/magic/transl8.rs | 22 | ||||
-rw-r--r-- | serde_v8/magic/u16string.rs | 48 |
3 files changed, 30 insertions, 120 deletions
diff --git a/serde_v8/magic/bytestring.rs b/serde_v8/magic/bytestring.rs index 43ac54b97..a4c664b29 100644 --- a/serde_v8/magic/bytestring.rs +++ b/serde_v8/magic/bytestring.rs @@ -1,85 +1,11 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. - -use std::ops::{Deref, DerefMut}; - use super::transl8::{FromV8, ToV8}; -use crate::magic::transl8::impl_magic; +use crate::magic::transl8::{impl_magic, impl_wrapper}; use crate::Error; -#[derive(PartialEq, Eq, Clone, Debug)] -pub struct ByteString(pub Vec<u8>); +impl_wrapper! { pub struct ByteString(Vec<u8>); } impl_magic!(ByteString); -impl ByteString { - pub fn new() -> ByteString { - ByteString(Vec::new()) - } - - pub fn with_capacity(capacity: usize) -> ByteString { - ByteString(Vec::with_capacity(capacity)) - } - - pub fn capacity(&self) -> usize { - self.0.capacity() - } - - pub fn reserve(&mut self, additional: usize) { - self.0.reserve(additional) - } - - pub fn reserve_exact(&mut self, additional: usize) { - self.0.reserve_exact(additional) - } - - pub fn shrink_to_fit(&mut self) { - self.0.shrink_to_fit() - } - - pub fn truncate(&mut self, len: usize) { - self.0.truncate(len) - } - - pub fn push(&mut self, value: u8) { - self.0.push(value) - } - - pub fn pop(&mut self) -> Option<u8> { - self.0.pop() - } -} - -impl Default for ByteString { - fn default() -> Self { - ByteString::new() - } -} - -impl Deref for ByteString { - type Target = [u8]; - - fn deref(&self) -> &[u8] { - self.0.deref() - } -} - -impl DerefMut for ByteString { - fn deref_mut(&mut self) -> &mut [u8] { - self.0.deref_mut() - } -} - -impl AsRef<[u8]> for ByteString { - fn as_ref(&self) -> &[u8] { - self.0.as_ref() - } -} - -impl AsMut<[u8]> for ByteString { - fn as_mut(&mut self) -> &mut [u8] { - self.0.as_mut() - } -} - impl ToV8 for ByteString { fn to_v8<'a>( &self, @@ -117,6 +43,6 @@ impl FromV8 for ByteString { ); assert!(written == len); } - Ok(ByteString(buffer)) + Ok(buffer.into()) } } diff --git a/serde_v8/magic/transl8.rs b/serde_v8/magic/transl8.rs index 458b82129..d08be468b 100644 --- a/serde_v8/magic/transl8.rs +++ b/serde_v8/magic/transl8.rs @@ -141,3 +141,25 @@ macro_rules! impl_magic { }; } pub(crate) use impl_magic; + +macro_rules! impl_wrapper { + ($i:item) => { + #[derive( + PartialEq, + Eq, + Clone, + Debug, + Default, + derive_more::Deref, + derive_more::DerefMut, + derive_more::AsRef, + derive_more::AsMut, + derive_more::From, + )] + #[as_mut(forward)] + #[as_ref(forward)] + #[from(forward)] + $i + }; +} +pub(crate) use impl_wrapper; diff --git a/serde_v8/magic/u16string.rs b/serde_v8/magic/u16string.rs index 39ebf88d9..c1d080ac7 100644 --- a/serde_v8/magic/u16string.rs +++ b/serde_v8/magic/u16string.rs @@ -1,49 +1,11 @@ -use crate::magic::transl8::impl_magic; +use super::transl8::{impl_magic, impl_wrapper, FromV8, ToV8}; use crate::Error; -use std::ops::{Deref, DerefMut}; -use super::transl8::{FromV8, ToV8}; - -#[derive(Default, PartialEq, Eq, Debug)] -pub struct U16String(pub Vec<u16>); +impl_wrapper!( + pub struct U16String(Vec<u16>); +); impl_magic!(U16String); -impl U16String { - pub fn with_zeroes(length: usize) -> U16String { - U16String(vec![0u16; length]) - } - - pub fn truncate(&mut self, new_length: usize) { - self.0.truncate(new_length); - self.0.shrink_to_fit() - } -} - -impl Deref for U16String { - type Target = [u16]; - fn deref(&self) -> &[u16] { - self.0.deref() - } -} - -impl DerefMut for U16String { - fn deref_mut(&mut self) -> &mut [u16] { - self.0.deref_mut() - } -} - -impl AsRef<[u16]> for U16String { - fn as_ref(&self) -> &[u16] { - self.0.as_ref() - } -} - -impl AsMut<[u16]> for U16String { - fn as_mut(&mut self) -> &mut [u16] { - self.0.as_mut() - } -} - impl ToV8 for U16String { fn to_v8<'a>( &self, @@ -78,6 +40,6 @@ impl FromV8 for U16String { ); assert!(written == len); } - Ok(U16String(buffer)) + Ok(buffer.into()) } } |