abi_stable/type_layout/
data_structures.rs

1//! Helper types for type_layout types.
2
3use std::cmp::{Eq, PartialEq};
4
5////////////////////////////////////////////////////////////////////////////////
6
7/// A pair of length and an array,
8/// which is treated as a slice of `0..self.len` in all its impls.
9#[repr(C)]
10#[derive(Debug, Copy, Clone, StableAbi)]
11pub struct ArrayLen<A> {
12    /// the length of initialized elements in `array`
13    pub len: u16,
14    ///
15    pub array: A,
16}
17
18impl<A> ArrayLen<A> {
19    /// The `len` field  casted to usize.
20    pub const fn len(&self) -> usize {
21        self.len as usize
22    }
23    /// Whether the array is empty
24    pub const fn is_empty(&self) -> bool {
25        self.len == 0
26    }
27}
28
29impl<A, T> PartialEq for ArrayLen<A>
30where
31    A: ArrayTrait<Elem = T>,
32    T: PartialEq,
33{
34    fn eq(&self, other: &Self) -> bool {
35        let t_slice = &self.array.as_slice()[..self.len as usize];
36        let o_slice = &other.array.as_slice()[..other.len as usize];
37        t_slice == o_slice
38    }
39}
40
41impl<A, T> Eq for ArrayLen<A>
42where
43    A: ArrayTrait<Elem = T>,
44    T: Eq,
45{
46}
47
48////////////////////////////////////////////////////////////////////////////////
49
50mod array_trait {
51    pub trait ArrayTrait {
52        type Elem;
53
54        fn as_slice(&self) -> &[Self::Elem];
55    }
56}
57use self::array_trait::ArrayTrait;
58
59macro_rules! impl_stable_abi_array {
60    ($($size:expr),*)=>{
61        $(
62            impl<T> ArrayTrait for [T;$size] {
63                type Elem=T;
64
65                fn as_slice(&self)->&[T]{
66                    self
67                }
68            }
69        )*
70    }
71}
72
73impl_stable_abi_array! {
74    00,01,02,03,04,05,06,07,08
75}