pub struct MutPtr<T>(/* private fields */);Expand description
A mutable pointer to a C++ object (similar to a C++ pointer).
A MutPtr 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, MutPtr can be freely copied,
producing multiple mutable pointers to the same object, which is usually necessary
to do when working with C++ libraries.
MutPtr 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+.
MutPtr 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.
MutPtr 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,
MutPtr 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 MutPtr exists. Note that
with MutPtr, 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
MutPtr in a safe interface.
Null pointers must not be dereferenced.
Implementations§
Source§impl<T> MutPtr<T>
impl<T> MutPtr<T>
Sourcepub unsafe fn null() -> Self
pub unsafe fn null() -> Self
Creates a null pointer.
Note that you can also use NullPtr to specify a null pointer to a function accepting
impl CastInto<MutPtr<_>>. Unlike MutPtr, 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.
Sourcepub fn as_raw_ptr(self) -> *const T
pub fn as_raw_ptr(self) -> *const T
Returns the content as a raw const pointer.
Sourcepub fn as_mut_raw_ptr(self) -> *mut T
pub fn as_mut_raw_ptr(self) -> *mut T
Returns the content as a raw mutable pointer.
Sourcepub unsafe fn as_ptr(self) -> Ptr<T>
pub unsafe fn as_ptr(self) -> Ptr<T>
Returns the content as a const Ptr.
§Safety
The operation is safe as long as self is valid or null. See type level documentation.
Sourcepub unsafe fn as_ref(self) -> Option<Ref<T>>
pub unsafe fn as_ref(self) -> Option<Ref<T>>
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.
Sourcepub unsafe fn as_mut_ref(self) -> Option<MutRef<T>>
pub unsafe fn as_mut_ref(self) -> Option<MutRef<T>>
Returns the content as a MutRef. 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.
Sourcepub unsafe fn static_upcast<U>(self) -> Ptr<U>where
T: StaticUpcast<U>,
pub unsafe fn static_upcast<U>(self) -> Ptr<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 or null.
Sourcepub unsafe fn static_downcast<U>(self) -> Ptr<U>where
T: StaticDowncast<U>,
pub unsafe fn static_downcast<U>(self) -> Ptr<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,
of if self is a null pointer.
Sourcepub unsafe fn dynamic_cast<U>(self) -> Ptr<U>where
T: DynamicCast<U>,
pub unsafe fn dynamic_cast<U>(self) -> Ptr<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 or null.
Sourcepub unsafe fn static_upcast_mut<U>(self) -> MutPtr<U>where
T: StaticUpcast<U>,
pub unsafe fn static_upcast_mut<U>(self) -> MutPtr<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 or null.
Sourcepub unsafe fn static_downcast_mut<U>(self) -> MutPtr<U>where
T: StaticDowncast<U>,
pub unsafe fn static_downcast_mut<U>(self) -> MutPtr<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,
of if self is a null pointer.
Sourcepub unsafe fn dynamic_cast_mut<U>(self) -> MutPtr<U>where
T: DynamicCast<U>,
pub unsafe fn dynamic_cast_mut<U>(self) -> MutPtr<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 or null.
Sourcepub unsafe fn begin(self) -> <&'static T as Begin>::Outputwhere
&'static T: Begin,
pub unsafe fn begin(self) -> <&'static T as Begin>::Outputwhere
&'static T: Begin,
Returns a C++ const iterator object pointing to the beginning of the collection.
It’s recommended to iterate directly on a MutPtr<T> when possible, using automatic
IntoIterator implementation.
§Safety
self must be valid. It’s not possible to make any guarantees about safety, since
this function calls arbitrary C++ library code.
Sourcepub unsafe fn begin_mut(self) -> <&'static mut T as BeginMut>::Outputwhere
&'static mut T: BeginMut,
pub unsafe fn begin_mut(self) -> <&'static mut T as BeginMut>::Outputwhere
&'static mut T: BeginMut,
Returns a C++ mutable iterator object pointing to the beginning of the collection.
It’s recommended to iterate directly on a MutPtr<T> when possible, using automatic
IntoIterator implementation.
§Safety
self must be valid. It’s not possible to make any guarantees about safety, since
this function calls arbitrary C++ library code.
Sourcepub unsafe fn end(self) -> <&'static T as End>::Outputwhere
&'static T: End,
pub unsafe fn end(self) -> <&'static T as End>::Outputwhere
&'static T: End,
Returns a C++ const iterator object pointing to the end of the collection.
It’s recommended to iterate directly on a MutPtr<T> when possible, using automatic
IntoIterator implementation.
§Safety
self must be valid. It’s not possible to make any guarantees about safety, since
this function calls arbitrary C++ library code.
Sourcepub unsafe fn end_mut(self) -> <&'static mut T as EndMut>::Outputwhere
&'static mut T: EndMut,
pub unsafe fn end_mut(self) -> <&'static mut T as EndMut>::Outputwhere
&'static mut T: EndMut,
Returns a C++ mutable iterator object pointing to the end of the collection.
It’s recommended to iterate directly on a MutPtr<T> when possible, using automatic
IntoIterator implementation.
§Safety
self must be valid. It’s not possible to make any guarantees about safety, since
this function calls arbitrary C++ library code.
Sourcepub unsafe fn as_slice<'a, T1>(self) -> &'a [T1]
pub unsafe fn as_slice<'a, T1>(self) -> &'a [T1]
Returns a slice corresponding to the object. This function is available when begin() and
end() functions of the object return pointers.
§Safety
self must be valid. It’s not possible to make any guarantees about safety, since
this function calls arbitrary C++ library code. It’s not recommended to store the slice
because it may be modified by the C++ library, which would violate Rust’s aliasing rules.
Sourcepub unsafe fn as_mut_slice<'a, T1>(self) -> &'a mut [T1]
pub unsafe fn as_mut_slice<'a, T1>(self) -> &'a mut [T1]
Returns a mutable slice corresponding to the object.
This function is available when begin() and
end() functions of the object return pointers.
§Safety
self must be valid. It’s not possible to make any guarantees about safety, since
this function calls arbitrary C++ library code. It’s not recommended to store the slice
because it may be modified by the C++ library, which would violate Rust’s aliasing rules.
Source§impl<T: CppDeletable> MutPtr<T>
impl<T: CppDeletable> MutPtr<T>
Source§impl MutPtr<c_char>
impl MutPtr<c_char>
Sourcepub unsafe fn from_c_str(str: &CStr) -> Self
pub unsafe fn from_c_str(str: &CStr) -> Self
Creates a MutPtr<c_char>, i.e. C++’s char* from a CStr.
§Safety
The source str must be valid
while MutPtr exists and while
it’s used by the C++ library.
After passing str to MutPtr, it’s unsafe to use str and
any references to the same buffer from Rust because
the memory can be modified through MutPtr.
Trait Implementations§
Source§impl<T, U> AddAssign<U> for MutPtr<T>where
T: AddAssign<U> + 'static,
impl<T, U> AddAssign<U> for MutPtr<T>where
T: AddAssign<U> + 'static,
Source§fn add_assign(&mut self, rhs: U)
fn add_assign(&mut self, rhs: U)
+= operation. Read moreSource§impl<T, U> BitAndAssign<U> for MutPtr<T>where
T: BitAndAssign<U> + 'static,
impl<T, U> BitAndAssign<U> for MutPtr<T>where
T: BitAndAssign<U> + 'static,
Source§fn bitand_assign(&mut self, rhs: U)
fn bitand_assign(&mut self, rhs: U)
&= operation. Read moreSource§impl<T, U> BitOrAssign<U> for MutPtr<T>where
T: BitOrAssign<U> + 'static,
impl<T, U> BitOrAssign<U> for MutPtr<T>where
T: BitOrAssign<U> + 'static,
Source§fn bitor_assign(&mut self, rhs: U)
fn bitor_assign(&mut self, rhs: U)
|= operation. Read moreSource§impl<T, U> BitXorAssign<U> for MutPtr<T>where
T: BitXorAssign<U> + 'static,
impl<T, U> BitXorAssign<U> for MutPtr<T>where
T: BitXorAssign<U> + 'static,
Source§fn bitxor_assign(&mut self, rhs: U)
fn bitxor_assign(&mut self, rhs: U)
^= operation. Read moreSource§impl<'a, T, U> CastFrom<&'a mut CppBox<U>> for MutPtr<T>where
U: StaticUpcast<T> + CppDeletable,
impl<'a, T, U> CastFrom<&'a mut CppBox<U>> for MutPtr<T>where
U: StaticUpcast<T> + CppDeletable,
Source§impl<T> Deref for MutPtr<T>
Allows to call member functions of T and its base classes directly on the pointer.
impl<T> Deref for MutPtr<T>
Allows to call member functions of T and its base classes directly on the pointer.
Source§impl<T> DerefMut for MutPtr<T>
Allows to call member functions of T and its base classes directly on the pointer.
impl<T> DerefMut for MutPtr<T>
Allows to call member functions of T and its base classes directly on the pointer.
Source§impl<T, U> DivAssign<U> for MutPtr<T>where
T: DivAssign<U> + 'static,
impl<T, U> DivAssign<U> for MutPtr<T>where
T: DivAssign<U> + 'static,
Source§fn div_assign(&mut self, rhs: U)
fn div_assign(&mut self, rhs: U)
/= operation. Read moreSource§impl<T, T1, T2> IntoIterator for MutPtr<T>where
T: 'static,
&'static mut T: BeginMut<Output = CppBox<T1>> + EndMut<Output = CppBox<T2>>,
T1: CppDeletable + PartialEq<Ref<T2>> + 'static,
T2: CppDeletable,
&'static T1: Indirection,
&'static mut T1: Increment,
impl<T, T1, T2> IntoIterator for MutPtr<T>where
T: 'static,
&'static mut T: BeginMut<Output = CppBox<T1>> + EndMut<Output = CppBox<T2>>,
T1: CppDeletable + PartialEq<Ref<T2>> + 'static,
T2: CppDeletable,
&'static T1: Indirection,
&'static mut T1: Increment,
Source§type Item = <&'static T1 as Indirection>::Output
type Item = <&'static T1 as Indirection>::Output
Source§type IntoIter = CppIterator<T1, T2>
type IntoIter = CppIterator<T1, T2>
Source§impl<T, U> MulAssign<U> for MutPtr<T>where
T: MulAssign<U> + 'static,
impl<T, U> MulAssign<U> for MutPtr<T>where
T: MulAssign<U> + 'static,
Source§fn mul_assign(&mut self, rhs: U)
fn mul_assign(&mut self, rhs: U)
*= operation. Read moreSource§impl<T, U> PartialOrd<U> for MutPtr<T>
impl<T, U> PartialOrd<U> for MutPtr<T>
Source§impl<T, U> RemAssign<U> for MutPtr<T>where
T: RemAssign<U> + 'static,
impl<T, U> RemAssign<U> for MutPtr<T>where
T: RemAssign<U> + 'static,
Source§fn rem_assign(&mut self, rhs: U)
fn rem_assign(&mut self, rhs: U)
%= operation. Read moreSource§impl<T, U> ShlAssign<U> for MutPtr<T>where
T: ShlAssign<U> + 'static,
impl<T, U> ShlAssign<U> for MutPtr<T>where
T: ShlAssign<U> + 'static,
Source§fn shl_assign(&mut self, rhs: U)
fn shl_assign(&mut self, rhs: U)
<<= operation. Read moreSource§impl<T, U> ShrAssign<U> for MutPtr<T>where
T: ShrAssign<U> + 'static,
impl<T, U> ShrAssign<U> for MutPtr<T>where
T: ShrAssign<U> + 'static,
Source§fn shr_assign(&mut self, rhs: U)
fn shr_assign(&mut self, rhs: U)
>>= operation. Read moreSource§impl<T, U> SubAssign<U> for MutPtr<T>where
T: SubAssign<U> + 'static,
impl<T, U> SubAssign<U> for MutPtr<T>where
T: SubAssign<U> + 'static,
Source§fn sub_assign(&mut self, rhs: U)
fn sub_assign(&mut self, rhs: U)
-= operation. Read moreimpl<T> Copy for MutPtr<T>
Creates another pointer to the same object.