feo3boy-opcodes 0.1.0

Defines the opcodes for the `feo3boy` gameboy.
Documentation
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use std::array;
use std::borrow::{Borrow, BorrowMut};
use std::iter;
use std::ops::{Deref, DerefMut};
use std::{mem, slice};

use crate::count_repetition;

/// Selects a child [`Element`][super::Element] within an `Element`. The selector must
/// match the parent type.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum ElementSelector {
    /// Selects between the true and false paths of a [`Branch`][super::Branch].
    Branch(bool),
    /// Indexes into the children of a [`Block`][super::Block].
    Block(usize),
}

impl ElementSelector {
    /// Get this [`ElementSelector`] as an [`ElementPath`] slice.
    #[inline]
    pub fn as_path(&self) -> &ElementPath {
        ElementPath::from_slice(slice::from_ref(self))
    }

    /// Get this [`ElementSelector`] as a mut [`ElementPath`] slice.
    #[inline]
    pub fn as_mut_path(&mut self) -> &mut ElementPath {
        ElementPath::from_mut_slice(slice::from_mut(self))
    }

    /// Gets the branch boolean or panics.
    pub fn unwrap_branch(self) -> bool {
        match self {
            ElementSelector::Branch(cond) => cond,
            ElementSelector::Block(_) => panic!("Not a Branch Selector"),
        }
    }

    /// Gets the block index or panics.
    pub fn unwrap_block(self) -> usize {
        match self {
            ElementSelector::Block(idx) => idx,
            ElementSelector::Branch(_) => panic!("Not a Block Selector"),
        }
    }

    /// Mutably gets the branch boolean or panics.
    pub fn unwrap_branch_mut(&mut self) -> &mut bool {
        match self {
            ElementSelector::Branch(cond) => cond,
            ElementSelector::Block(_) => panic!("Not a Branch Selector"),
        }
    }

    /// Mutably gets the block index or panics.
    pub fn unwrap_block_mut(&mut self) -> &mut usize {
        match self {
            ElementSelector::Block(idx) => idx,
            ElementSelector::Branch(_) => panic!("Not a Block Selector"),
        }
    }
}

impl From<bool> for ElementSelector {
    #[inline]
    fn from(cond: bool) -> Self {
        Self::Branch(cond)
    }
}

impl From<usize> for ElementSelector {
    #[inline]
    fn from(index: usize) -> Self {
        Self::Block(index)
    }
}

impl Borrow<ElementPath> for ElementSelector {
    #[inline]
    fn borrow(&self) -> &ElementPath {
        self.as_path()
    }
}

impl BorrowMut<ElementPath> for ElementSelector {
    #[inline]
    fn borrow_mut(&mut self) -> &mut ElementPath {
        self.as_mut_path()
    }
}

impl ElementIndex for ElementSelector {
    type PathSelectors = iter::Once<Self>;

    #[inline]
    fn path_selectors(self) -> Self::PathSelectors {
        iter::once(self)
    }
}

/// A path to a particular child [`Element`][super::Element].
#[derive(Debug, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct ElementPathBuf {
    /// The sequence of selectors that lead to the element.
    path: Vec<ElementSelector>,
}

impl ElementPathBuf {
    /// Create a new empty element path buf.
    #[inline]
    pub fn new() -> Self {
        Default::default()
    }

    /// Create a new selector by copying the given path.
    pub fn from_path(path: &ElementPath) -> Self {
        Self {
            path: path.as_slice().to_vec(),
        }
    }

    /// Get this [`ElementPathBuf`] as an [`ElementPath`] slice.
    #[inline]
    pub fn as_path(&self) -> &ElementPath {
        ElementPath::from_slice(&self.path)
    }

    /// Get this [`ElementPathBuf`] as a mut [`ElementPath`] slice.
    #[inline]
    pub fn as_mut_path(&mut self) -> &mut ElementPath {
        ElementPath::from_mut_slice(&mut self.path)
    }

    /// Push the given selector onto the end of this path.
    #[inline]
    pub fn push<S>(&mut self, selector: S)
    where
        S: Into<ElementSelector>,
    {
        self.path.push(selector.into());
    }

    /// Append another [`ElementIndex`] to the end of this path.
    #[inline]
    pub fn append<P>(&mut self, path: P)
    where
        P: ElementIndex,
    {
        self.path.extend(path.path_selectors())
    }

    /// Pop the last element off of this path selector, transforming it into its parent.
    /// Returns the [`ElementSelector`] that was popped, or `None` if the path was already
    /// the root.
    #[inline]
    pub fn pop(&mut self) -> Option<ElementSelector> {
        self.path.pop()
    }
}

impl Deref for ElementPathBuf {
    type Target = ElementPath;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.as_path()
    }
}

impl DerefMut for ElementPathBuf {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.as_mut_path()
    }
}

