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:
- if it is
Clone::cloned more than once - 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>
impl<T> Mut<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Trait Implementations§
Source§impl<T> BorrowMut<T> for Mut<T>
impl<T> BorrowMut<T> for Mut<T>
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> BorrowMut<T> for Mut<&mut T>
impl<T> BorrowMut<T> for Mut<&mut T>
Source§fn borrow_mut(&mut self) -> &mut T
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>
impl<T> BorrowMut<T> for Mut<&mut &mut T>
Source§fn borrow_mut(&mut self) -> &mut T
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>
impl<T> BorrowMut<T> for Mut<&mut &mut &mut T>
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
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>
impl<T> Unpin for Mut<T>
impl<T> UnsafeUnpin for Mut<T>
impl<T> UnwindSafe for Mut<T>
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
Mutably borrows from an owned value. Read more