[][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, 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: Ptr<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 Ptr.

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: *const 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.

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 fn as_mut_raw_ptr(&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) -> Ptr<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_raw_ref<'a>(&self) -> &'a T[src]

Returns a reference to the value.

Safety

self must be valid. The content must not be read or modified through other ways while the returned reference exists.See type level documentation.

pub unsafe fn as_mut_raw_ref<'a>(&self) -> &'a mut T[src]

Returns a mutable reference to the value.

Safety

self must be valid. The content must not be read or modified through other ways while the returned reference exists.See type level documentation.

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.

impl<V, T> CppBox<V> where
    V: Data<Output = *const T> + Size + CppDeletable
[src]

pub unsafe fn as_slice<'a>(&self) -> &'a [T][src]

Returns the content of the object as a slice, based on data() and size() methods.

Safety

The caller must make sure self contains a valid pointer. The content must not be read or modified through other ways while the returned slice exists. This function may invoke arbitrary foreign code, so no safety guarantees can be made.

impl<V, T> CppBox<V> where
    V: DataMut<Output = *mut T> + Size + CppDeletable
[src]

pub unsafe fn as_mut_slice<'a>(&self) -> &'a mut [T][src]

Returns the content of the vector as a mutable slice, based on data() and size() methods.

Safety

The caller must make sure self contains a valid pointer. The content must not be read or modified through other ways while the returned slice exists. This function may invoke arbitrary foreign code, so no safety guarantees can be made.

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

pub unsafe fn iter(&self) -> CppIterator<T1, T2>[src]

Returns an iterator over the content of the object, based on begin() and end() methods.

Safety

The caller must make sure self contains a valid pointer. The content must not be read or modified through other ways while the returned slice exists. This function may invoke arbitrary foreign code, so no safety guarantees can be made.

impl<T, T1, T2> CppBox<T> where
    T: BeginMut<Output = CppBox<T1>> + EndMut<Output = CppBox<T2>> + CppDeletable,
    T1: CppDeletable + PartialEq<Ref<T2>> + Increment + Indirection,
    T2: CppDeletable
[src]

pub unsafe fn iter_mut(&self) -> CppIterator<T1, T2>[src]

Returns a mutable iterator over the content of the object, based on begin() and end() methods.

Safety

The caller must make sure self contains a valid pointer. The content must not be read or modified through other ways while the returned slice exists. This function may invoke arbitrary foreign code, so no safety guarantees can be made.

Trait Implementations

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> 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, U: CppDeletable> CastFrom<&'a CppBox<U>> for Ptr<T> where
    U: StaticUpcast<T>, 
[src]

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

impl<T: CppDeletable> Debug for CppBox<T>[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<'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<T: CppDeletable> Drop for CppBox<T>[src]

Deletes the stored object using C++'s delete 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> 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<'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> 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<'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.

Auto Trait Implementations

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

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

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, U> Into<U> for T where
    U: From<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.