abi_stable/type_layout/
tl_lifetimes.rs

1use super::{data_structures::ArrayLen, *};
2
3use std::ops::{Deref, Index};
4
5abi_stable_shared::declare_tl_lifetime_types! {
6    repr=u8,
7    attrs=[
8        derive(StableAbi),
9    ]
10}
11
12////////////////////////////////////////////////////////////////////////////////
13////////////////////////////////////////////////////////////////////////////////
14
15impl Display for LifetimeIndex {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match *self {
18            LifetimeIndex::STATIC => f.write_str("'static"),
19            LifetimeIndex::ANONYMOUS => f.write_str("'_"),
20            LifetimeIndex::NONE => f.write_str("'NONE"),
21            LifetimeIndex { bits: n } => write!(f, "'{}", n - Self::START_OF_LIFETIMES),
22        }
23    }
24}
25
26////////////////////////////////////////////////////////////////////////////////
27
28impl LifetimeRange {
29    /// Expands this `LifetimeRange` into a `LifetimeArrayOrSlice`
30    pub fn slicing(self, lifetime_indices: &[LifetimeIndexPair]) -> LifetimeArrayOrSlice<'_> {
31        let len = (self.len() + 1) / 2;
32        if self.is_range() {
33            let start = (self.bits & Self::START_MASK) as usize;
34            let end = start + len;
35            let x = RSlice::from_slice(&lifetime_indices[start..end]);
36            LifetimeArrayOrSlice::Slice(x)
37        } else {
38            LifetimeArrayOrSlice::Array(ArrayLen {
39                len: len as u16,
40                array: LifetimeIndexArray::from_u20(self.bits).to_array(),
41            })
42        }
43    }
44}
45
46////////////////////////////////////////////////////////////////////////////////
47
48/// Either an array of 3 `LifetimeIndexPair`,or a slice of `LifetimeIndexPair`.
49#[repr(u8)]
50#[derive(Debug, Copy, Clone, PartialEq, Eq, StableAbi)]
51pub enum LifetimeArrayOrSlice<'a> {
52    ///
53    Slice(RSlice<'a, LifetimeIndexPair>),
54    ///
55    Array(ArrayLen<[LifetimeIndexPair; 3]>),
56}
57
58impl<'a> LifetimeArrayOrSlice<'a> {
59    /// An empty `LifetimeArrayOrSlice`.
60    pub const EMPTY: Self = LifetimeArrayOrSlice::Array(ArrayLen {
61        len: 0,
62        array: [
63            LifetimeIndexPair::NONE,
64            LifetimeIndexPair::NONE,
65            LifetimeIndexPair::NONE,
66        ],
67    });
68
69    /// Gets a slice of the `LifetimeIndexPair` this contains.
70    pub fn as_slice(&self) -> &[LifetimeIndexPair] {
71        match self {
72            LifetimeArrayOrSlice::Slice(slice) => slice.as_slice(),
73            LifetimeArrayOrSlice::Array(arraylen) => &arraylen.array[..arraylen.len as usize],
74        }
75    }
76}
77
78impl<'a> Deref for LifetimeArrayOrSlice<'a> {
79    type Target = [LifetimeIndexPair];
80
81    #[inline]
82    fn deref(&self) -> &[LifetimeIndexPair] {
83        self.as_slice()
84    }
85}
86
87impl<'a, I, Output: ?Sized> Index<I> for LifetimeArrayOrSlice<'a>
88where
89    [LifetimeIndexPair]: Index<I, Output = Output>,
90{
91    type Output = Output;
92
93    #[inline]
94    fn index(&self, i: I) -> &Output {
95        &self.as_slice()[i]
96    }
97}
98
99////////////////////////////////////////////////////////////////////////////////