abi_stable/type_layout/
shared_vars.rs

1use super::*;
2
3use crate::abi_stability::ConstGeneric;
4
5use std::{
6    fmt::{self, Debug},
7    slice,
8};
9
10////////////////////////////////////////////////////////////////////////////////
11
12/// A few static slices that many types in the `type_layout` module contain ranges into,
13/// requiring this type to be passed as a parameter.
14#[repr(C)]
15#[derive(StableAbi)]
16pub struct SharedVars {
17    mono: &'static MonoSharedVars,
18    type_layouts: *const extern "C" fn() -> &'static TypeLayout,
19    constants: *const ConstGeneric,
20    type_layouts_len: u16,
21    constants_len: u16,
22}
23
24unsafe impl Sync for SharedVars {}
25unsafe impl Send for SharedVars {}
26
27impl SharedVars {
28    /// Constructs a `SharedVars`.
29    pub const fn new(
30        mono: &'static MonoSharedVars,
31        type_layouts: RSlice<'static, extern "C" fn() -> &'static TypeLayout>,
32        constants: RSlice<'static, ConstGeneric>,
33    ) -> Self {
34        Self {
35            mono,
36
37            type_layouts: type_layouts.as_ptr(),
38            type_layouts_len: type_layouts.len() as u16,
39
40            constants: constants.as_ptr(),
41            constants_len: constants.len() as u16,
42        }
43    }
44
45    /// A string containing many strings that types in the `type_layout`
46    /// module store substrings inside of.
47    #[inline]
48    pub fn strings(&self) -> &'static str {
49        self.mono.strings()
50    }
51    /// Many lifetimes that types in the `type_layout` module reference.
52    #[inline]
53    pub fn lifetime_indices(&self) -> &'static [LifetimeIndexPair] {
54        self.mono.lifetime_indices()
55    }
56    /// Many type layouts that types in the `type_layout`
57    /// module reference.
58    ///
59    /// The `StableAbi` derive macro deduplicates identical looking types
60    /// when constructing SharedVars.
61    #[inline]
62    pub fn type_layouts(&self) -> &'static [extern "C" fn() -> &'static TypeLayout] {
63        unsafe { slice::from_raw_parts(self.type_layouts, self.type_layouts_len as usize) }
64    }
65    /// Many constants that types in the `type_layout` module contain ranges into.
66    #[inline]
67    pub fn constants(&self) -> &'static [ConstGeneric] {
68        unsafe { slice::from_raw_parts(self.constants, self.constants_len as usize) }
69    }
70}
71
72impl Debug for SharedVars {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        f.debug_struct("SharedVars").finish()
75    }
76}
77
78/// A few static slices that many types in the `type_layout` module contain ranges into,
79/// requiring this type to be passed as a parameter.
80#[repr(C)]
81#[derive(StableAbi, Copy, Clone)]
82pub struct MonoSharedVars {
83    /// Many strings,separated with ";".
84    strings: *const u8,
85    /// Stores the lifetime indices for lifetimes referenced in a type.
86    ///
87    /// Note that this only stores those indices if the type references more than 3 lifetimes,
88    /// otherwise it is stored in the range itself.
89    ///
90    /// Lifetimes indices are stored for these in order:
91    ///
92    /// - For fields
93    ///
94    /// - For parameters and return types in function pointers in fields.
95    ///
96    lifetime_indices: *const LifetimeIndexPair,
97    strings_len: u16,
98    lifetime_indices_len: u16,
99}
100
101impl MonoSharedVars {
102    /// Constructs a `MonoSharedVars`.
103    pub const fn new(
104        strings: RStr<'static>,
105        lifetime_indices: RSlice<'static, LifetimeIndexPairRepr>,
106    ) -> Self {
107        Self {
108            strings: strings.as_ptr(),
109            strings_len: strings.len() as u16,
110
111            lifetime_indices: lifetime_indices.as_ptr() as *const LifetimeIndexPairRepr
112                as *const LifetimeIndexPair,
113            lifetime_indices_len: lifetime_indices.len() as u16,
114        }
115    }
116
117    /// A string that types in the `type_layout` module store substrings inside of.
118    #[inline]
119    pub fn strings(&self) -> &'static str {
120        unsafe {
121            let slice = slice::from_raw_parts(self.strings, self.strings_len as usize);
122            std::str::from_utf8_unchecked(slice)
123        }
124    }
125
126    /// Many lifetimes that types in the `type_layout` module reference.
127    #[inline]
128    pub fn lifetime_indices(&self) -> &'static [LifetimeIndexPair] {
129        unsafe { slice::from_raw_parts(self.lifetime_indices, self.lifetime_indices_len as usize) }
130    }
131}