fuel_etk_ops/
lib.rs

1//! The EVM Toolkit Operations Crate.
2//!
3//! You can find more information about the command-line tools in
4//! [The ETK Book](https://quilt.github.io/etk/).
5//!
6//! This crate defines Rust types for all the instructions in the Ethereum
7//! Virtual Machine (EVM.)
8#![deny(unsafe_code)]
9#![deny(missing_docs)]
10#![deny(unreachable_pub)]
11#![deny(missing_debug_implementations)]
12
13use snafu::{Backtrace, Snafu};
14
15use std::borrow::{Borrow, BorrowMut};
16
17pub mod london {
18    //! Instructions available in the London hard fork.
19    include!(concat!(env!("OUT_DIR"), "/london.rs"));
20}
21
22/// Error that can occur when parsing an operation from a string.
23#[derive(Debug, Snafu)]
24pub struct FromStrError {
25    backtrace: Backtrace,
26    mnemonic: String,
27}
28
29/// Errors that can occur when parsing an operation from a byte slice.
30#[derive(Debug, Snafu)]
31pub enum FromSliceError<E>
32where
33    E: 'static + std::fmt::Display + std::error::Error,
34{
35    /// Converting the byte slice into an immediate failed.
36    ///
37    /// Often means the slice was the wrong length.
38    #[snafu(context(false))]
39    TryInto {
40        /// The source of this error.
41        source: E,
42
43        /// The source location where this error occurred.
44        backtrace: Backtrace,
45    },
46
47    /// The slice is too long for instructions that do not take an immediate argument.
48    NoImmediate {
49        /// The source location where this error occurred.
50        backtrace: Backtrace,
51    },
52}
53
54/// Trait for types that contain an immediate argument.
55pub trait Immediate<const N: usize> {}
56
57impl<const N: usize> Immediate<N> for [u8; N] {}
58
59impl<const N: usize> Immediate<N> for () {}
60
61/// Immediate type for operations that do not have an immediate argument.
62#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
63pub enum Void {}
64
65impl<const N: usize> Immediate<N> for Void {}
66
67/// Trait for describing the types of immediate arguments for operation enums.
68pub trait Immediates {
69    /// A reference type common to all immediate types ([`Self::P1`], [`Self::P2`], ...)
70    ///
71    /// For example, for immediates like `[u8; _]`, a possible `ImmediateRef` type
72    /// is `[u8]`.
73    type ImmediateRef: ?Sized;
74
75    /// A common type that all immediates ([`Self::P1`], [`Self::P2`], ...) can
76    /// be converted into.
77    ///
78    /// For example, for immediates like `[u8; _]`, a possible `Immediate` type
79    /// is `Vec<u8>`.
80    type Immediate: Borrow<Self::ImmediateRef> + BorrowMut<Self::ImmediateRef>;
81
82    /// The type of immediates used by `push1` instructions.
83    type P1: Immediate<1>
84        + Borrow<Self::ImmediateRef>
85        + BorrowMut<Self::ImmediateRef>
86        + Into<Self::Immediate>;
87
88    /// The type of immediates used by `push2` instructions.
89    type P2: Immediate<2>
90        + Borrow<Self::ImmediateRef>
91        + BorrowMut<Self::ImmediateRef>
92        + Into<Self::Immediate>;
93
94    /// The type of immediates used by `push3` instructions.
95    type P3: Immediate<3>
96        + Borrow<Self::ImmediateRef>
97        + BorrowMut<Self::ImmediateRef>
98        + Into<Self::Immediate>;
99
100    /// The type of immediates used by `push4` instructions.
101    type P4: Immediate<4>
102        + Borrow<Self::ImmediateRef>
103        + BorrowMut<Self::ImmediateRef>
104        + Into<Self::Immediate>;
105
106    /// The type of immediates used by `push5` instructions.
107    type P5: Immediate<5>
108        + Borrow<Self::ImmediateRef>
109        + BorrowMut<Self::ImmediateRef>
110        + Into<Self::Immediate>;
111
112    /// The type of immediates used by `push6` instructions.
113    type P6: Immediate<6>
114        + Borrow<Self::ImmediateRef>
115        + BorrowMut<Self::ImmediateRef>
116        + Into<Self::Immediate>;
117
118    /// The type of immediates used by `push7` instructions.
119    type P7: Immediate<7>
120        + Borrow<Self::ImmediateRef>
121        + BorrowMut<Self::ImmediateRef>
122        + Into<Self::Immediate>;
123
124    /// The type of immediates used by `push8` instructions.
125    type P8: Immediate<8>
126        + Borrow<Self::ImmediateRef>
127        + BorrowMut<Self::ImmediateRef>
128        + Into<Self::Immediate>;
129
130    /// The type of immediates used by `push9` instructions.
131    type P9: Immediate<9>
132        + Borrow<Self::ImmediateRef>
133        + BorrowMut<Self::ImmediateRef>
134        + Into<Self::Immediate>;
135
136    /// The type of immediates used by `push10` instructions.
137    type P10: Immediate<10>
138        + Borrow<Self::ImmediateRef>
139        + BorrowMut<Self::ImmediateRef>
140        + Into<Self::Immediate>;
141
142    /// The type of immediates used by `push11` instructions.
143    type P11: Immediate<11>
144        + Borrow<Self::ImmediateRef>
145        + BorrowMut<Self::ImmediateRef>
146        + Into<Self::Immediate>;
147
148    /// The type of immediates used by `push12` instructions.
149    type P12: Immediate<12>
150        + Borrow<Self::ImmediateRef>
151        + BorrowMut<Self::ImmediateRef>
152        + Into<Self::Immediate>;
153
154    /// The type of immediates used by `push13` instructions.
155    type P13: Immediate<13>
156        + Borrow<Self::ImmediateRef>
157        + BorrowMut<Self::ImmediateRef>
158        + Into<Self::Immediate>;
159
160    /// The type of immediates used by `push14` instructions.
161    type P14: Immediate<14>
162        + Borrow<Self::ImmediateRef>
163        + BorrowMut<Self::ImmediateRef>
164        + Into<Self::Immediate>;
165
166    /// The type of immediates used by `push15` instructions.
167    type P15: Immediate<15>
168        + Borrow<Self::ImmediateRef>
169        + BorrowMut<Self::ImmediateRef>
170        + Into<Self::Immediate>;
171
172    /// The type of immediates used by `push16` instructions.
173    type P16: Immediate<16>
174        + Borrow<Self::ImmediateRef>
175        + BorrowMut<Self::ImmediateRef>
176        + Into<Self::Immediate>;
177
178    /// The type of immediates used by `push17` instructions.
179    type P17: Immediate<17>
180        + Borrow<Self::ImmediateRef>
181        + BorrowMut<Self::ImmediateRef>
182        + Into<Self::Immediate>;
183
184    /// The type of immediates used by `push18` instructions.
185    type P18: Immediate<18>
186        + Borrow<Self::ImmediateRef>
187        + BorrowMut<Self::ImmediateRef>
188        + Into<Self::Immediate>;
189
190    /// The type of immediates used by `push19` instructions.
191    type P19: Immediate<19>
192        + Borrow<Self::ImmediateRef>
193        + BorrowMut<Self::ImmediateRef>
194        + Into<Self::Immediate>;
195
196    /// The type of immediates used by `push20` instructions.
197    type P20: Immediate<20>
198        + Borrow<Self::ImmediateRef>
199        + BorrowMut<Self::ImmediateRef>
200        + Into<Self::Immediate>;
201
202    /// The type of immediates used by `push21` instructions.
203    type P21: Immediate<21>
204        + Borrow<Self::ImmediateRef>
205        + BorrowMut<Self::ImmediateRef>
206        + Into<Self::Immediate>;
207
208    /// The type of immediates used by `push22` instructions.
209    type P22: Immediate<22>
210        + Borrow<Self::ImmediateRef>
211        + BorrowMut<Self::ImmediateRef>
212        + Into<Self::Immediate>;
213
214    /// The type of immediates used by `push23` instructions.
215    type P23: Immediate<23>
216        + Borrow<Self::ImmediateRef>
217        + BorrowMut<Self::ImmediateRef>
218        + Into<Self::Immediate>;
219
220    /// The type of immediates used by `push24` instructions.
221    type P24: Immediate<24>
222        + Borrow<Self::ImmediateRef>
223        + BorrowMut<Self::ImmediateRef>
224        + Into<Self::Immediate>;
225
226    /// The type of immediates used by `push25` instructions.
227    type P25: Immediate<25>
228        + Borrow<Self::ImmediateRef>
229        + BorrowMut<Self::ImmediateRef>
230        + Into<Self::Immediate>;
231
232    /// The type of immediates used by `push26` instructions.
233    type P26: Immediate<26>
234        + Borrow<Self::ImmediateRef>
235        + BorrowMut<Self::ImmediateRef>
236        + Into<Self::Immediate>;
237
238    /// The type of immediates used by `push27` instructions.
239    type P27: Immediate<27>
240        + Borrow<Self::ImmediateRef>
241        + BorrowMut<Self::ImmediateRef>
242        + Into<Self::Immediate>;
243
244    /// The type of immediates used by `push28` instructions.
245    type P28: Immediate<28>
246        + Borrow<Self::ImmediateRef>
247        + BorrowMut<Self::ImmediateRef>
248        + Into<Self::Immediate>;
249
250    /// The type of immediates used by `push29` instructions.
251    type P29: Immediate<29>
252        + Borrow<Self::ImmediateRef>
253        + BorrowMut<Self::ImmediateRef>
254        + Into<Self::Immediate>;
255
256    /// The type of immediates used by `push30` instructions.
257    type P30: Immediate<30>
258        + Borrow<Self::ImmediateRef>
259        + BorrowMut<Self::ImmediateRef>
260        + Into<Self::Immediate>;
261
262    /// The type of immediates used by `push31` instructions.
263    type P31: Immediate<31>
264        + Borrow<Self::ImmediateRef>
265        + BorrowMut<Self::ImmediateRef>
266        + Into<Self::Immediate>;
267
268    /// The type of immediates used by `push32` instructions.
269    type P32: Immediate<32>
270        + Borrow<Self::ImmediateRef>
271        + BorrowMut<Self::ImmediateRef>
272        + Into<Self::Immediate>;
273}
274
275impl Immediates for () {
276    type ImmediateRef = ();
277    type Immediate = ();
278
279    type P1 = ();
280    type P2 = ();
281    type P3 = ();
282    type P4 = ();
283    type P5 = ();
284    type P6 = ();
285    type P7 = ();
286    type P8 = ();
287    type P9 = ();
288    type P10 = ();
289    type P11 = ();
290    type P12 = ();
291    type P13 = ();
292    type P14 = ();
293    type P15 = ();
294    type P16 = ();
295    type P17 = ();
296    type P18 = ();
297    type P19 = ();
298    type P20 = ();
299    type P21 = ();
300    type P22 = ();
301    type P23 = ();
302    type P24 = ();
303    type P25 = ();
304    type P26 = ();
305    type P27 = ();
306    type P28 = ();
307    type P29 = ();
308    type P30 = ();
309    type P31 = ();
310    type P32 = ();
311}
312
313impl Immediates for [u8] {
314    type ImmediateRef = [u8];
315    type Immediate = Vec<u8>;
316
317    type P1 = [u8; 1];
318    type P2 = [u8; 2];
319    type P3 = [u8; 3];
320    type P4 = [u8; 4];
321    type P5 = [u8; 5];
322    type P6 = [u8; 6];
323    type P7 = [u8; 7];
324    type P8 = [u8; 8];
325    type P9 = [u8; 9];
326    type P10 = [u8; 10];
327    type P11 = [u8; 11];
328    type P12 = [u8; 12];
329    type P13 = [u8; 13];
330    type P14 = [u8; 14];
331    type P15 = [u8; 15];
332    type P16 = [u8; 16];
333    type P17 = [u8; 17];
334    type P18 = [u8; 18];
335    type P19 = [u8; 19];
336    type P20 = [u8; 20];
337    type P21 = [u8; 21];
338    type P22 = [u8; 22];
339    type P23 = [u8; 23];
340    type P24 = [u8; 24];
341    type P25 = [u8; 25];
342    type P26 = [u8; 26];
343    type P27 = [u8; 27];
344    type P28 = [u8; 28];
345    type P29 = [u8; 29];
346    type P30 = [u8; 30];
347    type P31 = [u8; 31];
348    type P32 = [u8; 32];
349}