use {
::core::{
mem::{ManuallyDrop as StdMD},
},
crate::{
ManuallyDrop,
},
};
pub struct MaybeDangling<T> {
value: ManuallyDrop<T>,
#[cfg(feature = "nightly-dropck_eyepatch")]
#[allow(nonstandard_style)]
_owns_T: ::core::marker::PhantomData<T>,
}
impl<T> MaybeDangling<T> {
pub const fn new(value: T) -> MaybeDangling<T> {
Self {
value: ManuallyDrop::new(value),
#[cfg(feature = "nightly-dropck_eyepatch")]
_owns_T: ::core::marker::PhantomData,
}
}
#[inline]
pub fn into_inner(slot: MaybeDangling<T>) -> T {
#![allow(unsafe_code)]
unsafe {
ManuallyDrop::take(&mut StdMD::new(slot).value)
}
}
}
crate::cfg_match! {
feature = "nightly-dropck_eyepatch" => {
#[allow(unsafe_code)]
unsafe impl<#[may_dangle] T> Drop for MaybeDangling<T> {
fn drop(&mut self) {
unsafe {
ManuallyDrop::drop(&mut self.value)
}
}
}
},
_ => {
impl<T> Drop for MaybeDangling<T> {
fn drop(&mut self) {
#![allow(unsafe_code)]
unsafe {
ManuallyDrop::drop(&mut self.value)
}
}
}
},
}
impl<T> ::core::ops::DerefMut for MaybeDangling<T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
impl<T> ::core::ops::Deref for MaybeDangling<T> {
type Target = T;
#[inline]
fn deref(self: &Self) -> &T {
let Self { value, .. } = self;
value
}
}
let Self { value, .. } = self;
value
}
}
impl<T: Default> Default for MaybeDangling<T> {
#[inline]
fn default() -> Self {
Self::new(T::default())
}
}
impl<T: Clone> Clone for MaybeDangling<T> {
fn clone(self: &Self) -> Self {
Self::new(T::clone(self))
}
fn clone_from(self: &mut Self, source: &Self) {
T::clone_from(self, source)
}
}