diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-04-19 15:19:49 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-19 15:19:49 +0200 |
commit | 167f017ca0a09417325c4f20d8ae08d9f6092e4b (patch) | |
tree | a3d9bf4f5517766e13a355f50d2858d6162c14b6 | |
parent | 65a2a04d3bf116c1dee8d2528ec18e71ca4d7bf2 (diff) |
refactor(core): move SerializablePkg to serde_v8 (#10231)
-rw-r--r-- | core/lib.rs | 1 | ||||
-rw-r--r-- | core/minvalue.rs | 95 | ||||
-rw-r--r-- | core/ops.rs | 3 | ||||
-rw-r--r-- | serde_v8/src/lib.rs | 2 | ||||
-rw-r--r-- | serde_v8/src/serializable.rs | 95 |
5 files changed, 97 insertions, 99 deletions
diff --git a/core/lib.rs b/core/lib.rs index bc9d50e3f..b49de3b7b 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -5,7 +5,6 @@ mod bindings; pub mod error; mod flags; mod gotham_state; -mod minvalue; mod module_specifier; mod modules; mod normalize_path; diff --git a/core/minvalue.rs b/core/minvalue.rs deleted file mode 100644 index 1c9059c12..000000000 --- a/core/minvalue.rs +++ /dev/null @@ -1,95 +0,0 @@ -use rusty_v8 as v8; -use std::any::TypeId; - -/// SerializablePkg exists to provide a fast path for op returns, -/// allowing them to avoid boxing primtives (ints/floats/bool/unit/...) -pub enum SerializablePkg { - MinValue(MinValue), - Serializable(Box<dyn serde_v8::Serializable>), -} - -impl SerializablePkg { - pub fn to_v8<'a>( - &self, - scope: &mut v8::HandleScope<'a>, - ) -> Result<v8::Local<'a, v8::Value>, serde_v8::Error> { - match &*self { - Self::MinValue(x) => serde_v8::to_v8(scope, x), - Self::Serializable(x) => x.to_v8(scope), - } - } -} - -/// MinValue serves as a lightweight serializable wrapper around primitives -/// so that we can use them for async values -#[derive(Clone, Copy)] -pub enum MinValue { - Unit(()), - Bool(bool), - Int8(i8), - Int16(i16), - Int32(i32), - Int64(i64), - UInt8(u8), - UInt16(u16), - UInt32(u32), - UInt64(u64), - Float32(f32), - Float64(f64), -} - -impl serde::Serialize for MinValue { - fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> - where - S: serde::Serializer, - { - match *self { - Self::Unit(_) => serializer.serialize_unit(), - Self::Bool(x) => serializer.serialize_bool(x), - Self::Int8(x) => serializer.serialize_i8(x), - Self::Int16(x) => serializer.serialize_i16(x), - Self::Int32(x) => serializer.serialize_i32(x), - Self::Int64(x) => serializer.serialize_i64(x), - Self::UInt8(x) => serializer.serialize_u8(x), - Self::UInt16(x) => serializer.serialize_u16(x), - Self::UInt32(x) => serializer.serialize_u32(x), - Self::UInt64(x) => serializer.serialize_u64(x), - Self::Float32(x) => serializer.serialize_f32(x), - Self::Float64(x) => serializer.serialize_f64(x), - } - } -} - -impl<T: serde::Serialize + 'static> From<T> for SerializablePkg { - fn from(x: T) -> Self { - let tid = TypeId::of::<T>(); - - if tid == TypeId::of::<()>() { - Self::MinValue(MinValue::Unit(())) - } else if tid == TypeId::of::<bool>() { - Self::MinValue(MinValue::Bool(unsafe { std::mem::transmute_copy(&x) })) - } else if tid == TypeId::of::<i8>() { - Self::MinValue(MinValue::Int8(unsafe { std::mem::transmute_copy(&x) })) - } else if tid == TypeId::of::<i16>() { - Self::MinValue(MinValue::Int16(unsafe { std::mem::transmute_copy(&x) })) - } else if tid == TypeId::of::<i32>() { - Self::MinValue(MinValue::Int32(unsafe { std::mem::transmute_copy(&x) })) - } else if tid == TypeId::of::<i64>() { - Self::MinValue(MinValue::Int64(unsafe { std::mem::transmute_copy(&x) })) - } else if tid == TypeId::of::<u8>() { - Self::MinValue(MinValue::UInt8(unsafe { std::mem::transmute_copy(&x) })) - } else if tid == TypeId::of::<u16>() { - Self::MinValue(MinValue::UInt16(unsafe { std::mem::transmute_copy(&x) })) - } else if tid == TypeId::of::<u32>() { - Self::MinValue(MinValue::UInt32(unsafe { std::mem::transmute_copy(&x) })) - } else if tid == TypeId::of::<u64>() { - Self::MinValue(MinValue::UInt64(unsafe { std::mem::transmute_copy(&x) })) - } else if tid == TypeId::of::<f32>() { - Self::MinValue(MinValue::Float32(unsafe { std::mem::transmute_copy(&x) })) - } else if tid == TypeId::of::<f64>() { - Self::MinValue(MinValue::Float64(unsafe { std::mem::transmute_copy(&x) })) - } else { - Self::Serializable(Box::new(x)) - } - } -} diff --git a/core/ops.rs b/core/ops.rs index 0c9e19292..ab6938a17 100644 --- a/core/ops.rs +++ b/core/ops.rs @@ -4,7 +4,6 @@ use crate::error::bad_resource_id; use crate::error::type_error; use crate::error::AnyError; use crate::gotham_state::GothamState; -use crate::minvalue::SerializablePkg; use crate::resources::ResourceId; use crate::resources::ResourceTable; use crate::runtime::GetErrorClassFn; @@ -76,7 +75,7 @@ pub enum Op { } pub enum OpResult { - Ok(SerializablePkg), + Ok(serde_v8::SerializablePkg), Err(OpError), } diff --git a/serde_v8/src/lib.rs b/serde_v8/src/lib.rs index bd4b00b32..2017f0e87 100644 --- a/serde_v8/src/lib.rs +++ b/serde_v8/src/lib.rs @@ -13,4 +13,4 @@ pub use error::{Error, Result}; pub use keys::KeyCache; pub use magic::Value; pub use ser::{to_v8, Serializer}; -pub use serializable::Serializable; +pub use serializable::{Serializable, SerializablePkg}; diff --git a/serde_v8/src/serializable.rs b/serde_v8/src/serializable.rs index 2e781340b..198f862e0 100644 --- a/serde_v8/src/serializable.rs +++ b/serde_v8/src/serializable.rs @@ -1,4 +1,6 @@ use rusty_v8 as v8; +use std::any::TypeId; +use std::mem::transmute_copy; /// Serializable exists to allow boxing values as "objects" to be serialized later, /// this is particularly useful for async op-responses. This trait is a more efficient @@ -20,3 +22,96 @@ impl<T: serde::Serialize> Serializable for T { crate::to_v8(scope, self) } } + +/// SerializablePkg exists to provide a fast path for op returns, +/// allowing them to avoid boxing primtives (ints/floats/bool/unit/...) +pub enum SerializablePkg { + Primitive(Primitive), + Serializable(Box<dyn Serializable>), +} + +impl SerializablePkg { + pub fn to_v8<'a>( + &self, + scope: &mut v8::HandleScope<'a>, + ) -> Result<v8::Local<'a, v8::Value>, crate::Error> { + match &*self { + Self::Primitive(x) => crate::to_v8(scope, x), + Self::Serializable(x) => x.to_v8(scope), + } + } +} + +/// Primitive serves as a lightweight serializable wrapper around primitives +/// so that we can use them for async values +#[derive(Clone, Copy)] +pub enum Primitive { + Unit, + Bool(bool), + Int8(i8), + Int16(i16), + Int32(i32), + Int64(i64), + UInt8(u8), + UInt16(u16), + UInt32(u32), + UInt64(u64), + Float32(f32), + Float64(f64), +} + +impl serde::Serialize for Primitive { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: serde::Serializer, + { + match *self { + Self::Unit => serializer.serialize_unit(), + Self::Bool(x) => serializer.serialize_bool(x), + Self::Int8(x) => serializer.serialize_i8(x), + Self::Int16(x) => serializer.serialize_i16(x), + Self::Int32(x) => serializer.serialize_i32(x), + Self::Int64(x) => serializer.serialize_i64(x), + Self::UInt8(x) => serializer.serialize_u8(x), + Self::UInt16(x) => serializer.serialize_u16(x), + Self::UInt32(x) => serializer.serialize_u32(x), + Self::UInt64(x) => serializer.serialize_u64(x), + Self::Float32(x) => serializer.serialize_f32(x), + Self::Float64(x) => serializer.serialize_f64(x), + } + } +} + +impl<T: serde::Serialize + 'static> From<T> for SerializablePkg { + fn from(x: T) -> Self { + let tid = TypeId::of::<T>(); + + if tid == TypeId::of::<()>() { + Self::Primitive(Primitive::Unit) + } else if tid == TypeId::of::<bool>() { + Self::Primitive(Primitive::Bool(unsafe { transmute_copy(&x) })) + } else if tid == TypeId::of::<i8>() { + Self::Primitive(Primitive::Int8(unsafe { transmute_copy(&x) })) + } else if tid == TypeId::of::<i16>() { + Self::Primitive(Primitive::Int16(unsafe { transmute_copy(&x) })) + } else if tid == TypeId::of::<i32>() { + Self::Primitive(Primitive::Int32(unsafe { transmute_copy(&x) })) + } else if tid == TypeId::of::<i64>() { + Self::Primitive(Primitive::Int64(unsafe { transmute_copy(&x) })) + } else if tid == TypeId::of::<u8>() { + Self::Primitive(Primitive::UInt8(unsafe { transmute_copy(&x) })) + } else if tid == TypeId::of::<u16>() { + Self::Primitive(Primitive::UInt16(unsafe { transmute_copy(&x) })) + } else if tid == TypeId::of::<u32>() { + Self::Primitive(Primitive::UInt32(unsafe { transmute_copy(&x) })) + } else if tid == TypeId::of::<u64>() { + Self::Primitive(Primitive::UInt64(unsafe { transmute_copy(&x) })) + } else if tid == TypeId::of::<f32>() { + Self::Primitive(Primitive::Float32(unsafe { transmute_copy(&x) })) + } else if tid == TypeId::of::<f64>() { + Self::Primitive(Primitive::Float64(unsafe { transmute_copy(&x) })) + } else { + Self::Serializable(Box::new(x)) + } + } +} |