#![no_std]
#![allow(internal_features)] #![feature(allocator_api)]
#![feature(ptr_metadata)]
#![feature(slice_ptr_get)]
#![feature(transmute_prefix)]
#![feature(const_destruct)]
#![feature(const_trait_impl)]
#![feature(const_convert)]
#![feature(arbitrary_self_types)]
#![feature(unsize)]
#![feature(coerce_unsized)]
#![feature(maybe_dangling)]
#![feature(deref_pure_trait)]
#![feature(negative_impls)]
#![feature(const_drop_in_place)]
#![feature(const_heap)]
#![feature(const_iter)]
#![feature(const_manually_drop_take)]
#![feature(decl_macro)]
#![feature(const_slice_make_iter)]
#![feature(sized_type_properties)]
#![feature(impl_restriction)]
#![feature(min_specialization)]
#![feature(fundamental)]
#![feature(const_clone)]
#![feature(trusted_random_access)]
#![feature(allow_internal_unstable)] #![feature(const_try)]
#![feature(rustc_attrs)]
pub mod iter;
pub mod traits;
pub mod macros;
pub mod unstable;
extern crate alloc;
use crate::traits::{DerefMove, DerefOwn};
#[cfg(feature = "alloc")]
use alloc::{boxed::Box, string::String, vec::Vec};
#[cfg(feature = "alloc")]
use core::{alloc::Allocator, mem::transmute_prefix as transmute};
use core::marker::{Destruct, Unsize};
use core::mem::{ManuallyDrop, MaybeDangling, forget};
use core::ops::{AddAssign, CoerceUnsized, Deref, DerefMut, DerefPure};
use core::panic::UnwindSafe;
use core::ptr::NonNull;
#[repr(transparent)]
#[fundamental]
pub struct Own<'a, T: ?Sized + 'a> {
inner: &'a mut ManuallyDrop<T>
}
const impl<'a, T: ?Sized+ 'a> Deref for Own<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&**self.inner
}
}
impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Own<'a, U>> for Own<'b, T> {}
const impl<'a, T: ?Sized> Own<'a, T> {
#[inline(always)]
pub unsafe fn new(inner: &'a mut ManuallyDrop<T>) -> Self {
Self {
inner
}
}
}
const impl<'a, T: ?Sized> Drop for Own<'a, T> where T: [const] Destruct {
fn drop(&mut self) {
unsafe { ManuallyDrop::drop(self.inner) }
}
}
impl<'a, T: ?Sized> !UnwindSafe for Own<'a, T> {}
const impl<T> DerefMove for Own<'_, T> {
fn deref_move(self) -> Self::Target {
let mut slot = ManuallyDrop::new(self);
unsafe { ManuallyDrop::into_inner(NonNull::from_mut(slot.inner).read()) }
}
}
const unsafe impl<T: ?Sized> DerefOwn for Own<'_, T> {
unsafe fn deref_own(self: &mut ManuallyDrop<Self>) -> Own<'_, Self::Target> {
unsafe { (&raw const **self).read() }
}
fn forget_contents(this: MaybeDangling<Self>) {
forget(this)
}
}
const impl<'a, T: ?Sized> DerefMut for Own<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut **self.inner
}
}
unsafe impl<T: ?Sized> DerefPure for Own<'_, T> {}
impl<T: ?Sized> !Copy for Own<'_, T> {}
#[cfg(feature = "alloc")]
const unsafe impl<T: ?Sized, A: [const] Allocator> DerefOwn for Box<T, A> where Box<ManuallyDrop<T>, A>: [const] Destruct {
unsafe fn deref_own(self: &mut ManuallyDrop<Self>) -> Own<'_, Self::Target> {
unsafe { Own::new(transmute(&mut ***self)) }
}
fn forget_contents(this: MaybeDangling<Self>) {
drop(unsafe { transmute::<_, Box<ManuallyDrop<T>, A>>(this) })
}
}
#[cfg(feature = "alloc")]
const impl<T> DerefMove for Box<T> where Box<T>: [const] Destruct {
fn deref_move(self) -> Self::Target {
*self
}
}
#[cfg(feature = "alloc")]
const unsafe impl<T, A: [const] Allocator> DerefOwn for Vec<T, A> where Vec<ManuallyDrop<T>, A>: [const] Destruct {
unsafe fn deref_own(self: &mut ManuallyDrop<Self>) -> Own<'_, Self::Target> {
unsafe { Own::new(transmute(&mut ***self)) }
}
fn forget_contents(this: MaybeDangling<Self>) {
drop(unsafe { transmute::<_, Vec<ManuallyDrop<T>, A>>(this) });
}
}
#[cfg(feature = "alloc")]
unsafe impl DerefOwn for String {
unsafe fn deref_own(self: &mut ManuallyDrop<Self>) -> Own<'_, Self::Target> {
unsafe { Own::new(transmute(&mut ***self)) }
}
fn forget_contents(this: MaybeDangling<Self>) {
drop(unsafe { transmute::<_, Vec<ManuallyDrop<u8>>>(this.into_inner().into_bytes()) });
}
}
pub struct DerefOwnGuard<T: DerefOwn>(ManuallyDrop<T>);
const impl<T: [const] DerefOwn> DerefOwnGuard<T> {
pub fn new(inner: T) -> Self {
Self(ManuallyDrop::new(inner))
}
pub fn make_own_ref(&mut self) -> Own<'_, <T as Deref>::Target> {
unsafe { self.0.deref_own() }
}
}
const impl<T: [const] DerefOwn> Drop for DerefOwnGuard<T> {
fn drop(&mut self) {
unsafe { DerefOwn::forget_contents_in_place(&mut self.0) }
}
}