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
use crate::{
    abi_stability::stable_abi_trait::TypeLayoutCtor, std_types::RSlice,
    type_layout::data_structures::ArrayLen,
};

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

abi_stable_shared::declare_type_layout_index! {
    attrs=[
        derive(StableAbi),
        sabi(unsafe_sabi_opaque_fields),
    ]
}

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

abi_stable_shared::declare_multi_tl_types! {
    attrs=[
        derive(StableAbi),
        sabi(unsafe_sabi_opaque_fields),
    ]
}

impl TypeLayoutRange {
    pub(crate) fn to_array(self) -> [u16; Self::STORED_INLINE] {
        [
            ((self.bits0 >> Self::INDEX_0_OFFSET) & Self::INDEX_MASK) as u16,
            ((self.bits0 >> Self::INDEX_1_OFFSET) & Self::INDEX_MASK) as u16,
            ((self.bits1 >> Self::INDEX_2_OFFSET) & Self::INDEX_MASK) as u16,
            ((self.bits1 >> Self::INDEX_3_OFFSET) & Self::INDEX_MASK) as u16,
            ((self.bits1 >> Self::INDEX_4_OFFSET) & Self::INDEX_MASK) as u16,
        ]
    }

    /// Expands this `TypeLayoutRange` into a `MultipleTypeLayouts<'a>`.
    pub fn expand<'a>(&self, type_layouts: &'a [TypeLayoutCtor]) -> MultipleTypeLayouts<'a> {
        let indices = self.to_array();
        let len = self.len();

        let first = ArrayLen {
            array: [
                type_layouts[indices[0] as usize],
                type_layouts[indices[1] as usize],
                type_layouts[indices[2] as usize],
                type_layouts[indices[3] as usize],
                type_layouts[indices[4] as usize],
            ],
            len: len.min(Self::STORED_INLINE) as u16,
        };

        let remaining = if len <= Self::STORED_INLINE {
            RSlice::EMPTY
        } else {
            let start_rem = (indices[Self::STORED_INLINE - 1] + 1) as usize;
            let len_rem = len - Self::STORED_INLINE;
            RSlice::from_slice(&type_layouts[start_rem..start_rem + len_rem])
        };

        MultipleTypeLayouts { first, remaining }
    }
}

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

/// This stores multiple `TypeLayoutCtor`,some inline and some in a borrowed slice.
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, StableAbi)]
pub struct MultipleTypeLayouts<'a> {
    first: ArrayLen<[TypeLayoutCtor; TypeLayoutRange::STORED_INLINE]>,
    remaining: RSlice<'a, TypeLayoutCtor>,
}

impl<'a> MultipleTypeLayouts<'a> {
    /// The amount of `TypeLayoutCtor` this contains.
    pub fn len(&self) -> usize {
        self.first.len as usize + self.remaining.len()
    }

    /// Whether this is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Gets an iterator over the `TypeLayoutCtor` this contains.
    pub fn iter(&self) -> MTLIterator<'a> {
        MTLIterator {
            this: *self,
            index: 0,
        }
    }
}

/// An iterator over a list of `TypeLayoutCtor`.
#[derive(Clone, Debug)]
pub struct MTLIterator<'a> {
    this: MultipleTypeLayouts<'a>,
    index: usize,
}

impl<'a> Iterator for MTLIterator<'a> {
    type Item = TypeLayoutCtor;

    fn next(&mut self) -> Option<TypeLayoutCtor> {
        if self.index < self.this.len() {
            let ret = if self.index < TypeLayoutRange::STORED_INLINE {
                self.this.first.array[self.index]
            } else {
                self.this.remaining[self.index - TypeLayoutRange::STORED_INLINE]
            };
            self.index += 1;
            Some(ret)
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.this.len() - self.index;
        (len, Some(len))
    }
}

impl<'a> ExactSizeIterator for MTLIterator<'a> {}