use core::any::Any;
use core::borrow;
use core::cmp::Ordering;
use core::fmt;
use core::future::Future;
use core::hash::{Hash, Hasher};
use core::marker::{Unpin};
use core::mem;
use core::ops::{
Deref, DerefMut,
};
use core::pin::Pin;
use core::ptr::{NonNull};
use core::task::{Context, Poll};
use core::marker;
use core::alloc::Layout;
use crate::alloc::LocalAlloc;
pub struct LocalBox<T: ?Sized, A: LocalAlloc> {
data: NonNull<T>,
_allocator: marker::PhantomData<A>,
}
impl<T, A: LocalAlloc> LocalBox<T, A> {
unsafe fn layout() -> Layout {
Layout::from_size_align_unchecked(mem::size_of::<T>(), mem::align_of::<T>())
}
#[inline(always)]
pub fn new(x: T) -> LocalBox<T, A> {
unsafe {
let l = Self::layout();
let mut s = Self::from_raw(A::alloc(l) as *mut T);
*s.data.as_mut() = x;
s
}
}
}
impl<T: ?Sized, A: LocalAlloc> LocalBox<T, A> {
fn layout_of(val: &T) -> Layout {
unsafe {
Layout::from_size_align_unchecked(mem::size_of_val(val), mem::align_of_val(val))
}
}
#[inline]
pub unsafe fn from_raw(raw: *mut T) -> Self {
LocalBox{
data: NonNull::new_unchecked(raw),
_allocator: marker::PhantomData,
}
}
#[inline]
pub fn into_raw(b: LocalBox<T, A>) -> *mut T {
LocalBox::into_raw_non_null(b).as_ptr()
}
#[inline]
#[doc(hidden)]
pub fn into_raw_non_null(b: LocalBox<T, A>) -> NonNull<T> {
unsafe {
NonNull::new_unchecked(LocalBox::leak(b) as *mut T)
}
}
#[inline]
pub fn leak<'a>(b: LocalBox<T, A>) -> &'a mut T
where
T: 'a, {
unsafe { &mut *LocalBox::into_raw(b) }
}
}
impl<T: ?Sized, A: LocalAlloc> Drop for LocalBox<T, A> {
fn drop(&mut self) {
unsafe {
A::dealloc(self.data.as_ptr() as *mut u8, Self::layout_of(self.data.as_ref()))
}
}
}
impl<T: Default, A: LocalAlloc> Default for LocalBox<T, A> {
fn default() -> LocalBox<T, A> {
Self::new(Default::default())
}
}
impl<T: Clone, A: LocalAlloc> Clone for LocalBox<T, A> {
fn clone(&self) -> LocalBox<T, A> {
Self::new((**self).clone())
}
#[inline]
fn clone_from(&mut self, source: &LocalBox<T, A>) {
(**self).clone_from(&(**source));
}
}
impl<T: ?Sized + PartialEq, A: LocalAlloc> PartialEq for LocalBox<T, A> {
#[inline]
fn eq(&self, other: &LocalBox<T, A>) -> bool {
PartialEq::eq(&**self, &**other)
}
#[inline]
fn ne(&self, other: &LocalBox<T, A>) -> bool {
PartialEq::ne(&**self, &**other)
}
}
impl<T: ?Sized + PartialOrd, A: LocalAlloc> PartialOrd for LocalBox<T, A> {
#[inline]
fn partial_cmp(&self, other: &LocalBox<T, A>) -> Option<Ordering> {
PartialOrd::partial_cmp(&**self, &**other)
}
#[inline]
fn lt(&self, other: &LocalBox<T, A>) -> bool {
PartialOrd::lt(&**self, &**other)
}
#[inline]
fn le(&self, other: &LocalBox<T, A>) -> bool {
PartialOrd::le(&**self, &**other)
}
#[inline]
fn ge(&self, other: &LocalBox<T, A>) -> bool {
PartialOrd::ge(&**self, &**other)
}
#[inline]
fn gt(&self, other: &LocalBox<T, A>) -> bool {
PartialOrd::gt(&**self, &**other)
}
}
impl<T: ?Sized + Ord, A: LocalAlloc> Ord for LocalBox<T, A> {
#[inline]
fn cmp(&self, other: &LocalBox<T, A>) -> Ordering {
Ord::cmp(&**self, &**other)
}
}
impl<T: ?Sized + Eq, A: LocalAlloc> Eq for LocalBox<T, A> {}
impl<T: ?Sized + Hash, A: LocalAlloc> Hash for LocalBox<T, A> {
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state);
}
}
impl<T: ?Sized + Hasher, A: LocalAlloc> Hasher for LocalBox<T, A> {
fn finish(&self) -> u64 {
(**self).finish()
}
fn write(&mut self, bytes: &[u8]) {
(**self).write(bytes)
}
fn write_u8(&mut self, i: u8) {
(**self).write_u8(i)
}
fn write_u16(&mut self, i: u16) {
(**self).write_u16(i)
}
fn write_u32(&mut self, i: u32) {
(**self).write_u32(i)
}
fn write_u64(&mut self, i: u64) {
(**self).write_u64(i)
}
fn write_u128(&mut self, i: u128) {
(**self).write_u128(i)
}
fn write_usize(&mut self, i: usize) {
(**self).write_usize(i)
}
fn write_i8(&mut self, i: i8) {
(**self).write_i8(i)
}
fn write_i16(&mut self, i: i16) {
(**self).write_i16(i)
}
fn write_i32(&mut self, i: i32) {
(**self).write_i32(i)
}
fn write_i64(&mut self, i: i64) {
(**self).write_i64(i)
}
fn write_i128(&mut self, i: i128) {
(**self).write_i128(i)
}
fn write_isize(&mut self, i: isize) {
(**self).write_isize(i)
}
}
impl<T, A:LocalAlloc> From<T> for LocalBox<T, A> {
fn from(t: T) -> Self {
LocalBox::new(t)
}
}
impl<A: LocalAlloc> LocalBox<dyn Any, A> {
#[inline]
pub fn downcast<T: Any>(self) -> Result<LocalBox<T, A>, LocalBox<dyn Any, A>> {
if self.is::<T>() {
unsafe {
let raw: *mut dyn Any = LocalBox::into_raw(self);
Ok(LocalBox::from_raw(raw as *mut T))
}
} else {
Err(self)
}
}
}
impl<A: LocalAlloc> LocalBox<dyn Any + Send, A> {
#[inline]
pub fn downcast<T: Any>(self) -> Result<LocalBox<T, A>, LocalBox<dyn Any + Send, A>> {
if self.is::<T>() {
unsafe {
let raw: *mut (dyn Any + Send) = LocalBox::into_raw(self);
Ok(LocalBox::from_raw(raw as *mut T))
}
} else {
Err(self)
}
}
}
impl<T: fmt::Display + ?Sized, A: LocalAlloc> fmt::Display for LocalBox<T, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
impl<T: fmt::Debug + ?Sized, A: LocalAlloc> fmt::Debug for LocalBox<T, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<T: ?Sized, A: LocalAlloc> fmt::Pointer for LocalBox<T, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ptr: *const T = &**self;
fmt::Pointer::fmt(&ptr, f)
}
}
impl<T: ?Sized, A: LocalAlloc> Deref for LocalBox<T, A> {
type Target = T;
#[allow(unconditional_recursion)]
fn deref(&self) -> &T {
&**self
}
}
impl<T: ?Sized, A: LocalAlloc> DerefMut for LocalBox<T, A> {
#[allow(unconditional_recursion)]
fn deref_mut(&mut self) -> &mut T {
&mut **self
}
}
impl<T: ?Sized, A: LocalAlloc> borrow::Borrow<T> for LocalBox<T, A> {
fn borrow(&self) -> &T {
&**self
}
}
impl<T: ?Sized, A: LocalAlloc> borrow::BorrowMut<T> for LocalBox<T, A> {
fn borrow_mut(&mut self) -> &mut T {
&mut **self
}
}
impl<T: ?Sized, A: LocalAlloc> AsRef<T> for LocalBox<T, A> {
fn as_ref(&self) -> &T {
&**self
}
}
impl<T: ?Sized, A: LocalAlloc> AsMut<T> for LocalBox<T, A> {
fn as_mut(&mut self) -> &mut T {
&mut **self
}
}
impl<T: ?Sized, A: LocalAlloc> Unpin for LocalBox<T, A> {}
impl<F: ?Sized + Future + Unpin, A: LocalAlloc> Future for LocalBox<F, A> {
type Output = F::Output;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
F::poll(Pin::new(&mut *self), cx)
}
}