use core::fmt;
#[repr(transparent)]
pub struct Frozen<T: ?Sized>(T);
impl<T: Copy> Frozen<T> {
#[inline]
pub const fn get(&self) -> T {
self.0
}
#[macropol::macropol]
pub const fn leak_slice<'out>(x: &[T]) -> &'out [Frozen<T>]
where
T: 'out,
{
let size = core::mem::size_of::<T>()
.checked_mul(x.len())
.expect("size overflow");
let align = core::mem::align_of::<T>();
if size == 0 {
return &[];
}
unsafe {
let ptr = core::intrinsics::const_allocate(size, align).cast::<T>();
assert!(
!ptr.guaranteed_eq(core::ptr::null_mut()).unwrap_or(false),
"heap allocation failed"
);
core::ptr::copy_nonoverlapping(x.as_ptr(), ptr, x.len());
let ptr = ptr.cast::<Frozen<T>>();
core::slice::from_raw_parts(ptr, x.len())
}
}
}
impl<T: Copy> Copy for Frozen<T> {}
impl<T: Copy> const Clone for Frozen<T> {
#[inline]
fn clone(&self) -> Self {
*self
}
}
impl<T: Copy + fmt::Debug> fmt::Debug for Frozen<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}