[][src]Struct abi_stable::sabi_types::MovePtr

#[repr(transparent)]pub struct MovePtr<'a, T> { /* fields omitted */ }

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) );


Implementations

impl<'a, T> MovePtr<'a, T>[src]

pub unsafe fn new(ptr: &'a mut T) -> Self[src]

Constructs this move 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, otherwise the value will be leaked.

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>

Notable traits for RBox<I>

impl<I> Iterator for RBox<I> where
    I: Iterator
type Item = I::Item;impl<T> Read for RBox<T> where
    T: Read
impl<T> Write for RBox<T> where
    T: Write
impl<F> Future for RBox<F> where
    F: Future + Unpin
type Output = F::Output;
[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> Debug for MovePtr<'a, T> where
    T: Debug
[src]

impl<'a, T> Deref for MovePtr<'a, T>[src]

type Target = T

The resulting type after dereferencing.

impl<'a, T> DerefMut for MovePtr<'a, T>[src]

impl<'a, T> Display for MovePtr<'a, T> where
    T: Display
[src]

impl<'a, T> Drop for MovePtr<'a, T>[src]

impl<'a, T> Eq for MovePtr<'a, T> where
    T: Eq
[src]

impl<'a, T> GetStaticEquivalent_ for MovePtr<'a, T> where
    T: __StableAbi,
    T: 'a, 
[src]

type StaticEquivalent = _static_MovePtr<'static, __GetStaticEquivalent<T>>

impl<'a, T> Hash for MovePtr<'a, T> where
    T: Hash
[src]

impl<'a, T> IntoInner for MovePtr<'a, T>[src]

type Element = T

The type of the value this owns.

impl<'a, T> Ord for MovePtr<'a, T> where
    T: Ord
[src]

impl<'a, T> PartialEq<MovePtr<'a, T>> for MovePtr<'a, T> where
    T: PartialEq
[src]

impl<'a, T> PartialOrd<MovePtr<'a, T>> for MovePtr<'a, T> where
    T: PartialOrd
[src]

impl<'a, T: Send> Send for MovePtr<'a, T>[src]

impl<'a, T> StableAbi for MovePtr<'a, T> where
    T: __StableAbi,
    T: 'a, 
[src]

type IsNonZeroType = <NonNull<T> as __StableAbi>::IsNonZeroType

Whether this type has a single invalid bit-pattern. Read more

impl<'a, T: Sync> Sync for MovePtr<'a, T>[src]

Auto Trait Implementations

impl<'a, T> RefUnwindSafe for MovePtr<'a, T> where
    T: RefUnwindSafe
[src]

impl<'a, T> Unpin for MovePtr<'a, T>[src]

impl<'a, T> !UnwindSafe for MovePtr<'a, T>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> GetWithMetadata for T[src]

type ForSelf = WithMetadata_<T, T>

This is always WithMetadata_<Self, Self>

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> SelfOps for T where
    T: ?Sized
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<This> TransmuteElement for This where
    This: ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The error type returned when the conversion fails.

impl<T> TypeIdentity for T where
    T: ?Sized
[src]

type Type = T

The same type as Self. Read more