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>
impl<'a, T: TempReprMutChk> TempInstMut<'a, T>
Sourcepub unsafe fn new(obj: T::Mutable<'a>) -> Self
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:
TempInstPin::new
if a pinned mutable reference toT
is sufficient.TempInstMut::call_with
if the use of the mutableT
reference can be confined to a closure.
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 ofTempInstMut
is called whenever the returned instance goes out of scope. (In particular, the instance must not be passed tocore::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 theTempInstMut
instance. When the instance goes out of scope after passing it tocore::mem::swap
, the otherTempInstMut
instance that it was swapped with can become dangling. Note that passing the result ofSelf::deref_mut
tocore::mem::swap
is not unsafe, however.) 'a
is'static
. (ATempInstMut
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)
.
Sourcepub fn call_with<R>(obj: T::Mutable<'a>, f: impl FnOnce(&mut T) -> R) -> R
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>
impl<'a, T: Clone + TempReprMutChk + 'a> Clone for TempInstMut<'a, T>
Source§fn clone(&self) -> TempInstMut<'a, T>
fn clone(&self) -> TempInstMut<'a, T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T: TempReprMutChk + Debug> Debug for TempInstMut<'_, T>
impl<T: TempReprMutChk + Debug> Debug for TempInstMut<'_, T>
Source§impl<T: TempReprMutChk<Mutable<'static>: Default>> Default for TempInstMut<'static, T>
impl<T: TempReprMutChk<Mutable<'static>: Default>> Default for TempInstMut<'static, T>
Source§impl<T: TempReprMutChk> Deref for TempInstMut<'_, T>
impl<T: TempReprMutChk> Deref for TempInstMut<'_, T>
Source§impl<T: TempReprMutChk> DerefMut for TempInstMut<'_, T>
impl<T: TempReprMutChk> DerefMut for TempInstMut<'_, T>
Source§impl<T: TempReprMutChk + Display> Display for TempInstMut<'_, T>
impl<T: TempReprMutChk + Display> Display for TempInstMut<'_, T>
Source§impl<T: TempReprMutChk> Drop for TempInstMut<'_, T>
impl<T: TempReprMutChk> Drop for TempInstMut<'_, T>
Source§impl<'a, T: Hash + TempReprMutChk + 'a> Hash for TempInstMut<'a, T>
impl<'a, T: Hash + TempReprMutChk + 'a> Hash for TempInstMut<'a, T>
Source§impl<'a, T: Ord + TempReprMutChk + 'a> Ord for TempInstMut<'a, T>
impl<'a, T: Ord + TempReprMutChk + 'a> Ord for TempInstMut<'a, T>
Source§fn cmp(&self, other: &TempInstMut<'a, T>) -> Ordering
fn cmp(&self, other: &TempInstMut<'a, T>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<'a, T: PartialEq + TempReprMutChk + 'a> PartialEq for TempInstMut<'a, T>
impl<'a, T: PartialEq + TempReprMutChk + 'a> PartialEq for TempInstMut<'a, T>
Source§impl<'a, T: PartialOrd + TempReprMutChk + 'a> PartialOrd for TempInstMut<'a, T>
impl<'a, T: PartialOrd + TempReprMutChk + 'a> PartialOrd for TempInstMut<'a, T>
impl<'a, T: Eq + TempReprMutChk + 'a> Eq for TempInstMut<'a, T>
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>where
T: RefUnwindSafe,
<T as TempReprMutChk>::SwapChkData: RefUnwindSafe,
<T as TempReprMut>::Mutable<'a>: RefUnwindSafe,
impl<'a, T> Send for TempInstMut<'a, T>
impl<'a, T> Sync for TempInstMut<'a, T>
impl<'a, T> Unpin for TempInstMut<'a, T>
impl<'a, T> UnwindSafe for TempInstMut<'a, T>where
T: UnwindSafe,
<T as TempReprMutChk>::SwapChkData: UnwindSafe,
<T as TempReprMut>::Mutable<'a>: UnwindSafe,
Blanket Implementations§
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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