Struct TempInstMut

Source
pub struct TempInstMut<'a, T: TempReprMutChk + 'a>(/* private fields */);
Expand description

A wrapper around an instance of T which implements TempReprMut, i.e. is a temporary representation of a type T::Mutable<'a>.

TempInstMut itself has a lifetime parameter 'a and borrows the object passed to TempInstMut::new for that lifetime; therefore it is logically equivalent to T::Mutable<'a>. However, it can hand out references to the lifetime-less type T via its DerefMut implementation.

Implementations§

Source§

impl<'a, T: TempReprMutChk> TempInstMut<'a, T>

Source

pub unsafe fn new(obj: T::Mutable<'a>) -> Self

Creates a TempInstMut from an instance of T::Mutable.

A mutable reference to T can be obtained from the result via DerefMut.

As this method is unsafe, using one of the safe alternatives is strongly recommended:

TempInstMut::new potentially has a slight overhead compared to TempInstPin::new, in terms of both time and space, though there is a good chance that the compiler will optimize both away if it can analyze how the instance is used.

§Safety

The caller must ensure at least one of the following two conditions.

  • The Drop implementation of TempInstMut is called whenever the returned instance goes out of scope. (In particular, the instance must not be passed to core::mem::forget.)
  • The state of the instance when it goes out of scope is the same as when it was created. (This condition can be violated by calling core::mem::swap or a related function on the TempInstMut instance. When the instance goes out of scope after passing it to core::mem::swap, the other TempInstMut instance that it was swapped with can become dangling. Note that passing the result of Self::deref_mut to core::mem::swap is not unsafe, however.)
  • 'a is 'static. (A TempInstMut instance with static lifetimes cannot become dangling.)
§Panics

The Drop implementation of the returned instance calls std::process::abort (after calling the standard panic handler) if the instance has been modified, which is not possible via its API but can be achieved by swapping it with another instance, e.g. using core::mem::swap.

Unfortunately, a regular panic is insufficient in this case because it can be caught with std::panic::catch_unwind, and a dangling TempInstMut reference can then be obtained from the closure passed to std::panic::catch_unwind (safely, because unfortunately std::panic::UnwindSafe is not an unsafe trait – why?!!).

The panic behavior can be changed via set_modification_panic_fn.

§Remarks

For many types, including TempRef, T::Mutable is actually the same as T::Shared. This can be useful when combining mutable and shared references in a tuple. E.g. T = (TempRefMut<U>, TempRef<V>) represents (&mut U, &V), and this is preserved by TempInstMut::new, whereas TempInst::new treats it as (&U, &V).

Source

pub fn call_with<R>(obj: T::Mutable<'a>, f: impl FnOnce(&mut T) -> R) -> R

Calls f with a mutable reference to T constructed from obj, and returns the value returned by f.

This method is a simple (but safe) wrapper around Self::new, which potentially has a slight overhead. If possible, use TempInstPin::new (or TempInstPin::call_with) instead.

§Panics

Calls std::process::abort if f modifies the internal state of the object that was passed to it. See Self::new for more information.

Trait Implementations§

Source§

impl<'a, T: Clone + TempReprMutChk + 'a> Clone for TempInstMut<'a, T>
where T::SwapChkData: Clone, T::Mutable<'a>: Clone,

Source§

fn clone(&self) -> TempInstMut<'a, T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: TempReprMutChk + Debug> Debug for TempInstMut<'_, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: TempReprMutChk<Mutable<'static>: Default>> Default for TempInstMut<'static, T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: TempReprMutChk> Deref for TempInstMut<'_, T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: TempReprMutChk> DerefMut for TempInstMut<'_, T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: TempReprMutChk + Display> Display for TempInstMut<'_, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: TempReprMutChk> Drop for TempInstMut<'_, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, T: Hash + TempReprMutChk + 'a> Hash for TempInstMut<'a, T>
where T::SwapChkData: Hash, T::Mutable<'a>: Hash,

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T: Ord + TempReprMutChk + 'a> Ord for TempInstMut<'a, T>
where T::SwapChkData: Ord, T::Mutable<'a>: Ord,

Source§

fn cmp(&self, other: &TempInstMut<'a, T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a, T: PartialEq + TempReprMutChk + 'a> PartialEq for TempInstMut<'a, T>

Source§

fn eq(&self, other: &TempInstMut<'a, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T: PartialOrd + TempReprMutChk + 'a> PartialOrd for TempInstMut<'a, T>

Source§

fn partial_cmp(&self, other: &TempInstMut<'a, T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, T: Eq + TempReprMutChk + 'a> Eq for TempInstMut<'a, T>
where T::SwapChkData: Eq, T::Mutable<'a>: Eq,

Source§

impl<'a, T: TempReprMutChk + 'a> StructuralPartialEq for TempInstMut<'a, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for TempInstMut<'a, T>

§

impl<'a, T> RefUnwindSafe for TempInstMut<'a, T>

§

impl<'a, T> Send for TempInstMut<'a, T>
where T: Send, <T as TempReprMutChk>::SwapChkData: Send, <T as TempReprMut>::Mutable<'a>: Send,

§

impl<'a, T> Sync for TempInstMut<'a, T>
where T: Sync, <T as TempReprMutChk>::SwapChkData: Sync, <T as TempReprMut>::Mutable<'a>: Sync,

§

impl<'a, T> Unpin for TempInstMut<'a, T>
where T: Unpin, <T as TempReprMutChk>::SwapChkData: Unpin, <T as TempReprMut>::Mutable<'a>: Unpin,

§

impl<'a, T> UnwindSafe for TempInstMut<'a, T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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>,

Source§

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.