abi_stable/type_layout/
construction.rs

1use super::*;
2
3////////////////////////////////////////////////////////////////////////////////
4
5#[allow(non_camel_case_types)]
6#[doc(hidden)]
7#[derive(Copy, Clone)]
8pub struct _private_TypeLayoutDerive {
9    pub shared_vars: &'static SharedVars,
10    pub mono: &'static MonoTypeLayout,
11    pub abi_consts: AbiConsts,
12    pub data: GenericTLData,
13    pub tag: Option<&'static Tag>,
14    pub extra_checks: Option<&'static ManuallyDrop<StoredExtraChecks>>,
15}
16
17#[allow(non_camel_case_types)]
18#[doc(hidden)]
19#[derive(Copy, Clone)]
20pub struct _private_MonoTypeLayoutDerive {
21    pub name: RStr<'static>,
22    pub item_info: ItemInfo,
23    pub data: MonoTLData,
24    pub generics: CompGenericParams,
25    pub repr_attr: ReprAttr,
26    pub mod_refl_mode: ModReflMode,
27    pub phantom_fields: RSlice<'static, CompTLFieldRepr>,
28    pub shared_vars: MonoSharedVars,
29}
30
31/// Information about where a type was declared.
32#[repr(C)]
33#[derive(Debug, Copy, Clone, PartialEq, Eq, StableAbi)]
34#[sabi(unsafe_sabi_opaque_fields)]
35pub struct ItemInfo {
36    /// The package where the type was defined,and the version string.
37    /// With the `package;version_number` format.
38    package_and_version: RStr<'static>,
39    /// The line in the file where the type was defined.
40    pub line: u32,
41    /// The full path to the module where the type was defined,
42    /// including the package name
43    pub mod_path: ModPath,
44}
45
46impl ItemInfo {
47    #[doc(hidden)]
48    pub const fn new(package_and_version: &'static str, line: u32, mod_path: ModPath) -> Self {
49        Self {
50            package_and_version: RStr::from_str(package_and_version),
51            line,
52            mod_path,
53        }
54    }
55
56    /// Constructs an ItemInfo for a std primitive
57    pub const fn primitive() -> Self {
58        Self {
59            package_and_version: RStr::from_str("std;1.0.0"),
60            line: 0,
61            mod_path: ModPath::PRELUDE,
62        }
63    }
64
65    /// Constructs an ItemInfo for an std type with a path.
66    pub const fn std_type_in(mod_path: NulStr<'static>) -> Self {
67        Self {
68            package_and_version: RStr::from_str("std;1.0.0"),
69            line: 0,
70            mod_path: ModPath::inside(mod_path),
71        }
72    }
73
74    /// Constructs an ItemInfo for a type in a package and the path to its module.
75    ///
76    /// `package_and_version` must be formatted like this:`package_name;major.minor.patch`
77    ///
78    /// `mod_path` must include the crate name.
79    pub const fn package_and_mod(
80        package_and_version: &'static str,
81        mod_path: NulStr<'static>,
82    ) -> Self {
83        Self {
84            package_and_version: RStr::from_str(package_and_version),
85            line: 0,
86            mod_path: ModPath::inside(mod_path),
87        }
88    }
89
90    /// Gets the package name and an unparsed package version.
91    pub fn package_and_version(&self) -> (&'static str, &'static str) {
92        let pav = self.package_and_version.as_str();
93        match pav.find(';') {
94            Some(separator) => (&pav[..separator], &pav[(separator + 1)..]),
95            None => (pav, ""),
96        }
97    }
98
99    /// Gets the package name.
100    pub fn package(&self) -> &'static str {
101        self.package_and_version().0
102    }
103
104    /// Gets the unparsed package version.
105    pub fn version(&self) -> &'static str {
106        self.package_and_version().1
107    }
108}