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
//! Helper types for type_layout types.

use std::cmp::{Eq, PartialEq};

////////////////////////////////////////////////////////////////////////////////

/// A pair of length and an array,
/// which is treated as a slice of `0..self.len` in all its impls.
#[repr(C)]
#[derive(Debug, Copy, Clone, StableAbi)]
pub struct ArrayLen<A> {
    /// the length of initialized elements in `array`
    pub len: u16,
    ///
    pub array: A,
}

impl<A> ArrayLen<A> {
    /// The `len` field  casted to usize.
    pub const fn len(&self) -> usize {
        self.len as usize
    }
    /// Whether the array is empty
    pub const fn is_empty(&self) -> bool {
        self.len == 0
    }
}

impl<A, T> PartialEq for ArrayLen<A>
where
    A: ArrayTrait<Elem = T>,
    T: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        let t_slice = &self.array.as_slice()[..self.len as usize];
        let o_slice = &other.array.as_slice()[..other.len as usize];
        t_slice == o_slice
    }
}

impl<A, T> Eq for ArrayLen<A>
where
    A: ArrayTrait<Elem = T>,
    T: Eq,
{
}

////////////////////////////////////////////////////////////////////////////////

mod array_trait {
    pub trait ArrayTrait {
        type Elem;

        fn as_slice(&self) -> &[Self::Elem];
    }
}
use self::array_trait::ArrayTrait;

macro_rules! impl_stable_abi_array {
    ($($size:expr),*)=>{
        $(
            impl<T> ArrayTrait for [T;$size] {
                type Elem=T;

                fn as_slice(&self)->&[T]{
                    self
                }
            }
        )*
    }
}

impl_stable_abi_array! {
    00,01,02,03,04,05,06,07,08
}