[][src]Struct cpp_core::Ptr

pub struct Ptr<T>(_);

A const pointer to a C++ object (similar to a C++ pointer).

A Ptr may or may not be owned. If you actually own the object, it's recommended to convert it to CppBox using to_box method.

Note that unlike Rust references, Ptr can be freely copied, producing multiple pointers to the same object, which is usually necessary to do when working with C++ libraries.

Ptr implements operator traits and delegates them to the corresponding C++ operators. This means that you can use &ptr + value to access the object's operator+.

Ptr 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.

Ptr can contain a null pointer. Deref will panic if attempted to dereference a null pointer.

If the object provides an iterator interface through begin() and end() functions, Ptr will implement IntoIterator, so you can iterate on it 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 Ptr exists. Note that with Ptr, it's possible to call unsafe C++ code without using any more unsafe Rust code, for example, by using operator traits, so care should be taken when exposing Ptr in a safe interface.

Null pointers must not be dereferenced.

Methods

impl<T> Ptr<T>[src]

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

Creates a Ptr from a raw pointer.

Safety

See type level documentation.

pub unsafe fn null() -> Self[src]

Creates a null pointer.

Note that you can also use NullPtr to specify a null pointer to a function accepting impl CastInto<Ptr<_>>. Unlike Ptr, NullPtr is not a generic type, so it will not cause type inference issues.

Safety

Null pointers must not be dereferenced. See type level documentation.

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

Returns the content as a raw const pointer.

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

Returns the content as a const Ref. Returns None if self is a null pointer.

Safety

The operation is safe as long as self is valid or null. See type level documentation.

pub fn is_null(self) -> bool[src]

Returns true if the pointer is null.

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

Converts the pointer to the base class type U.

Safety

This operation is safe as long as self is valid or null.

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

Converts the pointer to the derived class type U.

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, of if self is a null pointer.

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

Converts the pointer to the derived class type U. 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 or null.

impl Ptr<c_char>[src]

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

pub unsafe fn from_c_str(str: &CStr) -> Self[src]

Creates a Ptr<c_char>, i.e. C++'s const char* from a CStr.

Safety

The source str must be valid while Ptr exists and while it's used by the C++ library.

After passing str to Ptr, it's unsafe to use str and any references to the same buffer from Rust because the memory can be modified through Ptr.

pub unsafe fn to_c_str<'a>(self) -> &'a CStr[src]

Converts Ptr<c_char>, i.e. C++'s const char* to a &CStr.

Safety

No guarantees can be made about the validity and lifetime of the buffer, since it could be produced by a C++ library.

Trait Implementations

impl<T, U> CastFrom<MutPtr<U>> for Ptr<T> where
    U: StaticUpcast<T>, 
[src]

impl<T, U> CastFrom<Ref<U>> for Ptr<T> where
    U: StaticUpcast<T>, 
[src]

impl<T, U> CastFrom<MutRef<U>> for Ptr<T> where
    U: StaticUpcast<T>, 
[src]

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

impl<T, U> CastFrom<Ptr<U>> for Ptr<T> where
    U: StaticUpcast<T>, 
[src]

impl<T> CastFrom<NullPtr> for Ptr<T>[src]

impl<'a> CastFrom<&'a CStr> for Ptr<c_char>[src]

impl<T, U> CastFrom<*const U> for Ptr<T> where
    U: StaticUpcast<T>, 
[src]

impl<T, U> CastFrom<*mut U> for Ptr<T> where
    U: StaticUpcast<T>, 
[src]

impl<T, T1, T2> IntoIterator for Ptr<T> where
    T: Begin<Output = CppBox<T1>> + End<Output = CppBox<T2>>,
    T1: CppDeletable + PartialEq<Ref<T2>> + Increment + Indirection,
    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> Clone for Ptr<T>[src]

Creates another pointer to the same object.

impl<T> Copy for Ptr<T>[src]

Creates another pointer to the same object.

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

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

impl<T> Debug for Ptr<T>[src]

impl<T: 'static, U> Div<U> for Ptr<T> where
    &'static T: Div<U>, 
[src]

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

The resulting type after applying the / operator.

impl<T: 'static, U> Rem<U> for Ptr<T> where
    &'static T: Rem<U>, 
[src]

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

The resulting type after applying the % operator.

impl<T: 'static, U> Sub<U> for Ptr<T> where
    &'static T: Sub<U>, 
[src]

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

The resulting type after applying the - operator.

impl<T: 'static, U> Add<U> for Ptr<T> where
    &'static T: Add<U>, 
[src]

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

The resulting type after applying the + operator.

impl<T: 'static, U> Mul<U> for Ptr<T> where
    &'static T: Mul<U>, 
[src]

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

The resulting type after applying the * operator.

impl<T: 'static, U> BitAnd<U> for Ptr<T> where
    &'static T: BitAnd<U>, 
[src]

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

The resulting type after applying the & operator.

impl<T: 'static, U> BitOr<U> for Ptr<T> where
    &'static T: BitOr<U>, 
[src]

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

The resulting type after applying the | operator.

impl<T: 'static, U> BitXor<U> for Ptr<T> where
    &'static T: BitXor<U>, 
[src]

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

The resulting type after applying the ^ operator.

impl<T: 'static, U> Shl<U> for Ptr<T> where
    &'static T: Shl<U>, 
[src]

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

The resulting type after applying the << operator.

impl<T: 'static, U> Shr<U> for Ptr<T> where
    &'static T: Shr<U>, 
[src]

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

The resulting type after applying the >> operator.

impl<T> Deref for Ptr<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.

Auto Trait Implementations

impl<T> !Send for Ptr<T>

impl<T> !Sync for Ptr<T>

impl<T> Unpin for Ptr<T>

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

impl<T> RefUnwindSafe for Ptr<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<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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]