use core::mem::{transmute_copy, ManuallyDrop};
use crate::{CompactBorrow, CompactBorrowMut, CompactInnerCopy, Compactable};
#[repr(transparent)]
pub struct Compact<T: Compactable> {
pub(crate) inner: T::Inner,
}
impl<T: Compactable> Compact<T> {
#[inline]
pub fn as_raw_data(&self) -> *const u8 {
unsafe { transmute_copy(self) }
}
#[inline]
pub fn extract(self) -> T {
T::extract(self)
}
#[inline]
unsafe fn temp_extract(&self) -> ManuallyDrop<T> {
ManuallyDrop::new(T::extract(transmute_copy(self)))
}
#[inline]
pub fn map_ref<U>(&self, f: impl FnOnce(&T) -> U) -> U {
unsafe { f(&self.temp_extract()) }
}
#[inline]
pub unsafe fn map_mut<U>(&mut self, f: impl FnOnce(&mut T) -> U) -> U {
f(&mut self.temp_extract())
}
}
impl<T: CompactBorrow> Compact<T> {
#[inline]
pub fn borrow(&self) -> <T as CompactBorrow>::Target<'_> {
T::borrow(self)
}
}
impl<T: CompactBorrowMut> Compact<T> {
#[inline]
pub fn borrow_mut(&mut self) -> <T as CompactBorrowMut>::Target<'_> {
T::borrow_mut(self)
}
}
impl<T: Compactable> Clone for Compact<T>
where
T::Inner: Clone,
{
#[inline]
fn clone(&self) -> Self {
let inner = self.inner.clone();
Self { inner }
}
}
impl<T: Compactable<Inner = CompactInnerCopy<T>> + Copy> Copy for Compact<T> {}
impl<T: Compactable + PartialEq> PartialEq for Compact<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.map_ref(|this| other.map_ref(|that| this.eq(that)))
}
}
impl<T: Compactable + Eq> Eq for Compact<T> {}
impl<T: Compactable + PartialOrd> PartialOrd for Compact<T> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.map_ref(|this| other.map_ref(|that| this.partial_cmp(that)))
}
}
impl<T: Compactable + Ord> Ord for Compact<T> {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.map_ref(|this| other.map_ref(|that| this.cmp(that)))
}
}
impl<T: Compactable + core::fmt::Debug> core::fmt::Debug for Compact<T> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.map_ref(|this| this.fmt(f))
}
}
impl<T: Compactable + Default> Default for Compact<T> {
fn default() -> Self {
T::default().compact()
}
}
impl<T: Compactable + core::hash::Hash> core::hash::Hash for Compact<T> {
#[inline]
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.map_ref(|this| this.hash(state))
}
}
unsafe impl<T: Compactable + Send> Send for Compact<T> {}
unsafe impl<T: Compactable + Sync> Sync for Compact<T> {}