impl Borrow<ElementPath> for ElementPathBuf {
    #[inline]
    fn borrow(&self) -> &ElementPath {
        self.as_path()
    }
}

impl BorrowMut<ElementPath> for ElementPathBuf {
    #[inline]
    fn borrow_mut(&mut self) -> &mut ElementPath {
        self.as_mut_path()
    }
}

impl AsRef<ElementPath> for ElementPathBuf {
    #[inline]
    fn as_ref(&self) -> &ElementPath {
        self.as_path()
    }
}

impl AsMut<ElementPath> for ElementPathBuf {
    #[inline]
    fn as_mut(&mut self) -> &mut ElementPath {
        self.as_mut_path()
    }
}

impl AsRef<[ElementSelector]> for ElementPathBuf {
    #[inline]
    fn as_ref(&self) -> &[ElementSelector] {
        self.as_slice()
    }
}

impl AsMut<[ElementSelector]> for ElementPathBuf {
    #[inline]
    fn as_mut(&mut self) -> &mut [ElementSelector] {
        self.as_mut_slice()
    }
}

impl IntoIterator for ElementPathBuf {
    type IntoIter = std::vec::IntoIter<ElementSelector>;
    type Item = ElementSelector;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.path.into_iter()
    }
}

impl<'p> IntoIterator for &'p ElementPathBuf {
    type IntoIter = slice::Iter<'p, ElementSelector>;
    type Item = &'p ElementSelector;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'p> IntoIterator for &'p mut ElementPathBuf {
    type IntoIter = slice::IterMut<'p, ElementSelector>;
    type Item = &'p mut ElementSelector;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

impl PartialEq<ElementPath> for ElementPathBuf {
    #[inline]
    fn eq(&self, other: &ElementPath) -> bool {
        self.as_path() == other
    }
}

impl PartialEq<&ElementPath> for ElementPathBuf {
    #[inline]
    fn eq(&self, other: &&ElementPath) -> bool {
        self.as_path() == *other
    }
}

impl PartialEq<&mut ElementPath> for ElementPathBuf {
    #[inline]
    fn eq(&self, other: &&mut ElementPath) -> bool {
        self.as_path() == *other
    }
}

impl<'p> ElementIndex for ElementPathBuf {
    type PathSelectors = std::vec::IntoIter<ElementSelector>;

    #[inline]
    fn path_selectors(self) -> Self::PathSelectors {
        self.into_iter()
    }
}

impl<'p> ElementIndex for &'p ElementPathBuf {
    type PathSelectors = iter::Copied<slice::Iter<'p, ElementSelector>>;

    #[inline]
    fn path_selectors(self) -> Self::PathSelectors {
        self.iter().copied()
    }
}

impl<'p> ElementIndex for &'p mut ElementPathBuf {
    type PathSelectors = iter::Copied<slice::Iter<'p, ElementSelector>>;

    #[inline]
    fn path_selectors(self) -> Self::PathSelectors {
        self.iter().copied()
    }
}

/// A path to a particular child [`Element`][super::Element].
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct ElementPath {
    path: [ElementSelector],
}

impl ElementPath {
    /// An empty element path.
    pub const EMPTY: &ElementPath = ElementPath::from_slice(&[]);

    /// Create an [`ElementPath`] ref from a slice of [`ElementSelector`].
    #[inline]
    pub const fn from_slice<'p>(slice: &'p [ElementSelector]) -> &'p Self {
        // Safety: ElementPath is repr(transparent) to [ElementSelector], so transmuting
        // from &[ElementSelector] to &ElementPath is safe.
        unsafe { mem::transmute(slice) }
    }

    /// Create a mut [`ElementPath`] ref from a mut slice of [`ElementSelector`].
    #[inline]
    pub fn from_mut_slice<'p>(slice: &'p mut [ElementSelector]) -> &'p mut Self {
        // Safety: ElementPath is repr(transparent) to [ElementSelector], so transmuting
        // from &mut [ElementSelector] to &mut ElementPath is safe.
        unsafe { mem::transmute(slice) }
    }

    /// Gets the number of [selectors][ElementSelector] in this [`ElementPath`].
    #[inline]
    pub const fn len(&self) -> usize {
        self.path.len()
    }

    /// Creates an [`ElementPathBuf`] from this [`ElementPath`].
    #[inline]
    pub fn to_buf(&self) -> ElementPathBuf {
        ElementPathBuf::from_path(self)
    }

    /// Get this [`ElementPath`] as a slice.
    #[inline]
    pub const fn as_slice(&self) -> &[ElementSelector] {
        &self.path
    }

    /// Get this [`ElementPath`] as a mutable slice.
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [ElementSelector] {
        &mut self.path
    }

    /// Get an iterator over the selectors in this path.
    #[inline]
    pub fn iter(&self) -> slice::Iter<ElementSelector> {
        self.as_slice().iter()
    }

    /// Get an iterator over mutable selectors in this path.
    #[inline]
    pub fn iter_mut(&mut self) -> slice::IterMut<ElementSelector> {
        self.as_mut_slice().iter_mut()
    }

    /// Get the parent of this [`ElementPath`] if any. Returns `None` for the root (empty)
    /// path.
    pub const fn parent(&self) -> Option<&ElementPath> {
        match self.path.split_last() {
            Some((_, head)) => Some(ElementPath::from_slice(head)),
            None => None,
        }
    }

    /// Gets the last selector from the [`ElementPath`].
    #[inline]
    pub const fn last(&self) -> Option<ElementSelector> {
        match self.path.last() {
            Some(last) => Some(*last),
            None => None,
        }
    }

    /// Mutably gets the last selector from the [`ElementPath`].
    #[inline]
    pub fn last_mut(&mut self) -> Option<&mut ElementSelector> {
        self.path.last_mut()
    }
}

