Struct MovingPtr
pub struct MovingPtr<'a, T, A = Aligned>(/* private fields */)
where
A: IsAligned;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§
§impl<'a, T> MovingPtr<'a, T>
impl<'a, T> MovingPtr<'a, T>
pub fn to_unaligned(self) -> MovingPtr<'a, T, Unaligned>
pub fn to_unaligned(self) -> MovingPtr<'a, T, Unaligned>
Removes the alignment requirement of this pointer
pub unsafe fn from_value(value: &'a mut MaybeUninit<T>) -> MovingPtr<'a, T>
pub unsafe fn from_value(value: &'a mut MaybeUninit<T>) -> MovingPtr<'a, T>
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.
§impl<'a, T, A> MovingPtr<'a, T, A>where
A: IsAligned,
impl<'a, T, A> MovingPtr<'a, T, A>where
A: IsAligned,
pub unsafe fn new(inner: NonNull<T>) -> MovingPtr<'a, T, A>
pub unsafe fn new(inner: NonNull<T>) -> MovingPtr<'a, T, A>
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 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.
pub 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);
}pub fn read(self) -> T
pub fn read(self) -> T
Reads the value pointed to by this pointer.
pub 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.
pub 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.
pub 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 extra 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);
}§impl<'a, T, A> MovingPtr<'a, MaybeUninit<T>, A>where
A: IsAligned,
impl<'a, T, A> MovingPtr<'a, MaybeUninit<T>, A>where
A: IsAligned,
pub 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.
§impl<'a, T, A> MovingPtr<'a, MaybeUninit<T>, A>where
A: IsAligned,
impl<'a, T, A> MovingPtr<'a, MaybeUninit<T>, A>where
A: IsAligned,
pub 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.
Trait Implementations§
Auto Trait Implementations§
impl<'a, T, A> Freeze for MovingPtr<'a, T, A>
impl<'a, T, A> RefUnwindSafe for MovingPtr<'a, T, A>where
A: RefUnwindSafe,
T: RefUnwindSafe,
impl<'a, T, A = Aligned> !Send for MovingPtr<'a, T, A>
impl<'a, T, A = Aligned> !Sync for MovingPtr<'a, T, A>
impl<'a, T, A> Unpin for MovingPtr<'a, T, A>where
A: Unpin,
impl<'a, T, A> UnsafeUnpin for MovingPtr<'a, T, A>
impl<'a, T, A = Aligned> !UnwindSafe for MovingPtr<'a, T, A>
Blanket Implementations§
§impl<R> TryRngCore for Rwhere
R: TryRng,
impl<R> TryRngCore for Rwhere
R: TryRng,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
§fn into_result(self) -> Result<T, RunSystemError>
fn into_result(self) -> Result<T, RunSystemError>
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<R> RngExt for R
impl<R> RngExt for R
§fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
StandardUniform distribution. Read more§fn random_iter<T>(self) -> Iter<StandardUniform, Self, T> ⓘ
fn random_iter<T>(self) -> Iter<StandardUniform, Self, T> ⓘ
§fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
§fn random_bool(&mut self, p: f64) -> bool
fn random_bool(&mut self, p: f64) -> bool
p of being true. Read more§fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
numerator/denominator of being
true. Read more§fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
§fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T> ⓘwhere
D: Distribution<T>,
Self: Sized,
fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T> ⓘwhere
D: Distribution<T>,
Self: Sized,
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.