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
use abi_stable::{
std_types::{RBox, ROption, RString, RVec},
StableAbi,
};
pub use rplugin_macros::ast_for_plugin;
pub trait StableAst: StableAbi {
type Unstable: Sized;
fn from_unstable(n: Self::Unstable) -> Self;
fn into_unstable(self) -> Self::Unstable;
}
impl<T> StableAst for RBox<T>
where
T: StableAst,
{
type Unstable = Box<T::Unstable>;
fn from_unstable(n: Self::Unstable) -> Self {
RBox::new(T::from_unstable(*n))
}
fn into_unstable(self) -> Self::Unstable {
Box::new(T::into_unstable(RBox::into_inner(self)))
}
}
impl<T> StableAst for ROption<T>
where
T: StableAst,
{
type Unstable = Option<T::Unstable>;
fn from_unstable(n: Self::Unstable) -> Self {
match n {
Some(v) => ROption::RSome(T::from_unstable(v)),
None => ROption::RNone,
}
}
fn into_unstable(self) -> Self::Unstable {
match self {
ROption::RSome(v) => Some(v.into_unstable()),
ROption::RNone => None,
}
}
}
impl<T> StableAst for RVec<T>
where
T: StableAst,
{
type Unstable = Vec<T::Unstable>;
fn from_unstable(n: Self::Unstable) -> Self {
n.into_iter().map(T::from_unstable).collect()
}
fn into_unstable(self) -> Self::Unstable {
self.into_iter().map(T::into_unstable).collect()
}
}
macro_rules! as_is {
($T:ty) => {
impl StableAst for $T {
type Unstable = $T;
#[inline(always)]
fn from_unstable(n: Self::Unstable) -> Self {
n
}
#[inline(always)]
fn into_unstable(self) -> Self::Unstable {
self
}
}
};
(
$T:ty,
$($tt:tt),*
) => {
as_is!($T);
as_is!($($tt),*);
};
}
as_is!(bool);
as_is!(usize, u8, u16, u32, u64);
as_is!(isize, i8, i16, i32, i64);
as_is!(f32, f64);
macro_rules! convert {
(
$U:ty as $T:ty
) => {
impl StableAst for $T {
type Unstable = $U;
#[inline(always)]
fn from_unstable(n: Self::Unstable) -> Self {
n.into()
}
#[inline(always)]
fn into_unstable(self) -> Self::Unstable {
self.into()
}
}
};
}
convert!(String as RString);
as_is!(swc_common::Span);