abi_stable/type_layout/tl_prefix.rs
1use super::*;
2
3////////////////////////////////////////////////////////////////////////////////
4
5/// Properties of prefix types
6/// (vtables and modules) that don't change with generic parameters.
7#[repr(C)]
8#[derive(Copy, Clone, StableAbi)]
9pub struct MonoTLPrefixType {
10 /// The first field in the suffix,
11 /// the index to the field after
12 /// the one to which `#[sabi(last_prefix_field)]` was applied to
13 pub first_suffix_field: u8,
14 /// Which fields in the prefix
15 /// (the ones up to the one with the `#[sabi(last_prefix_field)]` attribute)
16 /// are conditionally accessible
17 /// (with the `#[sabi(accessible_if = expression)]` attribute).
18 pub conditional_prefix_fields: FieldConditionality,
19 /// All the fields of the prefix-type,even if they are inaccessible.
20 pub fields: CompTLFields,
21}
22
23impl MonoTLPrefixType {
24 /// Expands this into a `TLPrefixType`.
25 pub const fn expand(
26 self,
27 other: GenericTLPrefixType,
28 shared_vars: &'static SharedVars,
29 ) -> TLPrefixType {
30 TLPrefixType {
31 first_suffix_field: self.first_suffix_field,
32 conditional_prefix_fields: self.conditional_prefix_fields,
33 fields: self.fields.expand(shared_vars),
34 accessible_fields: other.accessible_fields,
35 }
36 }
37}
38
39/////////////////////////////////////////////////////
40
41/// Properties of prefix types
42/// (vtables and modules) that depends on generic parameters.
43#[repr(C)]
44#[derive(Copy, Clone, StableAbi)]
45pub struct GenericTLPrefixType {
46 /// Which fields are accessible when the prefix type is instantiated in
47 /// the same dynlib/binary.
48 pub accessible_fields: FieldAccessibility,
49}
50
51/////////////////////////////////////////////////////
52
53/// Properties of prefix types (vtables and modules),
54/// combining `MonoTLPrefixType` and `GenericTLPrefixType`.
55#[repr(C)]
56#[derive(Debug, Copy, Clone, PartialEq, Eq, StableAbi)]
57pub struct TLPrefixType {
58 /// The first field in the suffix,
59 /// the index to the field after
60 /// the one to which `#[sabi(last_prefix_field)]` was applied to
61 pub first_suffix_field: u8,
62 /// Which fields in the prefix
63 /// (the ones up to the one with the `#[sabi(last_prefix_field)]` attribute)
64 /// are conditionally accessible
65 /// (with the `#[sabi(accessible_if = expression)]` attribute).
66 pub conditional_prefix_fields: FieldConditionality,
67 /// All the fields of the prefix-type,even if they are inaccessible.
68 pub fields: TLFields,
69
70 /// Which fields are accessible when the prefix type is instantiated in
71 /// the same dynlib/binary.
72 pub accessible_fields: FieldAccessibility,
73}
74
75impl Display for TLPrefixType {
76 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 writeln!(f, "first_suffix_field:{}", self.first_suffix_field)?;
78 writeln!(
79 f,
80 "conditional_prefix_fields:\n {:b}",
81 self.conditional_prefix_fields.bits(),
82 )?;
83 writeln!(f, "fields:\n{}", self.fields.to_string().left_padder(4))?;
84 write!(f, "accessible_fields:\n ")?;
85 f.debug_list()
86 .entries(self.accessible_fields.iter().take(self.fields.len()))
87 .finish()?;
88 Ok(())
89 }
90}