pub struct CppLtRef<'a, T: ?Sized> { /* private fields */ }Expand description
A CppRef with an associated lifetime. This can be used in place of
any CppRef due to a Deref implementation.
Implementations§
Source§impl<T: ?Sized> CppLtRef<'_, T>
impl<T: ?Sized> CppLtRef<'_, T>
Sourcepub fn lifetime_cast(&self) -> CppRef<T>
pub fn lifetime_cast(&self) -> CppRef<T>
Extend the lifetime of the returned reference beyond normal Rust borrow checker rules.
Normally, a reference can’t be used beyond the lifetime of the object which gave it to you, but sometimes C++ APIs can return references to global or other longer-lived objects. In such a case you should use this method to get a longer-lived reference.
§Usage
When you’re given a C++ reference and you know its referent is valid
for a long time, use this method. Store the resulting CppRef
somewhere in Rust with an equivalent lifetime.
§Safety
Because CppRefs are never dereferenced in Rust, misuse of this API
cannot lead to undefined behavior in Rust and is therefore not
unsafe. Nevertheless this can lead to UB in C++, so use carefully.
Methods from Deref<Target = CppRef<T>>§
Sourcepub unsafe fn as_ref(&self) -> &T
pub unsafe fn as_ref(&self) -> &T
Get a regular Rust reference out of this C++ reference.
§Safety
Callers must guarantee that the referent is not modified by any other C++ or Rust code while the returned reference exists. Callers must also guarantee that no mutable Rust reference is created to the referent while the returned reference exists.
Callers must also be sure that the C++ reference is properly aligned, not null, pointing to valid data, etc.
Sourcepub fn const_cast(&self) -> CppMutRef<T>
pub fn const_cast(&self) -> CppMutRef<T>
Create a mutable version of this reference, roughly equivalent
to C++ const_cast.
The opposite is to use AsCppRef::as_cpp_ref on a CppMutRef
to obtain a CppRef.
§Safety
Because we never dereference a CppRef in Rust, this cannot create
undefined behavior within Rust and is therefore not unsafe. It is
however generally unwise, just as it is in C++. Use sparingly.