pub struct MovingPtr<'a, T, A: IsAligned = Aligned>(/* private fields */);Expand description
A Box-like pointer for moving a value to a new memory location without needing to pass by
value.
Conceptually represents ownership of whatever data is being pointed to and will call its
Drop impl upon being dropped. This pointer is not responsible for freeing
the memory pointed to by this pointer as it may be pointing to an element in a Vec or
to a local in a function etc.
This type tries to act “borrow-like” which means that:
- Pointer should be considered exclusive and mutable. It cannot be cloned as this would lead to aliased mutability and potentially use after free bugs.
- It must always point to a valid value of whatever the pointee type is.
- The lifetime
'aaccurately represents how long the pointer is valid for. - It does not support pointer arithmetic in any way.
- If
AisAligned, the pointer must always be properly aligned for the typeT.
A value can be deconstructed into its fields via deconstruct_moving_ptr, see it’s documentation
for an example on how to use it.
Implementations§
Source§impl<'a, T> MovingPtr<'a, T, Aligned>
impl<'a, T> MovingPtr<'a, T, Aligned>
Sourcepub fn to_unaligned(self) -> MovingPtr<'a, T, Unaligned>
pub fn to_unaligned(self) -> MovingPtr<'a, T, Unaligned>
Removes the alignment requirement of this pointer
Sourcepub unsafe fn from_value(value: &'a mut MaybeUninit<T>) -> Self
pub unsafe fn from_value(value: &'a mut MaybeUninit<T>) -> Self
Creates a MovingPtr from a provided value of type T.
For a safer alternative, it is strongly advised to use move_as_ptr where possible.
§Safety
valuemust store a properly initialized value of typeT.- Once the returned
MovingPtrhas been used,valuemust be treated as it were uninitialized unless it was explicitly leaked viacore::mem::forget.
Source§impl<'a, T, A: IsAligned> MovingPtr<'a, T, A>
impl<'a, T, A: IsAligned> MovingPtr<'a, T, A>
Sourcepub unsafe fn new(inner: NonNull<T>) -> Self
pub unsafe fn new(inner: NonNull<T>) -> Self
Creates a new instance from a raw pointer.
For a safer alternative, it is strongly advised to use move_as_ptr where possible.
§Safety
innermust point to valid value ofT.- If the
Atype parameter isAlignedtheninnermust be be properly aligned forT. innermust have correct provenance to allow read and writes of the pointee type.- The lifetime
'amust be constrained such that thisMovingPtrwill stay valid and nothing else can read or mutate the pointee while thisMovingPtris live.
Sourcepub fn partial_move<R>(
self,
f: impl FnOnce(MovingPtr<'_, T, A>) -> R,
) -> (MovingPtr<'a, MaybeUninit<T>, A>, R)
pub fn partial_move<R>( self, f: impl FnOnce(MovingPtr<'_, T, A>) -> R, ) -> (MovingPtr<'a, MaybeUninit<T>, A>, R)
Partially moves out some fields inside of self.
The partially returned value is returned back pointing to MaybeUninit<T>.
While calling this function is safe, care must be taken with the returned MovingPtr as it
points to a value that may no longer be completely valid.
§Example
use core::mem::{offset_of, MaybeUninit, forget};
use bevy_ptr::{MovingPtr, move_as_ptr};
struct Parent {
field_a: FieldAType,
field_b: FieldBType,
field_c: FieldCType,
}
// Converts `parent` into a `MovingPtr`
move_as_ptr!(parent);
// SAFETY:
// - `field_a` and `field_b` are both unique.
let (partial_parent, ()) = MovingPtr::partial_move(parent, |parent_ptr| unsafe {
bevy_ptr::deconstruct_moving_ptr!({
let Parent { field_a, field_b, field_c } = parent_ptr;
});
insert(field_a);
insert(field_b);
forget(field_c);
});
// Move the rest of fields out of the parent.
// SAFETY:
// - `field_c` is by itself unique and does not conflict with the previous accesses
// inside `partial_move`.
unsafe {
bevy_ptr::deconstruct_moving_ptr!({
let MaybeUninit::<Parent> { field_a: _, field_b: _, field_c } = partial_parent;
});
insert(field_c);
}Sourcepub unsafe fn write_to(self, dst: *mut T)
pub unsafe fn write_to(self, dst: *mut T)
Writes the value pointed to by this pointer to a provided location.
This does not drop the value stored at dst and it’s the caller’s responsibility
to ensure that it’s properly dropped.
§Safety
dstmust be valid for writes.- If the
Atype parameter isAlignedthendstmust be properly aligned forT.
Sourcepub fn assign_to(self, dst: &mut T)
pub fn assign_to(self, dst: &mut T)
Writes the value pointed to by this pointer into dst.
The value previously stored at dst will be dropped.
Sourcepub unsafe fn move_field<U>(
&self,
f: impl Fn(*mut T) -> *mut U,
) -> MovingPtr<'a, U, A>
pub unsafe fn move_field<U>( &self, f: impl Fn(*mut T) -> *mut U, ) -> MovingPtr<'a, U, A>
Creates a MovingPtr for a specific field within self.
This function is explicitly made for deconstructive moves.
The correct byte_offset for a field can be obtained via core::mem::offset_of.
§Safety
fmust return a non-null pointer to a valid field insideT- If
AisAligned, thenTmust not berepr(packed) selfshould not be accessed or dropped as if it were a complete value after this function returns. Other fields that have not been moved out of may still be accessed or dropped separately.- This function cannot alias the field with any other access, including other calls to
move_fieldfor the same field, without first callingforgeton it first.
A result of the above invariants means that any operation that could cause self to be dropped while
the pointers to the fields are held will result in undefined behavior. This requires exctra caution
around code that may panic. See the example below for an example of how to safely use this function.
§Example
use core::mem::offset_of;
use bevy_ptr::{MovingPtr, move_as_ptr};
struct Parent {
field_a: FieldAType,
field_b: FieldBType,
field_c: FieldCType,
}
let parent = Parent {
field_a: FieldAType(0),
field_b: FieldBType(0),
field_c: FieldCType(0),
};
// Converts `parent` into a `MovingPtr`.
move_as_ptr!(parent);
unsafe {
let field_a = parent.move_field(|ptr| &raw mut (*ptr).field_a);
let field_b = parent.move_field(|ptr| &raw mut (*ptr).field_b);
let field_c = parent.move_field(|ptr| &raw mut (*ptr).field_c);
// Each call to insert may panic! Ensure that `parent_ptr` cannot be dropped before
// calling them!
core::mem::forget(parent);
insert(field_a);
insert(field_b);
insert(field_c);
}Source§impl<'a, T, A: IsAligned> MovingPtr<'a, MaybeUninit<T>, A>
impl<'a, T, A: IsAligned> MovingPtr<'a, MaybeUninit<T>, A>
Sourcepub unsafe fn move_maybe_uninit_field<U>(
&self,
f: impl Fn(*mut T) -> *mut U,
) -> MovingPtr<'a, MaybeUninit<U>, A>
pub unsafe fn move_maybe_uninit_field<U>( &self, f: impl Fn(*mut T) -> *mut U, ) -> MovingPtr<'a, MaybeUninit<U>, A>
Creates a MovingPtr for a specific field within self.
This function is explicitly made for deconstructive moves.
The correct byte_offset for a field can be obtained via core::mem::offset_of.
§Safety
fmust return a non-null pointer to a valid field insideT- If
AisAligned, thenTmust not berepr(packed) selfshould not be accessed or dropped as if it were a complete value after this function returns. Other fields that have not been moved out of may still be accessed or dropped separately.- This function cannot alias the field with any other access, including other calls to
move_fieldfor the same field, without first callingforgeton it first.
Source§impl<'a, T, A: IsAligned> MovingPtr<'a, MaybeUninit<T>, A>
impl<'a, T, A: IsAligned> MovingPtr<'a, MaybeUninit<T>, A>
Sourcepub unsafe fn assume_init(self) -> MovingPtr<'a, T, A>
pub unsafe fn assume_init(self) -> MovingPtr<'a, T, A>
Creates a MovingPtr pointing to a valid instance of T.
See also: MaybeUninit::assume_init.
§Safety
It’s up to the caller to ensure that the value pointed to by self
is really in an initialized state. Calling this when the content is not yet
fully initialized causes immediate undefined behavior.