Skip to main content

Mut

Struct Mut 

Source
pub struct Mut<T>(/* private fields */);
Expand description

A Clone-once wrapper.

Mut allows passing types like &mut T safely. Mut implements a dummy Clone that allows being cloned exactly once. This allows passing non-Clone types by moving them between Muts. Mut can be thought of a bit like Option. Mut contains a value and when it is Cloned the value inside is moved to the new Mut leaving None in the old Mut. This approach has the disadvantage that the uniqueness check becomes a runtime panic.

§Panics

Mut will panic in any of the following situations:

  1. if it is Clone::cloned more than once
  2. if the inner value is accessed after a Clone

§Example

fn f(mut y: Mut<&mut i32>, x: i32) {
    **y += 2 * x;
}

let x: i32 = 42;
let mut y: i32 = 31;

magic_args::apply(f, (x, Mut::from(&mut y)));
assert_eq!(y, 115);

Implementations§

Source§

impl<T> Mut<T>

Source

pub fn into_inner(self) -> T

Destruct this Mut and get the value inside.

§Panics

See: type-level panic docs.

Trait Implementations§

Source§

impl<T> AsMut<T> for Mut<T>

Source§

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

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T> AsMut<T> for Mut<&mut T>

Source§

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

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T> AsMut<T> for Mut<&mut &mut T>

Source§

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

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T> AsMut<T> for Mut<&mut &mut &mut T>

Source§

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

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T> AsRef<T> for Mut<T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> AsRef<T> for Mut<&mut T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> AsRef<T> for Mut<&mut &mut T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> AsRef<T> for Mut<&mut &mut &mut T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Borrow<T> for Mut<T>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> Borrow<T> for Mut<&mut T>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> Borrow<T> for Mut<&mut &mut T>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> Borrow<T> for Mut<&mut &mut &mut T>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for Mut<T>

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for Mut<&mut T>

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for Mut<&mut &mut T>

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for Mut<&mut &mut &mut T>

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> Clone for Mut<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Deref for Mut<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T> DerefMut for Mut<T>

Source§

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

Mutably dereferences the value.
Source§

impl<T> From<T> for Mut<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> !Freeze for Mut<T>

§

impl<T> !RefUnwindSafe for Mut<T>

§

impl<T> !Sync for Mut<T>

§

impl<T> Send for Mut<T>
where Cell<Option<T>>: Send,

§

impl<T> Unpin for Mut<T>
where Cell<Option<T>>: Unpin,

§

impl<T> UnsafeUnpin for Mut<T>
where Cell<Option<T>>: UnsafeUnpin,

§

impl<T> UnwindSafe for Mut<T>
where Cell<Option<T>>: UnwindSafe,

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<U> MagicArgs for U

Source§

fn apply<C, T>(self, f: C) -> <C as Callable<U, T>>::Output
where C: Callable<U, T>, U: Sized,

Apply f on self. 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.