#![no_std]
#![feature(coerce_unsized)]
#![feature(unsize)]
use core::fmt;
use core::option::Option;
use core::marker::{PhantomData, Send, Sized, Sync};
use core::mem;
use core::ptr::NonNull;
#[allow(missing_debug_implementations)]
pub struct Unique<T: ?Sized> {
ptr: NonNull<T>,
_marker: PhantomData<T>,
}
impl<T: ?Sized> Clone for Unique<T> {
#[inline(always)]
fn clone(&self) -> Self { *self }
}
impl<T: ?Sized> Copy for Unique<T> {}
unsafe impl<T: Send + ?Sized> Send for Unique<T> { }
unsafe impl<T: Sync + ?Sized> Sync for Unique<T> { }
impl<T: Sized> Unique<T> {
pub const fn empty() -> Self {
unsafe { Self::new_unchecked(mem::align_of::<T>() as *mut T) }
}
}
impl<T: ?Sized> Unique<T> {
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
Unique { ptr: NonNull::new_unchecked(ptr), _marker: PhantomData }
}
pub fn new(ptr: *mut T) -> Option<Self> { NonNull::new(ptr).map(Self::from) }
pub const fn as_ptr(self) -> NonNull<T> { self.ptr }
pub unsafe fn as_ref(&self) -> &T { self.ptr.as_ref() }
pub unsafe fn as_mut(&mut self) -> &mut T { self.ptr.as_mut() }
}
impl<T: ?Sized> fmt::Pointer for Unique<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Pointer::fmt(&self.ptr, f)
}
}
impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> {
fn from(reference: &'a mut T) -> Self {
Unique { ptr: reference.into(), _marker: PhantomData }
}
}
impl<'a, T: ?Sized> From<&'a T> for Unique<T> {
fn from(reference: &'a T) -> Self {
Unique { ptr: reference.into(), _marker: PhantomData }
}
}
impl<T: ?Sized> From<NonNull<T>> for Unique<T> {
fn from(ptr: NonNull<T>) -> Self { Self { ptr, _marker: PhantomData } }
}
#[allow(missing_debug_implementations)]
pub struct Shared<T: ?Sized> {
ptr: NonNull<T>,
}
impl<T: Sized> Shared<T> {
pub const fn empty() -> Self {
unsafe { Self::new_unchecked(mem::align_of::<T>() as *mut T) }
}
}
impl<T: ?Sized> Shared<T> {
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self { Self { ptr: NonNull::new_unchecked(ptr) } }
pub fn new(ptr: *mut T) -> Option<Self> { NonNull::new(ptr).map(Self::from) }
pub const fn as_ptr(self) -> NonNull<T> { self.ptr }
pub unsafe fn as_ref(&self) -> &T { self.ptr.as_ref() }
pub unsafe fn as_mut(&mut self) -> &mut T { self.ptr.as_mut() }
}
impl<T: ?Sized> Clone for Shared<T> {
#[inline(always)]
fn clone(&self) -> Self { *self }
}
impl<T: ?Sized> Copy for Shared<T> {}
impl<T: ?Sized> fmt::Pointer for Shared<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Pointer::fmt(&self.as_ptr(), f)
}
}
impl<T: ?Sized> From<Unique<T>> for Shared<T> {
fn from(unique: Unique<T>) -> Self { Shared { ptr: unique.ptr } }
}
impl<'a, T: ?Sized> From<&'a mut T> for Shared<T> {
fn from(reference: &'a mut T) -> Self {
Shared { ptr: reference.into() }
}
}
impl<'a, T: ?Sized> From<&'a T> for Shared<T> {
fn from(reference: &'a T) -> Self {
Shared { ptr: reference.into() }
}
}
impl<T: ?Sized> From<NonNull<T>> for Shared<T> {
fn from(ptr: NonNull<T>) -> Self { Self { ptr } }
}
impl<S: ?Sized + core::marker::Unsize<T>, T: ?Sized> core::ops::CoerceUnsized<Unique<T>> for Unique<S> {}
impl<S: ?Sized + core::marker::Unsize<T>, T: ?Sized> core::ops::CoerceUnsized<Shared<T>> for Shared<S> {}