[][src]Struct cpp_core::CppBox

pub struct CppBox<T: CppDeletable>(_);

An owning pointer to a C++ object.

CppBox is automatically used in places where C++ class objects are passed by value and in return values of constructors because the ownership is apparent in these cases. However, sometimes an object is returned as a pointer but you must accept the ownership of the object. It's not possible to automatically determine ownership semantics of C++ code in this case, so you should manually convert Ptr to CppBox using to_box method.

When CppBox is dropped, it will automatically delete the object using C++'s delete operator.

Objects stored in CppBox are usually placed on the heap by the C++ code.

If a C++ API accepts an object by pointer and takes ownership of it, it's not possible to automatically detect this, so you must manually convert CppBox to a non-owning Ptr using into_ptr before passing it to such a function.

&CppBox<T> and &mut CppBox<T> implement operator traits and delegate them to the corresponding C++ operators. This means that you can use &box1 + value to access the object's operator+.

CppBox implements Deref and DerefMut, allowing to call the object's methods directly. In addition, methods of the object's first base class are also directly available thanks to nested Deref implementations.

If the object provides an iterator interface through begin() and end() functions, &CppBox<T> and &mut CppBox<T> will implement IntoIterator, so you can iterate on them directly.

Safety

It's not possible to automatically track the ownership of objects possibly managed by C++ libraries. The user must ensure that the object is alive while CppBox exists and that no pointers to the object are used after the object is deleted by CppBox's Drop implementation. Note that with CppBox, it's possible to call unsafe C++ code without using any more unsafe code, for example, by using operator traits or simply dropping the box, so care should be taken when exposing CppBox in a safe interface.

Methods

impl<T: CppDeletable> CppBox<T>[src]

pub unsafe fn new(ptr: MutPtr<T>) -> Option<Self>[src]

Encapsulates the object into a CppBox. Returns None if the pointer is null.

The same operation can be done by calling to_box function on MutPtr.

You should use this function only for pointers that were created on C++ side and passed through a FFI boundary to Rust. An object created with C++ new must be deleted using C++ delete, which is executed by CppBox.

Do not use this function for objects that would be deleted by other means. If another C++ object is the owner of the passed object, it will attempt to delete it. If CppBox containing the object still exists, it would result in a double deletion, which must never happen.

Use CppBox::into_ptr to unwrap the pointer before passing it to a function that takes ownership of the object.

Safety

The pointer must point to an object that can be safely deleted using C++'s delete operator. The object must not be deleted by other means while CppBox exists. Any other pointers to the object must not be used after CppBox is dropped.

pub unsafe fn from_raw(ptr: *mut T) -> Option<Self>[src]

Encapsulates the object into a CppBox. Returns None if the pointer is null.

See CppBox::new for more information.

Safety

The pointer must point to an object that can be safely deleted using C++'s delete operator. The object must not be deleted by other means while CppBox exists. Any other pointers to the object must not be used after CppBox is dropped.

impl<T: CppDeletable> CppBox<T>[src]

pub unsafe fn as_ptr(&self) -> Ptr<T>[src]

Returns a constant pointer to the value in the box.

Safety

This operation is safe as long as self is valid.

pub unsafe fn as_mut_ptr(&mut self) -> MutPtr<T>[src]

Returns a mutable pointer to the value in the box.

Safety

This operation is safe as long as self is valid.

pub fn as_mut_raw_ptr(&mut self) -> *mut T[src]

Returns a constant raw pointer to the value in the box.

pub fn as_raw_ptr(&self) -> *const T[src]

Returns a mutable raw pointer to the value in the box.

pub fn into_raw_ptr(self) -> *mut T[src]

Destroys the box without deleting the object and returns a raw pointer to the content. The caller of the function becomes the owner of the object and should ensure that the object will be deleted at some point.

pub unsafe fn into_ptr(self) -> MutPtr<T>[src]

Destroys the box without deleting the object and returns a pointer to the content. The caller of the function becomes the owner of the object and should ensure that the object will be deleted at some point.

Safety

This operation is safe as long as self is valid.

pub unsafe fn as_ref(&self) -> Ref<T>[src]

Returns a constant reference to the value in the box.

Safety

This operation is safe as long as self is valid.

pub unsafe fn as_mut_ref(&mut self) -> MutRef<T>[src]

