pub struct OwnRef<'slot, T: 'slot + ?Sized, DropFlags: 'static = No> { /* private fields */ }Expand description
&'slot own T.
Implementations§
source§impl<'slot, T> OwnRef<'slot, T>
impl<'slot, T> OwnRef<'slot, T>
sourcepub fn deref_move(self: OwnRef<'slot, T>) -> T
pub fn deref_move(self: OwnRef<'slot, T>) -> T
Perform a “deref-move” operation akin to * on Boxes.
Same API as OwnRef::into_inner(), but with some more panache.
source§impl<'slot, T> OwnRef<'slot, T>
impl<'slot, T> OwnRef<'slot, T>
sourcepub unsafe fn from_ref_unchecked(
r: &'slot mut ManuallyDrop<T>
) -> OwnRef<'slot, T, No>
pub unsafe fn from_ref_unchecked( r: &'slot mut ManuallyDrop<T> ) -> OwnRef<'slot, T, No>
Low-level OwnRef construction.
An OwnRef<'slot, T>, at least, one with
No DropFlags
attached, is “merely” a “glorified”
&'slot mut ManuallyDrop<T>, with an automatic
ManuallyDrop::drop() invocation engrained into its Drop glue,
and thus, raison d’être.
- From there, it grows to become way more than that, thanks to its
unsizingcapabilities, which in turn subsume whole language features such as#![feature(unsized_fn_params)], or even#![feature(unsized_rvalues)]altogether.
It thus makes sense for such a basic and quintessential construction to be available.
Do note that the Pin-related APIs and types, such as
OwnRef<'_, T, DropFlags::Yes>, are more involved
and subtle than this, with (raw) pointer provenance playing an
important role. Try to steer away from unsafely constructing that type.
§Safety
Calling this returns a handle which, ultimately, calls
ManuallyDrop::drop() (or ManuallyDrop::take() if calling
OwnRef::deref_move()), so necessarily, the safety requirements and
caveats of these ManuallyDrop APIs apply.
Good news is, they also suffice.
source§impl<'slot, T> OwnRef<'slot, T>
impl<'slot, T> OwnRef<'slot, T>
source§impl<'slot, T: ?Sized, D> OwnRef<'slot, T, D>
impl<'slot, T: ?Sized, D> OwnRef<'slot, T, D>
sourcepub unsafe fn from_raw(
ptr: *mut ManuallyDrop<T>,
_you_can_use_this_to_bound_the_lifetime: [&'slot (); 0]
) -> OwnRef<'slot, T, D>
pub unsafe fn from_raw( ptr: *mut ManuallyDrop<T>, _you_can_use_this_to_bound_the_lifetime: [&'slot (); 0] ) -> OwnRef<'slot, T, D>
Construct a Self out of a
&'slot mut ManuallyDrop<T>.
- (Consider the arg pair as acting as one).
§Safety
-
Casting the ptr to a
&'slot mut ManuallyDrop<T>must be sound. -
Since the resulting pointer has ownership over the pointee
T, i.e., sinceTis to be dropped bySelf, thenManuallyDrop::take()(and/orManuallyDrop::drop()) requirements fully apply. -
Dought not to bepin::DropFlags::Yes.If it is, then careful with variance! Also,
ptrmust be pointing to the.valuefield of apin::ManualOption, with exclusive write provenance over the wholeManualOption.- (currently that field is not exposed at all publicly since it is a very finicky requirement).
sourcepub fn into_raw(
self: OwnRef<'slot, T, D>
) -> (*mut ManuallyDrop<T>, [&'slot (); 0])
pub fn into_raw( self: OwnRef<'slot, T, D> ) -> (*mut ManuallyDrop<T>, [&'slot (); 0])
Converts the OwnRef back into its constituent raw pointer,
disabling the Drop glue, and whatnot.
The returned pair is conceptually equivalent to a
&'slot ManuallyDrop<T>, but the usage of a raw
pointer avoids shrinking provenance of the pointer, which matters
when D is DropFlags::Yes.
source§impl<'slot, T> OwnRef<'slot, T, Yes>
impl<'slot, T> OwnRef<'slot, T, Yes>
sourcepub fn with_pinned<R>(
value: T,
scope: impl FnOnce(Pin<OwnRef<'_, T, Yes>>) -> R
) -> R
pub fn with_pinned<R>( value: T, scope: impl FnOnce(Pin<OwnRef<'_, T, Yes>>) -> R ) -> R
Same as OwnRef::with(), but for the value being Pinned.
Uses runtime drop flags to guard against improper memory leakage, lest unsoundness ensue.
source§impl<'slot, T: ?Sized> OwnRef<'slot, T>
impl<'slot, T: ?Sized> OwnRef<'slot, T>
sourcepub fn downcast<U>(
self: OwnRef<'slot, T>
) -> Result<OwnRef<'slot, U>, OwnRef<'slot, T>>
pub fn downcast<U>( self: OwnRef<'slot, T> ) -> Result<OwnRef<'slot, U>, OwnRef<'slot, T>>
The moral equivalent of Box::downcast, but for OwnRefs.
More like
.owncast(), am I right? 🥁
§Example
#![forbid(unsafe_code)]
use ::core::any::{Any, TypeId};
use ::own_ref::prelude::*;
fn too_generic<T : 'static>(it: T) {
// Say we want to do something special if `T` is a `String`.
match own_ref!(: T = it).downcast::<String>() {
// Ok, `T = String` here, and this property is embodied
// by `s: &own String` in this branch:
Ok(own_s) => {
let s: String = own_s.deref_move();
// …
},
Err(own_t) => {
let it: T = own_t.deref_move();
},
}
}Trait Implementations§
source§impl<'slot, Args, F: ?Sized + FnOwn<Args>> FnOwn<Args> for OwnRef<'slot, F>
impl<'slot, Args, F: ?Sized + FnOwn<Args>> FnOwn<Args> for OwnRef<'slot, F>
source§fn call_ownref(self, args: Args) -> Self::Ret
fn call_ownref(self, args: Args) -> Self::Ret
extern "rust-call" fn call_once.source§fn call_ownref_1<_0>(self, _0: _0) -> Self::Ret
fn call_ownref_1<_0>(self, _0: _0) -> Self::Ret
Self::call_ownref(). Read moresource§fn call_ownref_11<_10, _9, _8, _7, _6, _5, _4, _3, _2, _1, _0>(
self,
_10: _10,
_9: _9,
_8: _8,
_7: _7,
_6: _6,
_5: _5,
_4: _4,
_3: _3,
_2: _2,
_1: _1,
_0: _0
) -> Self::Retwhere
Self: Sized,
(_10, _9, _8, _7, _6, _5, _4, _3, _2, _1, _0): Is<ItSelf = Args>,
fn call_ownref_11<_10, _9, _8, _7, _6, _5, _4, _3, _2, _1, _0>(
self,
_10: _10,
_9: _9,
_8: _8,
_7: _7,
_6: _6,
_5: _5,
_4: _4,
_3: _3,
_2: _2,
_1: _1,
_0: _0
) -> Self::Retwhere
Self: Sized,
(_10, _9, _8, _7, _6, _5, _4, _3, _2, _1, _0): Is<ItSelf = Args>,
Self::call_ownref(). Read more