Debian Codegraph @

third_party/rust/uniffi_meta/src/metadata.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 // Copied from uniffi_core/src/metadata.rs
6 // Due to a [Rust bug](https://github.com/rust-lang/rust/issues/113104) we don't want to pull in
7 // `uniffi_core`.
8 // This is the easy way out of that issue and is a temporary hacky solution.
9
10 /// Metadata constants, make sure to keep this in sync with copy in `uniffi_core::metadata`
11 pub mod codes {
12 // Top-level metadata item codes
13 pub const FUNC: u8 = 0;
14 pub const METHOD: u8 = 1;
15 pub const RECORD: u8 = 2;
16 pub const ENUM: u8 = 3;
17 pub const INTERFACE: u8 = 4;
18 pub const NAMESPACE: u8 = 6;
19 pub const CONSTRUCTOR: u8 = 7;
20 pub const UDL_FILE: u8 = 8;
21 pub const CALLBACK_INTERFACE: u8 = 9;
22 pub const TRAIT_METHOD: u8 = 10;
23 pub const UNIFFI_TRAIT: u8 = 11;
24 pub const TRAIT_INTERFACE: u8 = 12;
25 pub const CALLBACK_TRAIT_INTERFACE: u8 = 13;
26 pub const OBJECT_TRAIT_IMPL: u8 = 14;
27 pub const CUSTOM_TYPE: u8 = 15;
28
29 // Type codes
30 pub const TYPE_U8: u8 = 0;
31 pub const TYPE_U16: u8 = 1;
32 pub const TYPE_U32: u8 = 2;
33 pub const TYPE_U64: u8 = 3;
34 pub const TYPE_I8: u8 = 4;
35 pub const TYPE_I16: u8 = 5;
36 pub const TYPE_I32: u8 = 6;
37 pub const TYPE_I64: u8 = 7;
38 pub const TYPE_F32: u8 = 8;
39 pub const TYPE_F64: u8 = 9;
40 pub const TYPE_BOOL: u8 = 10;
41 pub const TYPE_STRING: u8 = 11;
42 pub const TYPE_OPTION: u8 = 12;
43 pub const TYPE_RECORD: u8 = 13;
44 pub const TYPE_ENUM: u8 = 14;
45 // 15 no longer used.
46 pub const TYPE_INTERFACE: u8 = 16;
47 pub const TYPE_VEC: u8 = 17;
48 pub const TYPE_HASH_MAP: u8 = 18;
49 pub const TYPE_SYSTEM_TIME: u8 = 19;
50 pub const TYPE_DURATION: u8 = 20;
51 pub const TYPE_CALLBACK_INTERFACE: u8 = 21;
52 pub const TYPE_CUSTOM: u8 = 22;
53 pub const TYPE_RESULT: u8 = 23;
54 pub const TYPE_TRAIT_INTERFACE: u8 = 24;
55 pub const TYPE_CALLBACK_TRAIT_INTERFACE: u8 = 25;
56 pub const TYPE_UNIT: u8 = 255;
57
58 // Literal codes
59 pub const LIT_STR: u8 = 0;
60 pub const LIT_INT: u8 = 1;
61 pub const LIT_FLOAT: u8 = 2;
62 pub const LIT_BOOL: u8 = 3;
63 pub const LIT_NONE: u8 = 4;
64 pub const LIT_SOME: u8 = 5;
65 pub const LIT_EMPTY_SEQ: u8 = 6;
66 }
67
68 // Create a checksum for a MetadataBuffer
69 //
70 // This is used by the bindings code to verify that the library they link to is the same one
71 // that the bindings were generated from.
72 pub const fn checksum_metadata(buf: &[u8]) -> u16 {
73 calc_checksum(buf, buf.len())
74 }
75
76 const fn calc_checksum(bytes: &[u8], size: usize) -> u16 {
77 // Taken from the fnv_hash() function from the FNV crate (https://github.com/servo/rust-fnv/blob/master/lib.rs).
78 // fnv_hash() hasn't been released in a version yet.
79 const INITIAL_STATE: u64 = 0xcbf29ce484222325;
80 const PRIME: u64 = 0x100000001b3;
81
82 let mut hash = INITIAL_STATE;
83 let mut i = 0;
84 while i < size {
85 hash ^= bytes[i] as u64;
86 hash = hash.wrapping_mul(PRIME);
87 i += 1;
88 }
89 // Convert the 64-bit hash to a 16-bit hash by XORing everything together
90 (hash ^ (hash >> 16) ^ (hash >> 32) ^ (hash >> 48)) as u16
91 }