1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-08 15:19:40 -05:00

cleanup(serde_v8): SerializablePkg verbosity (#13855)

This commit is contained in:
Aaron O'Mullan 2022-03-07 11:12:44 +01:00 committed by GitHub
parent 94c8ecfaaa
commit 0b9da1aa7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -61,55 +61,59 @@ pub enum Primitive {
} }
impl serde::Serialize for Primitive { impl serde::Serialize for Primitive {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where where
S: serde::Serializer, S: serde::Serializer,
{ {
match *self { match *self {
Self::Unit => serializer.serialize_unit(), Self::Unit => ().serialize(s),
Self::Bool(x) => serializer.serialize_bool(x), Self::Bool(x) => x.serialize(s),
Self::Int8(x) => serializer.serialize_i8(x), Self::Int8(x) => x.serialize(s),
Self::Int16(x) => serializer.serialize_i16(x), Self::Int16(x) => x.serialize(s),
Self::Int32(x) => serializer.serialize_i32(x), Self::Int32(x) => x.serialize(s),
Self::Int64(x) => serializer.serialize_i64(x), Self::Int64(x) => x.serialize(s),
Self::UInt8(x) => serializer.serialize_u8(x), Self::UInt8(x) => x.serialize(s),
Self::UInt16(x) => serializer.serialize_u16(x), Self::UInt16(x) => x.serialize(s),
Self::UInt32(x) => serializer.serialize_u32(x), Self::UInt32(x) => x.serialize(s),
Self::UInt64(x) => serializer.serialize_u64(x), Self::UInt64(x) => x.serialize(s),
Self::Float32(x) => serializer.serialize_f32(x), Self::Float32(x) => x.serialize(s),
Self::Float64(x) => serializer.serialize_f64(x), Self::Float64(x) => x.serialize(s),
} }
} }
} }
impl<T: serde::Serialize + 'static> From<T> for SerializablePkg { impl<T: serde::Serialize + 'static> From<T> for SerializablePkg {
fn from(x: T) -> Self { fn from(x: T) -> Self {
let tid = TypeId::of::<T>(); #[inline(always)]
fn tc<T, U>(src: T) -> U {
unsafe { transmute_copy(&src) }
}
let tid = TypeId::of::<T>();
if tid == TypeId::of::<()>() { if tid == TypeId::of::<()>() {
Self::Primitive(Primitive::Unit) Self::Primitive(Primitive::Unit)
} else if tid == TypeId::of::<bool>() { } else if tid == TypeId::of::<bool>() {
Self::Primitive(Primitive::Bool(unsafe { transmute_copy(&x) })) Self::Primitive(Primitive::Bool(tc(x)))
} else if tid == TypeId::of::<i8>() { } else if tid == TypeId::of::<i8>() {
Self::Primitive(Primitive::Int8(unsafe { transmute_copy(&x) })) Self::Primitive(Primitive::Int8(tc(x)))
} else if tid == TypeId::of::<i16>() { } else if tid == TypeId::of::<i16>() {
Self::Primitive(Primitive::Int16(unsafe { transmute_copy(&x) })) Self::Primitive(Primitive::Int16(tc(x)))
} else if tid == TypeId::of::<i32>() { } else if tid == TypeId::of::<i32>() {
Self::Primitive(Primitive::Int32(unsafe { transmute_copy(&x) })) Self::Primitive(Primitive::Int32(tc(x)))
} else if tid == TypeId::of::<i64>() { } else if tid == TypeId::of::<i64>() {
Self::Primitive(Primitive::Int64(unsafe { transmute_copy(&x) })) Self::Primitive(Primitive::Int64(tc(x)))
} else if tid == TypeId::of::<u8>() { } else if tid == TypeId::of::<u8>() {
Self::Primitive(Primitive::UInt8(unsafe { transmute_copy(&x) })) Self::Primitive(Primitive::UInt8(tc(x)))
} else if tid == TypeId::of::<u16>() { } else if tid == TypeId::of::<u16>() {
Self::Primitive(Primitive::UInt16(unsafe { transmute_copy(&x) })) Self::Primitive(Primitive::UInt16(tc(x)))
} else if tid == TypeId::of::<u32>() { } else if tid == TypeId::of::<u32>() {
Self::Primitive(Primitive::UInt32(unsafe { transmute_copy(&x) })) Self::Primitive(Primitive::UInt32(tc(x)))
} else if tid == TypeId::of::<u64>() { } else if tid == TypeId::of::<u64>() {
Self::Primitive(Primitive::UInt64(unsafe { transmute_copy(&x) })) Self::Primitive(Primitive::UInt64(tc(x)))
} else if tid == TypeId::of::<f32>() { } else if tid == TypeId::of::<f32>() {
Self::Primitive(Primitive::Float32(unsafe { transmute_copy(&x) })) Self::Primitive(Primitive::Float32(tc(x)))
} else if tid == TypeId::of::<f64>() { } else if tid == TypeId::of::<f64>() {
Self::Primitive(Primitive::Float64(unsafe { transmute_copy(&x) })) Self::Primitive(Primitive::Float64(tc(x)))
} else { } else {
Self::Serializable(Box::new(x)) Self::Serializable(Box::new(x))
} }