impl AsRef<[ElementSelector]> for ElementPath {
    #[inline]
    fn as_ref(&self) -> &[ElementSelector] {
        self.as_slice()
    }
}

impl AsMut<[ElementSelector]> for ElementPath {
    #[inline]
    fn as_mut(&mut self) -> &mut [ElementSelector] {
        self.as_mut_slice()
    }
}

impl ToOwned for ElementPath {
    type Owned = ElementPathBuf;

    #[inline]
    fn to_owned(&self) -> Self::Owned {
        self.to_buf()
    }
}

impl<'p> IntoIterator for &'p ElementPath {
    type IntoIter = slice::Iter<'p, ElementSelector>;
    type Item = &'p ElementSelector;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'p> IntoIterator for &'p mut ElementPath {
    type IntoIter = slice::IterMut<'p, ElementSelector>;
    type Item = &'p mut ElementSelector;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

impl<'p> ElementIndex for &'p ElementPath {
    type PathSelectors = iter::Copied<slice::Iter<'p, ElementSelector>>;

    #[inline]
    fn path_selectors(self) -> Self::PathSelectors {
        self.iter().copied()
    }
}

impl<'p> ElementIndex for &'p mut ElementPath {
    type PathSelectors = iter::Copied<slice::Iter<'p, ElementSelector>>;

    #[inline]
    fn path_selectors(self) -> Self::PathSelectors {
        self.iter().copied()
    }
}

impl PartialEq<ElementPathBuf> for ElementPath {
    #[inline]
    fn eq(&self, other: &ElementPathBuf) -> bool {
        self == other.as_path()
    }
}

impl PartialEq<ElementPathBuf> for &ElementPath {
    #[inline]
    fn eq(&self, other: &ElementPathBuf) -> bool {
        *self == other.as_path()
    }
}

impl PartialEq<ElementPathBuf> for &mut ElementPath {
    #[inline]
    fn eq(&self, other: &ElementPathBuf) -> bool {
        *self == other.as_path()
    }
}

/// Trait for types which can be used to index into an [`Element`][super::Element].
pub trait ElementIndex {
    type PathSelectors: Iterator<Item = ElementSelector>
        + DoubleEndedIterator
        + ExactSizeIterator
        + iter::FusedIterator;

    fn path_selectors(self) -> Self::PathSelectors;
}

impl ElementIndex for bool {
    type PathSelectors = iter::Once<ElementSelector>;

    #[inline]
    fn path_selectors(self) -> Self::PathSelectors {
        ElementSelector::from(self).path_selectors()
    }
}

impl ElementIndex for usize {
    type PathSelectors = iter::Once<ElementSelector>;

    #[inline]
    fn path_selectors(self) -> Self::PathSelectors {
        ElementSelector::from(self).path_selectors()
    }
}

macro_rules! tuple_element_index {
    ($($t:ident),* $(,)?) => {
        impl<$($t),*> ElementIndex for ($($t,)*)
        where $($t: Into<ElementSelector>,)*
        {
            type PathSelectors = array::IntoIter<ElementSelector, {count_repetition!($($t,)*)}>;

            fn path_selectors(self) -> Self::PathSelectors {
                #[allow(non_snake_case)]
                let ($($t,)*) = self;
                [$($t.into(),)*].into_iter()
            }
        }
    };
}

tuple_element_index!();
tuple_element_index!(A);
tuple_element_index!(A, B);
tuple_element_index!(A, B, C);
tuple_element_index!(A, B, C, D);
tuple_element_index!(A, B, C, D, E);
tuple_element_index!(A, B, C, D, E, F);
tuple_element_index!(A, B, C, D, E, F, G);
tuple_element_index!(A, B, C, D, E, F, G, H);
tuple_element_index!(A, B, C, D, E, F, G, H, I);
tuple_element_index!(A, B, C, D, E, F, G, H, I, J);