1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use crate::error::{Error, Result};
use serde::ser::{Impossible, Serialize, Serializer};
/// All serde_v8 "magic" values are reduced to structs with 1 or 2 u64 fields
/// assuming usize==u64, most types are simply a pointer or pointer+len (e.g: Box<T>)
pub type TransmutedField = u64;
pub type FieldResult = Result<TransmutedField>;
macro_rules! not_reachable {
($($name:ident($ty:ty);)*) => {
$(fn $name(self, _v: $ty) -> FieldResult {
unreachable!();
})*
};
}
/// FieldSerializer is a simple serde::Serializer that only returns u64s
/// it allows the "magic" struct serializers to obtain the transmuted field values
pub struct FieldSerializer {}
impl Serializer for FieldSerializer {
type Ok = TransmutedField;
type Error = Error;
type SerializeSeq = Impossible<TransmutedField, Error>;
type SerializeTuple = Impossible<TransmutedField, Error>;
type SerializeTupleStruct = Impossible<TransmutedField, Error>;
type SerializeTupleVariant = Impossible<TransmutedField, Error>;
type SerializeMap = Impossible<TransmutedField, Error>;
type SerializeStruct = Impossible<TransmutedField, Error>;
type SerializeStructVariant = Impossible<TransmutedField, Error>;
fn serialize_u64(self, transmuted_field: u64) -> FieldResult {
Ok(transmuted_field)
}
not_reachable! {
serialize_i8(i8);
serialize_i16(i16);
serialize_i32(i32);
serialize_i64(i64);
serialize_u8(u8);
serialize_u16(u16);
serialize_u32(u32);
// serialize_u64(TransmutedField); the chosen one
serialize_f32(f32);
serialize_f64(f64);
serialize_bool(bool);
serialize_char(char);
serialize_str(&str);
serialize_bytes(&[u8]);
}
fn serialize_none(self) -> FieldResult {
unreachable!();
}
fn serialize_some<T: ?Sized + Serialize>(self, _value: &T) -> FieldResult {
unreachable!();
}
fn serialize_unit(self) -> FieldResult {
unreachable!();
}
fn serialize_unit_struct(self, _name: &'static str) -> FieldResult {
unreachable!();
}
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
) -> FieldResult {
unreachable!();
}
fn serialize_newtype_struct<T: ?Sized + Serialize>(
self,
_name: &'static str,
_value: &T,
) -> FieldResult {
unreachable!();
}
fn serialize_newtype_variant<T: ?Sized + Serialize>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T,
) -> FieldResult {
unreachable!();
}
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
unreachable!();
}
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
unreachable!();
}
fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleStruct> {
unreachable!();
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant> {
unreachable!();
}
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
unreachable!();
}
fn serialize_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeStruct> {
unreachable!();
}
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant> {
unreachable!();
}
}
|