use core::borrow::{Borrow, BorrowMut};
use core::hash::{Hash, Hasher};
use core::ops::{Deref, DerefMut};
use core::pin::Pin;
use core::{fmt, ptr};
pub struct Alloc<'a, T: ?Sized> {
inner: &'a mut T,
}
impl<'a, T: ?Sized> Alloc<'a, T> {
#[inline]
pub(crate) unsafe fn from_mut(inner: &'a mut T) -> Self {
Self { inner }
}
#[must_use]
#[inline]
pub fn leak(this: Self) -> &'a mut T {
let this = core::mem::ManuallyDrop::new(this);
unsafe { core::ptr::read(&raw const this.inner) }
}
#[must_use]
#[inline]
pub fn into_pin(this: Self) -> Pin<Self> {
unsafe { Pin::new_unchecked(this) }
}
}
impl<T: ?Sized> Deref for Alloc<'_, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.inner
}
}
impl<T: ?Sized> DerefMut for Alloc<'_, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
self.inner
}
}
impl<T: ?Sized> Drop for Alloc<'_, T> {
#[inline]
fn drop(&mut self) {
unsafe { ptr::drop_in_place(self.inner) };
}
}
impl<T: ?Sized> AsRef<T> for Alloc<'_, T> {
#[inline]
fn as_ref(&self) -> &T {
self.inner
}
}
impl<T: ?Sized> AsMut<T> for Alloc<'_, T> {
#[inline]
fn as_mut(&mut self) -> &mut T {
self.inner
}
}
impl<T: ?Sized> Borrow<T> for Alloc<'_, T> {
#[inline]
fn borrow(&self) -> &T {
self.inner
}
}
impl<T: ?Sized> BorrowMut<T> for Alloc<'_, T> {
#[inline]
fn borrow_mut(&mut self) -> &mut T {
self.inner
}
}
impl<T: ?Sized + fmt::Debug> fmt::Debug for Alloc<'_, T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<T: ?Sized + fmt::Display> fmt::Display for Alloc<'_, T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
impl<T: ?Sized> fmt::Pointer for Alloc<'_, T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ptr: *const T = self.inner;
fmt::Pointer::fmt(&ptr, f)
}
}
impl<T: ?Sized + PartialEq> PartialEq for Alloc<'_, T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
PartialEq::eq(&**self, &**other)
}
}
impl<T: ?Sized + Eq> Eq for Alloc<'_, T> {}
impl<T: ?Sized + PartialOrd> PartialOrd for Alloc<'_, T> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
PartialOrd::partial_cmp(&**self, &**other)
}
}
impl<T: ?Sized + Ord> Ord for Alloc<'_, T> {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
Ord::cmp(&**self, &**other)
}
}
impl<T: ?Sized + Hash> Hash for Alloc<'_, T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state);
}
}
impl<'a, T: ?Sized> From<Alloc<'a, T>> for Pin<Alloc<'a, T>> {
#[inline]
fn from(a: Alloc<'a, T>) -> Self {
Alloc::into_pin(a)
}
}