pub struct MaybeDangling<P>(/* private fields */)
where
P: ?Sized;maybe_dangling)Expand description
Allows wrapped references and boxes to dangle.
That is, if a reference (or a Box) is wrapped in MaybeDangling (including when in a
(nested) field of a compound type wrapped in MaybeDangling), it does not have to follow
pointer aliasing rules or be dereferenceable.
This can be useful when the value can become dangling while the function holding it is still executing (particularly in concurrent code). As a somewhat absurd example, consider this code:
#![feature(box_as_ptr)]
let mut boxed = Box::new(0_u32);
let ptr = Box::as_mut_ptr(&mut boxed);
// Safety: the pointer comes from a box and thus was allocated before; `box` is not used afterwards
unsafe { dealloc(ptr.cast(), Layout::new::<u32>()) };
mem::forget(boxed); // <-- this is UB!Even though the Boxeโs destructor is not run (and thus we donโt have a double free bug), this
code is still UB. This is because when moving boxed into forget, its validity invariants
are asserted, causing UB since the Box is dangling. The safety comment is as such wrong, as
moving the boxed variable as part of the forget call is a use.
To fix this we could use MaybeDangling:
#![feature(maybe_dangling, box_as_ptr)]
let mut boxed = MaybeDangling::new(Box::new(0_u32));
let ptr = Box::as_mut_ptr(boxed.as_mut());
// Safety: the pointer comes from a box and thus was allocated before; `box` is not used afterwards
unsafe { dealloc(ptr.cast(), Layout::new::<u32>()) };
mem::forget(boxed); // <-- this is OK!Note that the bit pattern must still be valid for the wrapped type. That is, references (and boxes) still must be aligned and non-null.
Additionally note that safe code can still assume that the inner value in a MaybeDangling is
not dangling โ functions like as_ref and into_inner are safe. It is not sound to
return a dangling reference in a MaybeDangling to safe code. However, it is sound
to hold such values internally inside your code โ and thereโs no way to do that without
this type. Note that other types can use this type and thus get the same effect; in particular,
ManuallyDrop will use MaybeDangling.
Note that MaybeDangling doesnโt prevent drops from being run, which can lead to UB if the
drop observes a dangling value. If you need to prevent drops from being run use ManuallyDrop
instead.
Implementationsยง
Sourceยงimpl<P> MaybeDangling<P>where
P: ?Sized,
impl<P> MaybeDangling<P>where
P: ?Sized,
Sourcepub const fn new(x: P) -> MaybeDangling<P>
๐ฌThis is a nightly-only experimental API. (maybe_dangling)
pub const fn new(x: P) -> MaybeDangling<P>
maybe_dangling)Wraps a value in a MaybeDangling, allowing it to dangle.
Sourcepub const fn as_ref(&self) -> &P
๐ฌThis is a nightly-only experimental API. (maybe_dangling)
pub const fn as_ref(&self) -> &P
maybe_dangling)Returns a reference to the inner value.
Note that this is UB if the inner value is currently dangling.
Sourcepub const fn as_mut(&mut self) -> &mut P
๐ฌThis is a nightly-only experimental API. (maybe_dangling)
pub const fn as_mut(&mut self) -> &mut P
maybe_dangling)Returns a mutable reference to the inner value.
Note that this is UB if the inner value is currently dangling.
Sourcepub const fn into_inner(self) -> P
๐ฌThis is a nightly-only experimental API. (maybe_dangling)
pub const fn into_inner(self) -> P
maybe_dangling)Extracts the value from the MaybeDangling container.
Note that this is UB if the inner value is currently dangling.
Trait Implementationsยง
Sourceยงimpl<P> Clone for MaybeDangling<P>
impl<P> Clone for MaybeDangling<P>
Sourceยงfn clone(&self) -> MaybeDangling<P>
fn clone(&self) -> MaybeDangling<P>
1.0.0 ยท Sourceยงfn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSourceยงimpl<P> Debug for MaybeDangling<P>
impl<P> Debug for MaybeDangling<P>
Sourceยงimpl<P> Default for MaybeDangling<P>
impl<P> Default for MaybeDangling<P>
Sourceยงfn default() -> MaybeDangling<P>
fn default() -> MaybeDangling<P>
impl<P> Copy for MaybeDangling<P>
Auto Trait Implementationsยง
impl<P> Freeze for MaybeDangling<P>
impl<P> RefUnwindSafe for MaybeDangling<P>where
P: RefUnwindSafe + ?Sized,
impl<P> Send for MaybeDangling<P>
impl<P> Sync for MaybeDangling<P>
impl<P> Unpin for MaybeDangling<P>
impl<P> UnwindSafe for MaybeDangling<P>where
P: UnwindSafe + ?Sized,
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Sourceยงimpl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Sourceยงimpl<T> FmtForward for T
impl<T> FmtForward for T
Sourceยงfn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Sourceยงfn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Sourceยงfn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Sourceยงfn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Sourceยงfn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Sourceยงfn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Sourceยงfn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Sourceยงfn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
Sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self> โ
fn into_either(self, into_left: bool) -> Either<Self, Self> โ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSourceยงimpl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Sourceยงfn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Sourceยงfn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSourceยงfn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSourceยงfn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Sourceยงfn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Sourceยงfn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Sourceยงfn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Sourceยงfn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Sourceยงimpl<T> Tap for T
impl<T> Tap for T
Sourceยงfn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSourceยงfn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSourceยงfn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSourceยงfn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSourceยงfn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSourceยงfn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSourceยงfn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Sourceยงfn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Sourceยงfn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Sourceยงfn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Sourceยงfn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Sourceยงfn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Sourceยงfn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.