use core::fmt::Debug;
use core::fmt::Display;
use core::fmt::Formatter;
use core::ops::Deref;
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct TransmuteErr<T> {
data: T,
kind: TransmuteErrKind,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum TransmuteErrKind {
SizeMismatch { atype: usize, btype: usize },
#[cfg(all(feature = "assert_transmute_mode", debug_assertions))]
SizeMismatchInDebugAssert { atype: usize, btype: usize },
}
impl TransmuteErrKind {
#[inline]
pub const fn size_mismatch(atype: usize, btype: usize) -> Self {
Self::SizeMismatch { atype, btype }
}
#[cfg(all(feature = "assert_transmute_mode", debug_assertions))]
#[inline]
pub const fn size_mismatch_in_debug_assert(atype: usize, btype: usize) -> Self {
Self::SizeMismatchInDebugAssert { atype, btype }
}
#[inline]
pub const fn is_size_mismatch(&self) -> bool {
#[cfg(all(feature = "assert_transmute_mode", debug_assertions))]
return matches!(
self,
Self::SizeMismatch { .. } | Self::SizeMismatchInDebugAssert { .. }
);
#[cfg(not(all(feature = "assert_transmute_mode", debug_assertions)))]
matches!(self, Self::SizeMismatch { .. })
}
#[inline]
pub const fn as_description(&self) -> DescriptionOut {
m_as_description(*self)
}
#[track_caller]
pub const fn unwrap(self) -> ! {
let description = self.as_description();
#[cold]
#[track_caller]
const fn __cold_panic(dstr: &str) -> ! {
panic!("{}", dstr);
}
__cold_panic(description.as_str());
}
}
impl<T> Display for TransmuteErr<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
let description = self.as_description();
Display::fmt(description.as_str(), f)
}
}
impl<T> TransmuteErr<T> {
#[inline]
pub const fn new(kind: TransmuteErrKind, data: T) -> Self {
Self { data, kind }
}
#[inline]
pub const fn size_mismatch(sizea: usize, sizeb: usize, data: T) -> Self {
Self::new(TransmuteErrKind::size_mismatch(sizea, sizeb), data)
}
#[inline]
#[track_caller]
pub const fn unwrap(self) -> ! {
self.kind.unwrap()
}
#[inline]
pub fn into_data(self) -> T {
self.data
}
#[inline]
pub fn into_kind(self) -> TransmuteErrKind {
self.kind
}
#[inline]
pub fn kind(&self) -> TransmuteErrKind {
self.kind
}
#[inline]
pub const fn as_data(&self) -> &T {
&self.data
}
#[inline]
pub fn as_mut_data(&mut self) -> &mut T {
&mut self.data
}
}
impl<T> Deref for TransmuteErr<T> {
type Target = TransmuteErrKind;
#[inline]
fn deref(&self) -> &Self::Target {
&self.kind
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "stderr")))]
#[cfg(feature = "stderr")]
mod stderr {
use crate::err::TransmuteErr;
use crate::err::TransmuteErrKind;
use core::fmt::Debug;
use std::error::Error;
impl<T> Error for TransmuteErr<T>
where
T: Debug,
{
#[allow(deprecated)]
#[inline]
fn description(&self) -> &str {
match self.kind {
TransmuteErrKind::SizeMismatch { .. } => {
"TransmuteErrKind::SizeMismatch(atype != bsize)"
}
#[cfg(all(feature = "assert_transmute_mode", debug_assertions))]
TransmuteErrKind::SizeMismatchInDebugAssert { .. } => {
"TransmuteErrKind::SizeMismatch(atype != bsize)"
}
}
}
}
}
#[allow(unused_imports)]
#[cfg(feature = "stderr")]
pub use stderr::*;
#[cfg_attr(docsrs, doc(cfg(feature = "error_details")))]
#[cfg(feature = "error_details")]
mod error_details {
use crate::err::TransmuteErrKind;
use cluConstData::buf::ConstStrBuf;
use cluConstData::buf::size::ConstByteBufSize;
pub type DescriptionOut = ConstStrBuf<{ CAPACITY }>;
const CAPACITY: usize = DESCRIPTION_S0.len() + usize::MAX_DECIMAL_LEN + DESCRIPTION_S1.len() + usize::MAX_DECIMAL_LEN + DESCRIPTION_S2.len() + { #[cfg(all(feature = "assert_transmute_mode", debug_assertions))]
{DESCRIPTION_S3.len()}
#[cfg(not(all(feature = "assert_transmute_mode", debug_assertions)))]
0
};
const DESCRIPTION_S0: &str = "Invalid transmute: attempted to reinterpret type A (";
const DESCRIPTION_S1: &str = " bytes) as incompatible type B (";
const DESCRIPTION_S2: &str = " bytes). Sizes must match exactly.";
#[cfg(all(feature = "assert_transmute_mode", debug_assertions))]
const DESCRIPTION_S3: &str = "This check was added additionally due to the inclusion of `debug_assertions` and the `assert_transmute_mode` function in `cluFullTransmute`.";
pub(crate) const fn as_description(kind: TransmuteErrKind) -> DescriptionOut {
let (a_size, b_size, adebug_assert) = match kind {
TransmuteErrKind::SizeMismatch { atype, btype } => (atype, btype, false),
#[cfg(all(feature = "assert_transmute_mode", debug_assertions))]
TransmuteErrKind::SizeMismatchInDebugAssert { atype, btype } => (atype, btype, true),
};
let mut buf = ConstStrBuf::new();
buf.push_str(DESCRIPTION_S0);
buf.push_usize(a_size);
buf.push_str(DESCRIPTION_S1);
buf.push_usize(b_size);
buf.push_str(DESCRIPTION_S2);
if adebug_assert {
#[cfg(all(feature = "assert_transmute_mode", debug_assertions))]
buf.push_str(DESCRIPTION_S3);
let _e = adebug_assert;
}
buf
}
}
#[cfg_attr(docsrs, doc(cfg(not(feature = "error_details"))))]
#[cfg(not(feature = "error_details"))]
mod error_details {
use core::ops::Deref;
use crate::err::TransmuteErrKind;
pub type DescriptionOut = &'static Str;
#[derive(Debug)]
#[repr(transparent)]
pub struct Str(str);
impl Str {
#[inline]
pub const fn new(a: &str) -> &Str {
unsafe { &*(a as *const _ as *const Str) }
}
#[inline]
pub const fn as_str(&self) -> &str {
unsafe { &*(self as *const _ as *const str) }
}
}
impl Deref for Str {
type Target = str;
#[inline]
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
pub(crate) const fn as_description(kind: TransmuteErrKind) -> DescriptionOut {
match kind {
TransmuteErrKind::SizeMismatch { .. } => {
Str::new("TransmuteErrKind::SizeMismatch(asize != bsize)")
}
#[cfg(all(feature = "assert_transmute_mode", debug_assertions))]
TransmuteErrKind::SizeMismatchInDebugAssert { .. } => {
Str::new("TransmuteErrKind::SizeMismatchInDebugAssert(asize != bsize)")
}
}
}
}
pub use error_details::DescriptionOut;
pub(crate) use error_details::as_description as m_as_description;