#[doc(hidden)]
pub mod doc_enums;
#[cfg(any(feature = "testing", feature = "nonexhaustive_examples"))]
pub mod examples;
pub(crate) mod alt_c_functions;
pub(crate) mod nonexhaustive;
pub(crate) mod traits;
pub(crate) mod vtable;
pub use self::{
nonexhaustive::{
NonExhaustive, NonExhaustiveFor, NonExhaustiveSharedOps, NonExhaustiveWI, NonExhaustiveWS,
UnwrapEnumError,
},
traits::{
DeserializeEnum, EnumInfo, GetEnumInfo, NonExhaustiveMarker, SerializeEnum,
ValidDiscriminant,
},
vtable::GetVTable,
};
pub(crate) use self::traits::GetSerializeEnumProxy;
#[track_caller]
pub fn assert_correct_default_storage<E>()
where
E: GetEnumInfo,
{
assert_correct_storage::<E, E::DefaultStorage>(AssertCsArgs {
enum_ty: std::any::type_name::<E>(),
storage_ty: std::any::type_name::<E::DefaultStorage>(),
})
}
pub struct AssertCsArgs {
pub enum_ty: &'static str,
pub storage_ty: &'static str,
}
impl AssertCsArgs {
pub const UNKNOWN: Self = Self {
enum_ty: "<unknown>",
storage_ty: "<unknown>",
};
}
#[track_caller]
pub const fn assert_correct_storage<Enum, Storage>(args: AssertCsArgs) {
#[derive(Debug)]
#[allow(dead_code)]
struct TypeAndStorageLayout {
enum_: &'static str,
enum_size: usize,
enum_alignment: usize,
storage_: &'static str,
storage_size: usize,
storage_alignment: usize,
}
#[track_caller]
const fn inner(lay: TypeAndStorageLayout) {
let msg = match (
lay.enum_alignment <= lay.storage_alignment,
lay.enum_size <= lay.storage_size,
) {
(false, false) => {
"The alignment and size of the storage is smaller than the contained type"
}
(false, true) => "The alignment of the storage is lower than the contained type",
(true, false) => "The size of the storage is smaller than the contained type",
(true, true) => return,
};
const_panic::concat_panic!(
"\n",
display: msg,
":\n",
"\tenum_: ",
lay.enum_,
"\n",
"\tenum_size: ",
lay.enum_size,
"\n",
"\tenum_alignment: ",
lay.enum_alignment,
"\n",
"\tstorage_: ",
lay.storage_,
"\n",
"\tstorage_size: ",
lay.storage_size,
"\n",
"\tstorage_alignment: ",
lay.storage_alignment,
"\n",
)
}
inner(TypeAndStorageLayout {
enum_: args.enum_ty,
enum_size: std::mem::size_of::<Enum>(),
enum_alignment: std::mem::align_of::<Enum>(),
storage_: args.storage_ty,
storage_size: std::mem::size_of::<Storage>(),
storage_alignment: std::mem::align_of::<Storage>(),
})
}