Returns a mutable reference to the value in the box.

Safety

This operation is safe as long as self is valid.

pub unsafe fn static_upcast<U>(&self) -> Ref<U> where
    T: StaticUpcast<U>, 
[src]

Returns a non-owning reference to the content converted to the base class type U. CppBox retains the ownership of the object.

Safety

This operation is safe as long as self is valid.

pub unsafe fn static_downcast<U>(&self) -> Ref<U> where
    T: StaticDowncast<U>, 
[src]

Returns a non-owning reference to the content converted to the derived class type U. CppBox retains the ownership of the object.

It's recommended to use dynamic_cast instead because it performs a checked conversion.

Safety

This operation is safe as long as self is valid and it's type is U or inherits from U.

pub unsafe fn dynamic_cast<U>(&self) -> Option<Ref<U>> where
    T: DynamicCast<U>, 
[src]

Returns a non-owning reference to the content converted to the derived class type U. CppBox retains the ownership of the object. Returns None if the object's type is not U and doesn't inherit U.

Safety

This operation is safe as long as self is valid.

pub unsafe fn static_upcast_mut<U>(&mut self) -> MutRef<U> where
    T: StaticUpcast<U>, 
[src]

Returns a non-owning reference to the content converted to the base class type U. CppBox retains the ownership of the object.

Safety

This operation is safe as long as self is valid.

pub unsafe fn static_downcast_mut<U>(&mut self) -> MutRef<U> where
    T: StaticDowncast<U>, 
[src]

Returns a non-owning reference to the content converted to the derived class type U. CppBox retains the ownership of the object.

It's recommended to use dynamic_cast instead because it performs a checked conversion.

Safety

This operation is safe as long as self is valid and it's type is U or inherits from U.

pub unsafe fn dynamic_cast_mut<U>(&mut self) -> Option<MutRef<U>> where
    T: DynamicCast<U>, 
[src]

Returns a non-owning reference to the content converted to the derived class type U. CppBox retains the ownership of the object. Returns None if the object's type is not U and doesn't inherit U.

Safety

This operation is safe as long as self is valid.

Trait Implementations

impl<'a, T, U: CppDeletable> CastFrom<&'a CppBox<U>> for Ptr<T> where
    U: StaticUpcast<T>, 
[src]

impl<'a, T, U: CppDeletable> CastFrom<&'a mut CppBox<U>> for MutPtr<T> where
    U: StaticUpcast<T>, 
[src]

impl<'a, T, U: CppDeletable> CastFrom<&'a CppBox<U>> for Ref<T> where
    U: StaticUpcast<T>, 
[src]

impl<'a, T, U: CppDeletable> CastFrom<&'a mut CppBox<U>> for MutRef<T> where
    U: StaticUpcast<T>, 
[src]

impl<T: CppDeletable> Drop for CppBox<T>[src]

Deletes the stored object using C++'s delete operator.

impl<'a, T, T1, T2> IntoIterator for &'a CppBox<T> where
    T: CppDeletable + Begin<Output = CppBox<T1>> + End<Output = CppBox<T2>>,
    T1: CppDeletable + PartialEq<Ref<T2>> + Indirection + Increment,
    T2: CppDeletable
[src]

type Item = <T1 as Indirection>::Output

The type of the elements being iterated over.

type IntoIter = CppIterator<T1, T2>

Which kind of iterator are we turning this into?

impl<'a, T, T1, T2> IntoIterator for &'a mut CppBox<T> where
    T: CppDeletable + BeginMut<Output = CppBox<T1>> + EndMut<Output = CppBox<T2>>,
    T1: CppDeletable + PartialEq<Ref<T2>> + Indirection + Increment,
    T2: CppDeletable
[src]

type Item = <T1 as Indirection>::Output

The type of the elements being iterated over.

type IntoIter = CppIterator<T1, T2>

Which kind of iterator are we turning this into?

impl<T: CppDeletable, U> PartialEq<U> for CppBox<T> where
    T: PartialEq<U>, 
[src]

impl<T: CppDeletable, U> PartialOrd<U> for CppBox<T> where
    T: Lt<U> + Le<U> + Gt<U> + Ge<U> + PartialEq<U>, 
[src]

impl<T: CppDeletable> Debug for CppBox<T>[src]

