abi_stable/erased_types/
type_info.rs1use std::fmt;
4
5use crate::{
6 marker_type::NonOwningPhantom,
7 sabi_types::{Constructor, MaybeCmp},
8 std_types::{utypeid::UTypeId, RStr},
9 type_level::downcasting::GetUTID,
10 InterfaceType,
11};
12
13#[derive(Debug, Eq, PartialEq)]
15#[repr(C)]
16#[derive(StableAbi)]
17pub struct TypeInfo {
18 pub size: usize,
20 pub alignment: usize,
22 #[doc(hidden)]
23 pub _uid: Constructor<MaybeCmp<UTypeId>>,
24 pub type_name: Constructor<RStr<'static>>,
26 #[doc(hidden)]
27 pub _private_field: (),
28}
29
30impl TypeInfo {
31 pub fn is_compatible(&self, other: &Self) -> bool {
33 self._uid == other._uid
34 }
35}
36
37impl fmt::Display for TypeInfo {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 writeln!(
40 f,
41 "type:{ty}\n\
42 size:{size} alignment:{alignment}\n\
43 ",
44 ty = self.type_name,
45 size = self.size,
46 alignment = self.alignment,
47 )
48 }
49}
50
51#[doc(hidden)]
54pub struct TypeInfoFor<T, Interface, Downcasting>(NonOwningPhantom<(T, Interface, Downcasting)>);
55
56impl<T, Interface, Downcasting> TypeInfoFor<T, Interface, Downcasting>
57where
58 Interface: InterfaceType,
59 Downcasting: GetUTID<T>,
60{
61 pub const INFO: &'static TypeInfo = &TypeInfo {
63 size: std::mem::size_of::<T>(),
64 alignment: std::mem::align_of::<T>(),
65 _uid: Constructor(<Downcasting as GetUTID<T>>::UID),
66 type_name: Constructor(crate::utils::get_type_name::<T>),
67 _private_field: (),
68 };
69}