cleanup(serde_v8): SerializablePkg verbosity (#13855)

This commit is contained in:
Aaron O'Mullan 2022-03-07 11:12:44 +01:00 committed by Yoshiya Hinosawa
parent f5773cd073
commit 4bdb76db68
No known key found for this signature in database
GPG Key ID: 0E8BFAA8A5B4E92B

View File

@ -61,55 +61,59 @@ pub enum 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
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),
Self::Unit => ().serialize(s),
Self::Bool(x) => x.serialize(s),
Self::Int8(x) => x.serialize(s),
Self::Int16(x) => x.serialize(s),
Self::Int32(x) => x.serialize(s),
Self::Int64(x) => x.serialize(s),
Self::UInt8(x) => x.serialize(s),
Self::UInt16(x) => x.serialize(s),
Self::UInt32(x) => x.serialize(s),
Self::UInt64(x) => x.serialize(s),
Self::Float32(x) => x.serialize(s),
Self::Float64(x) => x.serialize(s),
}
}
}
impl<T: serde::Serialize + 'static> From<T> for SerializablePkg {
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::<()>() {
Self::Primitive(Primitive::Unit)
} 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>() {
Self::Primitive(Primitive::Int8(unsafe { transmute_copy(&x) }))
Self::Primitive(Primitive::Int8(tc(x)))
} 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>() {
Self::Primitive(Primitive::Int32(unsafe { transmute_copy(&x) }))
Self::Primitive(Primitive::Int32(tc(x)))
} 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>() {
Self::Primitive(Primitive::UInt8(unsafe { transmute_copy(&x) }))
Self::Primitive(Primitive::UInt8(tc(x)))
} 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>() {
Self::Primitive(Primitive::UInt32(unsafe { transmute_copy(&x) }))
Self::Primitive(Primitive::UInt32(tc(x)))
} 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>() {
Self::Primitive(Primitive::Float32(unsafe { transmute_copy(&x) }))
Self::Primitive(Primitive::Float32(tc(x)))
} else if tid == TypeId::of::<f64>() {
Self::Primitive(Primitive::Float64(unsafe { transmute_copy(&x) }))
Self::Primitive(Primitive::Float64(tc(x)))
} else {
Self::Serializable(Box::new(x))
}