impl<'a, T: CppDeletable, U> Div<U> for &'a CppBox<T> where
    &'a T: Div<U>, 
[src]

type Output = <&'a T as Div<U>>::Output

The resulting type after applying the / operator.

impl<'a, T: CppDeletable, U> Rem<U> for &'a CppBox<T> where
    &'a T: Rem<U>, 
[src]

type Output = <&'a T as Rem<U>>::Output

The resulting type after applying the % operator.

impl<'a, T: CppDeletable, U> Sub<U> for &'a CppBox<T> where
    &'a T: Sub<U>, 
[src]

type Output = <&'a T as Sub<U>>::Output

The resulting type after applying the - operator.

impl<'a, T: CppDeletable, U> Add<U> for &'a CppBox<T> where
    &'a T: Add<U>, 
[src]

type Output = <&'a T as Add<U>>::Output

The resulting type after applying the + operator.

impl<'a, T: CppDeletable, U> Mul<U> for &'a CppBox<T> where
    &'a T: Mul<U>, 
[src]

type Output = <&'a T as Mul<U>>::Output

The resulting type after applying the * operator.

impl<T: CppDeletable, U> AddAssign<U> for CppBox<T> where
    T: AddAssign<U>, 
[src]

impl<T: CppDeletable, U> SubAssign<U> for CppBox<T> where
    T: SubAssign<U>, 
[src]

impl<T: CppDeletable, U> MulAssign<U> for CppBox<T> where
    T: MulAssign<U>, 
[src]

impl<T: CppDeletable, U> DivAssign<U> for CppBox<T> where
    T: DivAssign<U>, 
[src]

impl<T: CppDeletable, U> RemAssign<U> for CppBox<T> where
    T: RemAssign<U>, 
[src]

impl<'a, T: CppDeletable, U> BitAnd<U> for &'a CppBox<T> where
    &'a T: BitAnd<U>, 
[src]

type Output = <&'a T as BitAnd<U>>::Output

The resulting type after applying the & operator.

impl<'a, T: CppDeletable, U> BitOr<U> for &'a CppBox<T> where
    &'a T: BitOr<U>, 
[src]

type Output = <&'a T as BitOr<U>>::Output

The resulting type after applying the | operator.

impl<'a, T: CppDeletable, U> BitXor<U> for &'a CppBox<T> where
    &'a T: BitXor<U>, 
[src]

type Output = <&'a T as BitXor<U>>::Output

The resulting type after applying the ^ operator.

impl<'a, T: CppDeletable, U> Shl<U> for &'a CppBox<T> where
    &'a T: Shl<U>, 
[src]

type Output = <&'a T as Shl<U>>::Output

The resulting type after applying the << operator.

impl<'a, T: CppDeletable, U> Shr<U> for &'a CppBox<T> where
    &'a T: Shr<U>, 
[src]

type Output = <&'a T as Shr<U>>::Output

The resulting type after applying the >> operator.

impl<T: CppDeletable, U> BitAndAssign<U> for CppBox<T> where
    T: BitAndAssign<U>, 
[src]

impl<T: CppDeletable, U> BitOrAssign<U> for CppBox<T> where
    T: BitOrAssign<U>, 
[src]

impl<T: CppDeletable, U> BitXorAssign<U> for CppBox<T> where
    T: BitXorAssign<U>, 
[src]

impl<T: CppDeletable, U> ShlAssign<U> for CppBox<T> where
    T: ShlAssign<U>, 
[src]

impl<T: CppDeletable, U> ShrAssign<U> for CppBox<T> where
    T: ShrAssign<U>, 
[src]

impl<T: CppDeletable> Deref for CppBox<T>[src]

Allows to call member functions of T and its base classes directly on the pointer.

type Target = T

The resulting type after dereferencing.

impl<T: CppDeletable> DerefMut for CppBox<T>[src]

Allows to call member functions of T and its base classes directly on the pointer.

Auto Trait Implementations

impl<T> !Send for CppBox<T>

impl<T> !Sync for CppBox<T>

impl<T> Unpin for CppBox<T>

impl<T> UnwindSafe for CppBox<T> where
    T: RefUnwindSafe

impl<T> RefUnwindSafe for CppBox<T> where
    T: RefUnwindSafe

Blanket Implementations

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

impl<T> From<T> for T[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> Borrow<T> for T where
    T: ?Sized
[src]

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

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