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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::{collections::BTreeMap, hash::Hasher};
pub use uniffi_checksum_derive::Checksum;
use serde::{Deserialize, Serialize};
/// Similar to std::hash::Hash.
///
/// Implementations of this trait are expected to update the hasher state in
/// the same way across platforms. #[derive(Checksum)] will do the right thing.
pub trait Checksum {
fn checksum<H: Hasher>(&self, state: &mut H);
}
impl Checksum for bool {
fn checksum<H: Hasher>(&self, state: &mut H) {
state.write_u8(*self as u8);
}
}
impl Checksum for u64 {
fn checksum<H: Hasher>(&self, state: &mut H) {
state.write(&self.to_le_bytes());
}
}
impl Checksum for i64 {
fn checksum<H: Hasher>(&self, state: &mut H) {
state.write(&self.to_le_bytes());
}
}
impl<T: Checksum> Checksum for Box<T> {
fn checksum<H: Hasher>(&self, state: &mut H) {
(**self).checksum(state)
}
}
impl<T: Checksum> Checksum for [T] {
fn checksum<H: Hasher>(&self, state: &mut H) {
state.write(&(self.len() as u64).to_le_bytes());
for item in self {
Checksum::checksum(item, state);
}
}
}
impl<T: Checksum> Checksum for Vec<T> {
fn checksum<H: Hasher>(&self, state: &mut H) {
Checksum::checksum(&**self, state);
}
}
impl<K: Checksum, V: Checksum> Checksum for BTreeMap<K, V> {
fn checksum<H: Hasher>(&self, state: &mut H) {
state.write(&(self.len() as u64).to_le_bytes());
for (key, value) in self {
Checksum::checksum(key, state);
Checksum::checksum(value, state);
}
}
}
impl<T: Checksum> Checksum for Option<T> {
fn checksum<H: Hasher>(&self, state: &mut H) {
match self {
None => state.write(&0u64.to_le_bytes()),
Some(value) => {
state.write(&1u64.to_le_bytes());
Checksum::checksum(value, state)
}
}
}
}
impl Checksum for str {
fn checksum<H: Hasher>(&self, state: &mut H) {
state.write(self.as_bytes());
state.write_u8(0xff);
}
}
impl Checksum for String {
fn checksum<H: Hasher>(&self, state: &mut H) {
(**self).checksum(state)
}
}
impl Checksum for &str {
fn checksum<H: Hasher>(&self, state: &mut H) {
(**self).checksum(state)
}
}
#[derive(Clone, Debug, Checksum, Deserialize, Serialize)]
pub struct FnMetadata {
pub module_path: Vec<String>,
pub name: String,
pub inputs: Vec<FnParamMetadata>,
pub return_type: Option<Type>,
pub throws: Option<String>,
}
impl FnMetadata {
pub fn ffi_symbol_name(&self) -> String {
fn_ffi_symbol_name(&self.module_path, &self.name, checksum(self))
}
}
#[derive(Clone, Debug, Checksum, Deserialize, Serialize)]
pub struct MethodMetadata {
pub module_path: Vec<String>,
pub self_name: String,
pub name: String,
pub inputs: Vec<FnParamMetadata>,
pub return_type: Option<Type>,
pub throws: Option<String>,
}
impl MethodMetadata {
pub fn ffi_symbol_name(&self) -> String {
let full_name = format!("impl_{}_{}", self.self_name, self.name);
fn_ffi_symbol_name(&self.module_path, &full_name, checksum(self))
}
}
#[derive(Clone, Debug, Checksum, Deserialize, Serialize)]
pub struct FnParamMetadata {
pub name: String,
#[serde(rename = "type")]
pub ty: Type,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Checksum, Deserialize, Serialize)]
pub enum Type {
U8,
U16,
U32,
U64,
I8,
I16,
I32,
I64,
F32,
F64,
Bool,
String,
Option {
inner_type: Box<Type>,
},
Vec {
inner_type: Box<Type>,
},
HashMap {
key_type: Box<Type>,
value_type: Box<Type>,
},
ArcObject {
object_name: String,
},
Unresolved {
name: String,
},
}
#[derive(Clone, Debug, Checksum, Deserialize, Serialize)]
pub struct RecordMetadata {
pub module_path: Vec<String>,
pub name: String,
pub fields: Vec<FieldMetadata>,
}
#[derive(Clone, Debug, Checksum, Deserialize, Serialize)]
pub struct FieldMetadata {
pub name: String,
#[serde(rename = "type")]
pub ty: Type,
}
#[derive(Clone, Debug, Checksum, Deserialize, Serialize)]
pub struct EnumMetadata {
pub module_path: Vec<String>,
pub name: String,
pub variants: Vec<VariantMetadata>,
}
#[derive(Clone, Debug, Checksum, Deserialize, Serialize)]
pub struct VariantMetadata {
pub name: String,
pub fields: Vec<FieldMetadata>,
}
#[derive(Clone, Debug, Checksum, Deserialize, Serialize)]
pub struct ObjectMetadata {
pub module_path: Vec<String>,
pub name: String,
}
impl ObjectMetadata {
/// FFI symbol name for the `free` function for this object.
///
/// This function is used to free the memory used by this object.
pub fn free_ffi_symbol_name(&self) -> String {
let free_name = format!("object_free_{}", self.name);
fn_ffi_symbol_name(&self.module_path, &free_name, checksum(self))
}
}
#[derive(Clone, Debug, Checksum, Deserialize, Serialize)]
pub struct ErrorMetadata {
pub module_path: Vec<String>,
pub name: String,
pub variants: Vec<VariantMetadata>,
pub flat: bool,
}
/// Returns the last 16 bits of the value's hash as computed with [`SipHasher13`].
///
/// To be used as a checksum of FFI symbols, as a safeguard against different UniFFI versions being
/// used for scaffolding and bindings generation.
pub fn checksum<T: Checksum>(val: &T) -> u16 {
let mut hasher = siphasher::sip::SipHasher13::new();
val.checksum(&mut hasher);
(hasher.finish() & 0x000000000000FFFF) as u16
}
pub fn fn_ffi_symbol_name(mod_path: &[String], name: &str, checksum: u16) -> String {
let mod_path = mod_path.join("__");
format!("_uniffi_{mod_path}_{name}_{checksum:x}")
}
/// Enum covering all the possible metadata types
#[derive(Clone, Debug, Checksum, Deserialize, Serialize)]
pub enum Metadata {
Func(FnMetadata),
Method(MethodMetadata),
Record(RecordMetadata),
Enum(EnumMetadata),
Object(ObjectMetadata),
Error(ErrorMetadata),
}
impl From<FnMetadata> for Metadata {
fn from(value: FnMetadata) -> Metadata {
Self::Func(value)
}
}
impl From<MethodMetadata> for Metadata {
fn from(m: MethodMetadata) -> Self {
Self::Method(m)
}
}
impl From<RecordMetadata> for Metadata {
fn from(r: RecordMetadata) -> Self {
Self::Record(r)
}
}
impl From<EnumMetadata> for Metadata {
fn from(e: EnumMetadata) -> Self {
Self::Enum(e)
}
}
impl From<ObjectMetadata> for Metadata {
fn from(v: ObjectMetadata) -> Self {
Self::Object(v)
}
}
impl From<ErrorMetadata> for Metadata {
fn from(v: ErrorMetadata) -> Self {
Self::Error(v)
}
}
|