pub struct Ref<T>(/* private fields */);
Expand description
A non-null, mutable pointer to a C++ object (similar to a C++ reference).
Ref
never owns its content.
Note that unlike Rust references, Ref
can be freely copied,
producing multiple mutable pointers to the same object, which is usually necessary
to do when working with C++ libraries.
Ref
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+
.
Ref
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,
Ref
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 Ref
exists. Note that
with Ref
, 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
Ref
in a safe interface.
Implementations§
Source§impl<T> Ref<T>
impl<T> Ref<T>
Sourcepub unsafe fn new(ptr: Ptr<T>) -> Option<Self>
pub unsafe fn new(ptr: Ptr<T>) -> Option<Self>
Creates a Ref
from a Ptr
. Returns None
if ptr
is null.
§Safety
ptr
must be valid. See type level documentation.
Sourcepub unsafe fn from_raw(ptr: *const T) -> Option<Self>
pub unsafe fn from_raw(ptr: *const T) -> Option<Self>
Creates a Ref
from a raw pointer. Returns None
if ptr
is null.
§Safety
ptr
must be valid. See type level documentation.
Sourcepub unsafe fn from_raw_non_null(ptr: NonNull<T>) -> Self
pub unsafe fn from_raw_non_null(ptr: NonNull<T>) -> Self
Sourcepub unsafe fn as_raw_ref<'a>(self) -> &'a T
pub unsafe fn as_raw_ref<'a>(self) -> &'a T
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.
Sourcepub unsafe fn as_mut_raw_ref<'a>(self) -> &'a mut T
pub unsafe fn as_mut_raw_ref<'a>(self) -> &'a mut T
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.
Sourcepub fn as_raw_ptr(self) -> *const T
pub fn as_raw_ptr(self) -> *const T
Returns constant raw pointer to the value.
Sourcepub fn as_mut_raw_ptr(self) -> *mut T
pub fn as_mut_raw_ptr(self) -> *mut T
Returns constant raw pointer to the value.
Sourcepub unsafe fn static_upcast<U>(self) -> Ref<U>where
T: StaticUpcast<U>,
pub unsafe fn static_upcast<U>(self) -> Ref<U>where
T: StaticUpcast<U>,
Converts the pointer to the base class type U
.
§Safety
This operation is safe as long as self
is valid.
Sourcepub unsafe fn static_downcast<U>(self) -> Ref<U>where
T: StaticDowncast<U>,
pub unsafe fn static_downcast<U>(self) -> Ref<U>where
T: StaticDowncast<U>,
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
.
Sourcepub unsafe fn dynamic_cast<U>(self) -> Option<Ref<U>>where
T: DynamicCast<U>,
pub unsafe fn dynamic_cast<U>(self) -> Option<Ref<U>>where
T: DynamicCast<U>,
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.
Source§impl<V, T> Ref<V>
impl<V, T> Ref<V>
Sourcepub unsafe fn as_slice<'a>(self) -> &'a [T]
pub unsafe fn as_slice<'a>(self) -> &'a [T]
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.
Source§impl<V, T> Ref<V>
impl<V, T> Ref<V>
Sourcepub unsafe fn as_mut_slice<'a>(self) -> &'a mut [T]
pub unsafe fn as_mut_slice<'a>(self) -> &'a mut [T]
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.
Source§impl<T, T1, T2> Ref<T>where
T: Begin<Output = CppBox<T1>> + End<Output = CppBox<T2>>,
T1: CppDeletable + PartialEq<Ref<T2>> + Increment + Indirection,
T2: CppDeletable,
impl<T, T1, T2> Ref<T>where
T: Begin<Output = CppBox<T1>> + End<Output = CppBox<T2>>,
T1: CppDeletable + PartialEq<Ref<T2>> + Increment + Indirection,
T2: CppDeletable,
Sourcepub unsafe fn iter(self) -> CppIterator<T1, T2> ⓘ
pub unsafe fn iter(self) -> CppIterator<T1, T2> ⓘ
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.
Source§impl<T, T1, T2> Ref<T>where
T: BeginMut<Output = CppBox<T1>> + EndMut<Output = CppBox<T2>>,
T1: CppDeletable + PartialEq<Ref<T2>> + Increment + Indirection,
T2: CppDeletable,
impl<T, T1, T2> Ref<T>where
T: BeginMut<Output = CppBox<T1>> + EndMut<Output = CppBox<T2>>,
T1: CppDeletable + PartialEq<Ref<T2>> + Increment + Indirection,
T2: CppDeletable,
Sourcepub unsafe fn iter_mut(self) -> CppIterator<T1, T2> ⓘ
pub unsafe fn iter_mut(self) -> CppIterator<T1, T2> ⓘ
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§
Source§impl<'a, T, U> CastFrom<&'a CppBox<U>> for Ref<T>where
U: StaticUpcast<T> + CppDeletable,
impl<'a, T, U> CastFrom<&'a CppBox<U>> for Ref<T>where
U: StaticUpcast<T> + CppDeletable,
Source§impl<T> Deref for Ref<T>
Allows to call member functions of T
and its base classes directly on the pointer.
impl<T> Deref for Ref<T>
Allows to call member functions of T
and its base classes directly on the pointer.
Source§impl<T, U> PartialOrd<U> for Ref<T>
impl<T, U> PartialOrd<U> for Ref<T>
impl<T> Copy for Ref<T>
Creates another pointer to the same object.