runestick/
protocol.rs

1use crate::{Hash, InstFnNameHash, IntoTypeHash, Item};
2use std::cmp;
3use std::fmt;
4use std::hash;
5
6/// A built in instance function.
7#[derive(Debug, Clone, Copy)]
8pub struct Protocol {
9    /// The name of the builtin function.
10    pub name: &'static str,
11    /// The hash of the builtin function.
12    pub hash: Hash,
13}
14
15impl InstFnNameHash for Protocol {
16    fn inst_fn_name_hash(self) -> Hash {
17        self.hash
18    }
19
20    fn into_name(self) -> String {
21        String::from(self.name)
22    }
23}
24
25impl IntoTypeHash for Protocol {
26    fn into_type_hash(self) -> Hash {
27        self.hash
28    }
29
30    fn into_item(self) -> Item {
31        Item::with_item(&[self.name])
32    }
33}
34
35impl std::ops::Deref for Protocol {
36    type Target = Hash;
37
38    fn deref(&self) -> &Self::Target {
39        &self.hash
40    }
41}
42
43impl fmt::Display for Protocol {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        write!(f, "{}", self.name)
46    }
47}
48
49impl cmp::PartialEq for Protocol {
50    fn eq(&self, other: &Self) -> bool {
51        self.hash.eq(&other.hash)
52    }
53}
54
55impl cmp::Eq for Protocol {}
56
57impl hash::Hash for Protocol {
58    fn hash<H: hash::Hasher>(&self, state: &mut H) {
59        self.hash.hash(state)
60    }
61}
62
63impl Protocol {
64    /// Check two types for equality.
65    pub const EQ: Protocol = Protocol {
66        name: "eq",
67        hash: Hash::new(0x418f5becbf885806),
68    };
69
70    /// The function to access a field.
71    pub const GET: Protocol = Protocol {
72        name: "get",
73        hash: Hash::new(0x504007af1a8485a4),
74    };
75
76    /// The function to set a field.
77    pub const SET: Protocol = Protocol {
78        name: "set",
79        hash: Hash::new(0x7d13d47fd8efef5a),
80    };
81
82    /// The function to access an index.
83    pub const INDEX_GET: Protocol = Protocol {
84        name: "index_get",
85        hash: Hash::new(0xadb5b27e2a4d2dec),
86    };
87
88    /// The function to set an index.
89    pub const INDEX_SET: Protocol = Protocol {
90        name: "index_set",
91        hash: Hash::new(0x162943f7bd03ad36),
92    };
93
94    /// The function to implement for the addition operation.
95    pub const ADD: Protocol = Protocol {
96        name: "+",
97        hash: Hash::new(0xe4ecf51fa0bf1076),
98    };
99
100    /// The function to implement for the addition assign operation.
101    pub const ADD_ASSIGN: Protocol = Protocol {
102        name: "+=",
103        hash: Hash::new(0x42451ccb0a2071a9),
104    };
105
106    /// The function to implement for the subtraction operation.
107    pub const SUB: Protocol = Protocol {
108        name: "-",
109        hash: Hash::new(0x6fa86a5f18d0bf71),
110    };
111
112    /// The function to implement for the subtraction assign operation.
113    pub const SUB_ASSIGN: Protocol = Protocol {
114        name: "-=",
115        hash: Hash::new(0x5939bb56a1415284),
116    };
117
118    /// The function to implement for the multiply operation.
119    pub const MUL: Protocol = Protocol {
120        name: "*",
121        hash: Hash::new(0xb09e99dc94091d1c),
122    };
123
124    /// The function to implement for the multiply assign operation.
125    pub const MUL_ASSIGN: Protocol = Protocol {
126        name: "*=",
127        hash: Hash::new(0x29a54b727f980ebf),
128    };
129
130    /// The function to implement for the division operation.
131    pub const DIV: Protocol = Protocol {
132        name: "/",
133        hash: Hash::new(0xf26d6eea1afca6e8),
134    };
135
136    /// The function to implement for the division assign operation.
137    pub const DIV_ASSIGN: Protocol = Protocol {
138        name: "/=",
139        hash: Hash::new(0x4dd087a8281c04e6),
140    };
141
142    /// The function to implement for the remainder operation.
143    pub const REM: Protocol = Protocol {
144        name: "%",
145        hash: Hash::new(0x5c6293639c74e671),
146    };
147
148    /// The function to implement for the remainder assign operation.
149    pub const REM_ASSIGN: Protocol = Protocol {
150        name: "%=",
151        hash: Hash::new(0x3a8695980e77baf4),
152    };
153
154    /// The function to implement for the bitwise and operation.
155    pub const BIT_AND: Protocol = Protocol {
156        name: "&",
157        hash: Hash::new(0x0e11f20d940eebe8),
158    };
159
160    /// The function to implement for the bitwise and assign operation.
161    pub const BIT_AND_ASSIGN: Protocol = Protocol {
162        name: "&=",
163        hash: Hash::new(0x95cb1ba235dfb5ec),
164    };
165
166    /// The function to implement for the bitwise xor operation.
167    pub const BIT_XOR: Protocol = Protocol {
168        name: "^",
169        hash: Hash::new(0xa3099c54e1de4cbf),
170    };
171
172    /// The function to implement for the bitwise xor assign operation.
173    pub const BIT_XOR_ASSIGN: Protocol = Protocol {
174        name: "^=",
175        hash: Hash::new(0x01fa9706738f9867),
176    };
177
178    /// The function to implement for the bitwise or operation.
179    pub const BIT_OR: Protocol = Protocol {
180        name: "|",
181        hash: Hash::new(0x05010afceb4a03d0),
182    };
183
184    /// The function to implement for the bitwise xor assign operation.
185    pub const BIT_OR_ASSIGN: Protocol = Protocol {
186        name: "|=",
187        hash: Hash::new(0x606d79ff1750a7ec),
188    };
189
190    /// The function to implement for the bitwise shift left operation.
191    pub const SHL: Protocol = Protocol {
192        name: "<<",
193        hash: Hash::new(0x6845f7d0cc9e002d),
194    };
195
196    /// The function to implement for the bitwise shift left assign operation.
197    pub const SHL_ASSIGN: Protocol = Protocol {
198        name: "<<=",
199        hash: Hash::new(0xdc4702d0307ba27b),
200    };
201
202    /// The function to implement for the bitwise shift right operation.
203    pub const SHR: Protocol = Protocol {
204        name: ">>",
205        hash: Hash::new(0x6b485e8e6e58fbc8),
206    };
207
208    /// The function to implement for the bitwise shift right assign operation.
209    pub const SHR_ASSIGN: Protocol = Protocol {
210        name: ">>=",
211        hash: Hash::new(0x61ff7c46ff00e74a),
212    };
213
214    /// Protocol function used by template strings.
215    pub const STRING_DISPLAY: Protocol = Protocol {
216        name: "string_display",
217        hash: Hash::new(0x811b62957ea9d9f9),
218    };
219
220    /// Protocol function used by custom debug impls.
221    pub const STRING_DEBUG: Protocol = Protocol {
222        name: "string_debug",
223        hash: Hash::new(0x4064e3867aaa0717),
224    };
225
226    /// Function used to convert an argument into an iterator.
227    pub const INTO_ITER: Protocol = Protocol {
228        name: "into_iter",
229        hash: Hash::new(0x15a85c8d774b4065),
230    };
231
232    /// The function to call to continue iteration.
233    pub const NEXT: Protocol = Protocol {
234        name: "next",
235        hash: Hash::new(0xc3cde069de2ba320),
236    };
237
238    /// Function used to convert an argument into a future.
239    pub const INTO_FUTURE: Protocol = Protocol {
240        name: "into_future",
241        hash: Hash::new(0x596e6428deabfda2),
242    };
243
244    /// Function used to convert an argument into a future.
245    pub const INTO_TYPE_NAME: Protocol = Protocol {
246        name: "into_type_name",
247        hash: Hash::new(0xbffd08b816c24682),
248    };
249}