pub struct SimultaneousUnsafeAssignment;
Expand description

Assign several UnsafeField ‘simultaneously’.

There might be occasions where we cannot assign multiple fields simultaenously by reconstructing the struct (though this should be done in most cases). In this case, we can enforce a slightly lesser form of safety, by upholding invariants “only when the struct” is read from. This pattern guarantees that we cannot get a &self in between writes to fields.

unsafe_field::SimultaneousUnsafeAssignment
    .with(&mut foo.field_1, 5)
    .with(&mut foo.field_2, 10)
    .with(&mut foo.field_3, 15)
    .set_all();

Safety

  • ensure that all invariants are upheld after all assignments are complete
  • you must not rely on the ordering of the assignments, that is, the Unit state should be the same no matter the order of the assignments. This should be trivially verifiable, since I’m pretty sure it’s impossible. Just putting it in here in case someon can find a way of doing this.

Implementation Notes

This is possible to implement without storing references to the fields, but I don’t think it should matter in the Unit. This is probably optimised to the same thing? Not sure though. I don’t think it matters that much.

There’s probably some kind of way of doing this with pure functions that inlines functions more aggressively, as well.

Implementations§

Trait Implementations§

source§

impl SimultaneousUnsafeAssign for SimultaneousUnsafeAssignment

source§

unsafe fn set_all(self)

Complete all assignments that have been deferred ‘simultaneously’. This is not actually simultaneous, but ensures that all values are assigned, without the struct they are a part of being read in an invalid state Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.