abi_stable/type_layout/tl_reflection.rs
1use crate::std_types::RStr;
2
3////////////////////////////////////////////////////////////////////////////////
4
5/// Whether a field is accessible,and how it is accessed.
6#[repr(u8)]
7#[derive(Debug, Copy, Clone, PartialEq, Eq, StableAbi)]
8#[sabi(unsafe_sabi_opaque_fields)]
9pub enum FieldAccessor {
10 /// Accessible with `self.field_name`
11 Direct,
12 /// Accessible with `fn field_name(&self)->FieldType`
13 Method,
14 /// Accessible with `fn name(&self)->FieldType`
15 MethodNamed {
16 ///
17 name: RStr<'static>,
18 },
19 /// Accessible with `fn field_name(&self)->Option<FieldType>`
20 MethodOption,
21 /// This field is completely inaccessible.
22 Opaque,
23}
24
25impl FieldAccessor {
26 /// Constructs a FieldAccessor for a method named `name`.
27 pub const fn method_named(name: RStr<'static>) -> Self {
28 FieldAccessor::MethodNamed { name }
29 }
30}
31
32////////////////////////////////////////////////////////////////////////////////
33
34abi_stable_shared::declare_comp_field_accessor! {
35 attrs=[
36 derive(StableAbi),
37 sabi(unsafe_sabi_opaque_fields),
38 ]
39}
40
41impl CompFieldAccessor {
42 /// Expands this `CompFieldAccessor` into a `FieldAccessor`,
43 /// using the string slice contained in the `SharedVars` of
44 /// the `TypeLayout` this is stored inside of.
45 pub const fn expand(self, string: &'static str) -> Option<FieldAccessor> {
46 Some(match self {
47 Self::DIRECT => FieldAccessor::Direct,
48 Self::METHOD => FieldAccessor::Method,
49 Self::METHOD_NAMED => FieldAccessor::MethodNamed {
50 name: RStr::from_str(string),
51 },
52 Self::METHOD_OPTION => FieldAccessor::MethodOption,
53 Self::OPAQUE => FieldAccessor::Opaque,
54 _ => return None,
55 })
56 }
57}
58
59////////////////////////////////////////////////////////////////////////////////