[−][src]Struct abi_stable::sabi_types::move_ptr::MovePtr
A move pointer,which allows moving the value from the reference, consuming it in the process.
if MovePtr::into_inner isn't called, this drops the referenced value when its dropped
Safety
This is unsafe to construct since the user must ensure that the value being referenced is not read again,even when being dropped.
Motivation
MovePtr was created as a way to pass self by value to ffi-safe trait object methods, since one can't simply pass self by value(because the type is erased).
Examples
Using OwnedPointer::in_move_ptr
This is how one can use MovePtr without unsafe
.
This simply moves the contents of an RBox<T>
into a Box<T>
.
use abi_stable::{ pointer_trait::OwnedPointer, sabi_types::MovePtr, std_types::RBox, }; fn move_rbox_to_box<T>(rbox:RBox<T>)->Box<T>{ rbox.in_move_ptr(|move_ptr|{ MovePtr::into_box(move_ptr) }) } assert_eq!( move_rbox_to_box(RBox::new(99)), Box::new(99) ); assert_eq!( move_rbox_to_box(RBox::new(())), Box::new(()) ); assert_eq!( move_rbox_to_box(RBox::new(String::from("SHIT"))), Box::new(String::from("SHIT")) );
Using the (unsafe) MovePtr::new
This is (sort of) how RBox<T>
implements moving the T it owns out of its allocation
This is basically what OwnedPointer::{with_move_ptr,in_move_ptr}
do.
use abi_stable::{ pointer_trait::OwnedPointer, sabi_types::MovePtr, std_types::RBox, }; use std::mem::ManuallyDrop; let rbox=RBox::new(0x100); let second_rbox; unsafe{ let mut rbox=ManuallyDrop::new(rbox); let move_ptr=unsafe{ MovePtr::new( &mut **rbox ) }; second_rbox=RBox::from_move_ptr(move_ptr); OwnedPointer::drop_allocation(&mut rbox); } assert_eq!( second_rbox, RBox::new(0x100) );
Methods
impl<'a, T> MovePtr<'a, T>
[src]
pub unsafe fn new(ptr: &'a mut T) -> Self
[src]
Constructs this more pointer from a mutable reference, moving the value out of the reference.
Safety
Callers must ensure that the value the reference points at is never read again.
Example
use abi_stable::sabi_types::MovePtr; use std::mem::ManuallyDrop; let mut manual=ManuallyDrop::new(String::from("hello")); let moveptr=unsafe{ MovePtr::new(&mut *manual) }; drop(moveptr); // moveptr drops the String here.
pub const fn as_ptr(this: &Self) -> *const T
[src]
Gets a raw pointer to the value being moved.
Example
use abi_stable::{ pointer_trait::OwnedPointer, sabi_types::MovePtr, std_types::RBox, }; let rbox=RBox::new(String::from("NOPE")); let address_rbox=&*rbox as *const String as usize; rbox.in_move_ptr(|move_ptr|{ assert_eq!( address_rbox, MovePtr::as_ptr(&move_ptr) as usize ); });
pub fn as_mut_ptr(this: &mut Self) -> *mut T
[src]
Gets a raw pointer to the value being moved.
Example
Example
use abi_stable::{ pointer_trait::OwnedPointer, sabi_types::MovePtr, std_types::RBox, }; let rbox=RBox::new(String::from("NOPE")); let address_rbox=&*rbox as *const String as usize; rbox.in_move_ptr(|mut move_ptr|{ assert_eq!( address_rbox, MovePtr::as_mut_ptr(&mut move_ptr) as usize ); });
pub const fn into_raw(this: Self) -> *mut T
[src]
Converts this MovePtr into a raw pointer, which must be moved from before the pointed to value is deallocated.
Example
use abi_stable::{ pointer_trait::OwnedPointer, sabi_types::MovePtr, std_types::RBox, }; let rbox=RBox::new(String::from("NOPE")); let string=rbox.in_move_ptr(|move_ptr|unsafe{ MovePtr::into_raw(move_ptr).read() }); assert_eq!(string,String::from("NOPE"));
pub fn into_box(this: Self) -> Box<T>
[src]
Moves the value into a new Box<T>
Example
use abi_stable::{ pointer_trait::OwnedPointer, sabi_types::MovePtr, std_types::RBox, }; let rbox=RBox::new(String::from("WHAT!!!")); let boxed=rbox.in_move_ptr(|move_ptr|unsafe{ MovePtr::into_box(move_ptr) }); assert_eq!(boxed,Box::new(String::from("WHAT!!!")));
pub fn into_rbox(this: Self) -> RBox<T>
[src]
Moves the value into a new RBox<T>
Example
use abi_stable::{ pointer_trait::OwnedPointer, sabi_types::MovePtr, std_types::RBox, }; let rbox=RBox::new(String::from("WHAT!!!")); let boxed=rbox.in_move_ptr(|move_ptr|unsafe{ MovePtr::into_rbox(move_ptr) }); assert_eq!( boxed, RBox::new(String::from("WHAT!!!")) );
pub fn into_inner(this: Self) -> T
[src]
Moves the value out of the reference
Example
use abi_stable::{ pointer_trait::OwnedPointer, sabi_types::MovePtr, std_types::RBox, }; let rbox=RBox::new(String::from("(The Wi)zard(of)oz")); let string=rbox.in_move_ptr(|ptr| MovePtr::into_inner(ptr) ); assert_eq!( string, String::from("(The Wi)zard(of)oz") );
Trait Implementations
impl<'a, T> IntoInner for MovePtr<'a, T>
[src]
impl<'a, T> GetStaticEquivalent_ for MovePtr<'a, T> where
T: __StableAbi,
T: 'a,
[src]
T: __StableAbi,
T: 'a,
type StaticEquivalent = _static_MovePtr<'static, __GetStaticEquivalent<T>>
impl<'a, T> SharedStableAbi for MovePtr<'a, T> where
T: __StableAbi,
T: 'a,
[src]
T: __StableAbi,
T: 'a,
type IsNonZeroType = <*mut T as __SharedStableAbi>::IsNonZeroType
Whether this type has a single invalid bit-pattern. Read more
type Kind = __ValueKind
The kind of abi stability of this type,there are 2: Read more
const S_LAYOUT: &'static TypeLayout
[src]
const S_ABI_CONSTS: AbiConsts
[src]
impl<'a, T> Drop for MovePtr<'a, T>
[src]
impl<'a, T> Eq for MovePtr<'a, T> where
T: Eq,
[src]
T: Eq,
impl<'a, T> Ord for MovePtr<'a, T> where
T: Ord,
[src]
T: Ord,
fn cmp(&self, other: &Self) -> Ordering
[src]
fn max(self, other: Self) -> Self
1.21.0[src]
fn min(self, other: Self) -> Self
1.21.0[src]
fn clamp(self, min: Self, max: Self) -> Self
[src]
impl<'a, T> PartialEq<MovePtr<'a, T>> for MovePtr<'a, T> where
T: PartialEq,
[src]
T: PartialEq,
impl<'a, T> PartialOrd<MovePtr<'a, T>> for MovePtr<'a, T> where
T: PartialOrd,
[src]
T: PartialOrd,
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl<'a, T> Display for MovePtr<'a, T> where
T: Display,
[src]
T: Display,
impl<'a, T> Debug for MovePtr<'a, T> where
T: Debug,
[src]
T: Debug,
impl<'a, T> Deref for MovePtr<'a, T>
[src]
impl<'a, T> DerefMut for MovePtr<'a, T>
[src]
impl<'a, T> Hash for MovePtr<'a, T> where
T: Hash,
[src]
T: Hash,
Auto Trait Implementations
impl<'a, T> !Send for MovePtr<'a, T>
impl<'a, T> !Sync for MovePtr<'a, T>
impl<'a, T> Unpin for MovePtr<'a, T>
impl<'a, T> !UnwindSafe for MovePtr<'a, T>
impl<'a, T> RefUnwindSafe for MovePtr<'a, T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<This> StableAbi for This where
This: SharedStableAbi<Kind = ValueKind>,
[src]
This: SharedStableAbi<Kind = ValueKind>,
const LAYOUT: &'static TypeLayout
[src]
const ABI_CONSTS: AbiConsts
[src]
impl<This> TransmuteElement for This where
This: ?Sized,
[src]
This: ?Sized,
unsafe fn transmute_element<T>(self) -> Self::TransmutedPtr where
Self: CanTransmuteElement<T>,
Self::Target: Sized,
[src]
Self: CanTransmuteElement<T>,
Self::Target: Sized,
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> TypeIdentity for T where
T: ?Sized,
[src]
T: ?Sized,
type Type = T
The same type as Self. Read more
fn into_type_val(self) -> Self::Type where
Self::Type: Sized,
[src]
Self::Type: Sized,
fn into_type_ref(&self) -> &Self::Type
[src]
fn into_type_mut(&mut self) -> &mut Self::Type
[src]
fn into_type_box(self: Box<Self>) -> Box<Self::Type>
[src]
fn into_type_arc(this: Arc<Self>) -> Arc<Self::Type>
[src]
fn into_type_rc(this: Rc<Self>) -> Rc<Self::Type>
[src]
fn from_type_val(this: Self::Type) -> Self where
Self::Type: Sized,
[src]
Self::Type: Sized,
fn from_type_ref(this: &Self::Type) -> &Self
[src]
fn from_type_mut(this: &mut Self::Type) -> &mut Self
[src]
fn from_type_box(this: Box<Self::Type>) -> Box<Self>
[src]
fn from_type_arc(this: Arc<Self::Type>) -> Arc<Self>
[src]
fn from_type_rc(this: Rc<Self::Type>) -> Rc<Self>
[src]
impl<T> SelfOps for T where
T: ?Sized,
[src]
T: ?Sized,
const T: PhantomData<fn() -> Self>
[src]
const T_D: PhantomData<Self>
[src]
fn assert_ty(self, _other: PhantomData<fn() -> Self>) -> Self
[src]
fn assert_ty_ref(&self, _other: PhantomData<fn() -> Self>) -> &Self
[src]
fn assert_ty_mut(&mut self, _other: PhantomData<fn() -> Self>) -> &mut Self
[src]
fn ty_(&self) -> PhantomData<fn() -> Self>
[src]
fn ty_d(&self) -> PhantomData<Self>
[src]
fn ty_inv(&self) -> PhantomData<fn(Self) -> Self>
[src]
fn ty_inv_ref(&self) -> PhantomData<Cell<&Self>>
[src]
fn eq_id(&self, other: &Self) -> bool
[src]
fn piped<F, U>(self, f: F) -> U where
F: FnOnce(Self) -> U,
[src]
F: FnOnce(Self) -> U,
fn piped_ref<'a, F, U>(&'a self, f: F) -> U where
F: FnOnce(&'a Self) -> U,
[src]
F: FnOnce(&'a Self) -> U,
fn piped_mut<'a, F, U>(&'a mut self, f: F) -> U where
F: FnOnce(&'a mut Self) -> U,
[src]
F: FnOnce(&'a mut Self) -> U,
fn mutated<F>(self, f: F) -> Self where
F: FnOnce(&mut Self),
[src]
F: FnOnce(&mut Self),
fn observe<F>(self, f: F) -> Self where
F: FnOnce(&Self),
[src]
F: FnOnce(&Self),
fn into_<T>(self, PhantomData<fn() -> T>) -> T where
Self: Into<T>,
[src]
Self: Into<T>,
fn as_ref_<T>(&self) -> &T where
Self: AsRef<T>,
T: ?Sized,
[src]
Self: AsRef<T>,
T: ?Sized,
fn as_mut_<T>(&mut self) -> &mut T where
Self: AsMut<T>,
T: ?Sized,
[src]
Self: AsMut<T>,
T: ?Sized,
fn drop_(self)
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,