deno.land / x / deno@v1.28.2 / serde_v8 / magic / transl8.rs

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
//! Transerialization extends the set of serde-compatible types (for given de/serializers).//! By "hackishly" transmuting references across serde boundaries as u64s.//! Type-safety is enforced using special struct names for each "magic type".//! Memory-safety relies on transerialized values being "pinned" during de/serialization.
pub(crate) const MAGIC_FIELD: &str = "$__v8_magic_field";
pub(crate) trait MagicType { const NAME: &'static str; const MAGIC_NAME: &'static str;}
pub(crate) trait ToV8 { fn to_v8<'a>( &mut self, scope: &mut v8::HandleScope<'a>, ) -> Result<v8::Local<'a, v8::Value>, crate::Error>;}
pub(crate) trait FromV8: Sized { fn from_v8( scope: &mut v8::HandleScope, value: v8::Local<v8::Value>, ) -> Result<Self, crate::Error>;}
pub(crate) fn magic_serialize<T, S>( serializer: S, x: &T,) -> Result<S::Ok, S::Error>where S: serde::Serializer, T: MagicType,{ use serde::ser::SerializeStruct;
let mut s = serializer.serialize_struct(T::MAGIC_NAME, 1)?; let ptr = opaque_send(x); s.serialize_field(MAGIC_FIELD, &ptr)?; s.end()}
pub(crate) fn magic_deserialize<'de, T, D>( deserializer: D,) -> Result<T, D::Error>where D: serde::Deserializer<'de>, T: MagicType,{ struct ValueVisitor<T> { p1: std::marker::PhantomData<T>, }
impl<'de, T: MagicType> serde::de::Visitor<'de> for ValueVisitor<T> { type Value = T;
fn expecting( &self, formatter: &mut std::fmt::Formatter, ) -> std::fmt::Result { formatter.write_str("a ")?; formatter.write_str(T::NAME) }
fn visit_u64<E>(self, ptr: u64) -> Result<Self::Value, E> where E: serde::de::Error, { // SAFETY: opaque ptr originates from visit_magic, which forgets ownership so we can take it Ok(unsafe { opaque_take(ptr) }) } }
deserializer.deserialize_struct( T::MAGIC_NAME, &[MAGIC_FIELD], ValueVisitor::<T> { p1: std::marker::PhantomData, }, )}
pub(crate) fn visit_magic<'de, T, V, E>(visitor: V, x: T) -> Result<V::Value, E>where V: serde::de::Visitor<'de>, E: serde::de::Error,{ let y = visitor.visit_u64::<E>(opaque_send(&x)); std::mem::forget(x); y}
/// Constructs an "opaque" ptr from a reference to transerializepub(crate) fn opaque_send<T: Sized>(x: &T) -> u64 { (x as *const T) as u64}
/// Copies an "opaque" ptr from a reference to an opaque ptr (transerialized)/// NOTE: ptr-to-ptr, extra indirectionpub(crate) unsafe fn opaque_recv<T: ?Sized>(ptr: &T) -> u64 { *(ptr as *const T as *const u64)}
/// Transmutes an "opaque" ptr back into a referencepub(crate) unsafe fn opaque_deref_mut<'a, T>(ptr: u64) -> &'a mut T { std::mem::transmute(ptr as usize)}
/// Transmutes & copies the value from the "opaque" ptr/// NOTE: takes ownership & requires other end to forget its ownershippub(crate) unsafe fn opaque_take<T>(ptr: u64) -> T { std::mem::transmute_copy::<T, T>(std::mem::transmute(ptr as usize))}
macro_rules! impl_magic { ($t:ty) => { impl crate::magic::transl8::MagicType for $t { const NAME: &'static str = stringify!($t); const MAGIC_NAME: &'static str = concat!("$__v8_magic_", stringify!($t)); }
impl serde::Serialize for $t { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer, { crate::magic::transl8::magic_serialize(serializer, self) } }
impl<'de> serde::Deserialize<'de> for $t { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de>, { crate::magic::transl8::magic_deserialize(deserializer) } } };}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;
deno

Version Info

Tagged at
a year ago