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#[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 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 #[inline]
48 pub fn strings(&self) -> &'static str {
49 self.mono.strings()
50 }
51 #[inline]
53 pub fn lifetime_indices(&self) -> &'static [LifetimeIndexPair] {
54 self.mono.lifetime_indices()
55 }
56 #[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 #[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#[repr(C)]
81#[derive(StableAbi, Copy, Clone)]
82pub struct MonoSharedVars {
83 strings: *const u8,
85 lifetime_indices: *const LifetimeIndexPair,
97 strings_len: u16,
98 lifetime_indices_len: u16,
99}
100
101impl MonoSharedVars {
102 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 #[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 #[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}