abi_stable/prefix_type/
pt_metadata.rs

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
use std::{borrow::Cow, slice};

#[allow(unused_imports)]
use crate::{
    std_types::*,
    type_layout::{
        TLData, TLDataDiscriminant, TLField, TLFields, TLFieldsIterator, TLPrefixType, TypeLayout,
    },
};

use super::accessible_fields::{FieldAccessibility, FieldConditionality, IsAccessible};

#[allow(unused_imports)]
use core_extensions::SelfOps;

#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct __PrefixTypeMetadata {
    /// This is the amount of fields on the prefix of the struct,
    /// which is always the same for the same type,regardless of which library it comes from.
    pub prefix_field_count: u8,

    pub accessible_fields: FieldAccessibility,

    pub conditional_prefix_fields: FieldConditionality,

    pub fields: __InitialFieldsOrMut,

    /// The layout of the struct,for error messages.
    pub layout: &'static TypeLayout,
}

impl __PrefixTypeMetadata {
    #[allow(dead_code)]
    #[cfg(feature = "testing")]
    pub fn new(layout: &'static TypeLayout) -> Self {
        match layout.data() {
            TLData::PrefixType(prefix) => Self::with_prefix_layout(prefix, layout),
            _ => panic!(
                "Attempting to construct a __PrefixTypeMetadata from a \
                 TypeLayout of a non-prefix-type.\n\
                 Type:{}\nDataVariant:{:?}\nPackage:{}",
                layout.full_type(),
                layout.data_discriminant(),
                layout.package(),
            ),
        }
    }

    pub(crate) fn with_prefix_layout(prefix: TLPrefixType, layout: &'static TypeLayout) -> Self {
        Self {
            fields: __InitialFieldsOrMut::from(prefix.fields),
            accessible_fields: prefix.accessible_fields,
            conditional_prefix_fields: prefix.conditional_prefix_fields,
            prefix_field_count: prefix.first_suffix_field,
            layout,
        }
    }

    // #[cfg(test)]
    // pub(crate) fn assert_valid(&self){
    //     assert_eq!(self.layout.data.as_discriminant(),TLDataDiscriminant::PrefixType );
    // }

    /// Returns the maximum prefix.Does not check that they are compatible.
    ///
    /// # Preconditions
    ///
    /// The prefixes must already have been checked for compatibility.
    #[allow(dead_code)]
    #[cfg(feature = "testing")]
    pub fn max(self, other: Self) -> Self {
        if self.fields.len() < other.fields.len() {
            other
        } else {
            self
        }
    }
    /// Returns the minimum and maximum prefix.Does not check that they are compatible.
    ///
    /// # Preconditions
    ///
    /// The prefixes must already have been checked for compatibility.
    pub(crate) fn min_max(self, other: Self) -> (Self, Self) {
        if self.fields.len() < other.fields.len() {
            (self, other)
        } else {
            (other, self)
        }
    }

    /// Combines the fields from `other` into `self`,
    /// replacing any innaccessible field with one from `other`.
    ///
    /// # Preconditions
    ///
    /// This must be called after both were checked for compatibility,
    /// otherwise fields accessible in both `self` and `other`
    /// won't be checked for compatibility or copied.
    pub(crate) fn combine_fields_from(&mut self, other: &Self) {
        let mut o_fields = other.fields.iter();

        let min_field_count = o_fields.len().min(self.fields.len());

        for (field_i, (t_acc, o_acc)) in self
            .accessible_fields
            .iter()
            .take(min_field_count)
            .zip(other.accessible_fields.iter().take(min_field_count))
            .enumerate()
        {
            let o_field = o_fields.next().unwrap();
            if !t_acc.is_accessible() && o_acc.is_accessible() {
                let t_fields = self.fields.to_mut();

                t_fields[field_i] = o_field.into_owned();
            }
        }

        if min_field_count == self.fields.len() {
            let t_fields = self.fields.to_mut();

            for (i, o_field) in o_fields.enumerate() {
                let field_i = i + min_field_count;

                t_fields.push(o_field.into_owned());
                self.accessible_fields = self.accessible_fields.set(field_i, IsAccessible::Yes);
            }
        }
    }
}

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

#[doc(hidden)]
#[derive(Debug, Clone)]
pub enum __InitialFieldsOrMut {
    TLFields(TLFields),
    Mutable(Vec<TLField>),
}

impl From<TLFields> for __InitialFieldsOrMut {
    fn from(this: TLFields) -> Self {
        __InitialFieldsOrMut::TLFields(this)
    }
}

impl __InitialFieldsOrMut {
    pub fn to_mut(&mut self) -> &mut Vec<TLField> {
        match self {
            __InitialFieldsOrMut::Mutable(x) => x,
            this => {
                let list = this.iter().map(Cow::into_owned).collect::<Vec<TLField>>();
                *this = __InitialFieldsOrMut::Mutable(list);
                match this {
                    __InitialFieldsOrMut::Mutable(x) => x,
                    _ => unreachable!(),
                }
            }
        }
    }
    pub fn iter(&self) -> IFOMIter<'_> {
        match self {
            __InitialFieldsOrMut::TLFields(x) => IFOMIter::TLFields(x.iter()),
            __InitialFieldsOrMut::Mutable(x) => IFOMIter::Slice(x.iter()),
        }
    }
    pub fn len(&self) -> usize {
        match self {
            __InitialFieldsOrMut::TLFields(x) => x.len(),
            __InitialFieldsOrMut::Mutable(x) => x.len(),
        }
    }
}

#[repr(C)]
#[derive(Clone, Debug)]
pub enum IFOMIter<'a> {
    TLFields(TLFieldsIterator),
    Slice(slice::Iter<'a, TLField>),
}

impl<'a> Iterator for IFOMIter<'a> {
    type Item = Cow<'a, TLField>;

    fn next(&mut self) -> Option<Cow<'a, TLField>> {
        match self {
            IFOMIter::TLFields(iter) => iter.next().map(Cow::Owned),
            IFOMIter::Slice(iter) => iter.next().map(Cow::Borrowed),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match self {
            IFOMIter::TLFields(iter) => iter.size_hint(),
            IFOMIter::Slice(iter) => iter.size_hint(),
        }
    }
    fn count(self) -> usize {
        match self {
            IFOMIter::TLFields(iter) => iter.count(),
            IFOMIter::Slice(iter) => iter.count(),
        }
    }
}

impl<'a> std::iter::ExactSizeIterator for IFOMIter<'a> {}