mutify 0.1.0

Macro for coercing a `mut var: T` or `var: &mut T` into a `&mut T`.
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented2 out of 2 items with examples
  • Size
  • Source code size: 16.13 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.06 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • mintlu8/mutify
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • mintlu8

Mutify

Crates.io Docs

Macro for coercing a mut var: T or var: &mut T into a &mut T.

Why

A naive apporach would be putting a &mut before the expression, however this doesn't work.

let func = |v: &mut i32| *v += 1;
let mut b = 0;
let a = &mut b;
// `a` is not mutable.
func(&mut a);

Example

fn plus_one(n: &mut i32) {
    *n += 1;
}

let mut a = 3;
plus_one(mutify!(a));
assert_eq!(a, 4);

let b = &mut a;
plus_one(mutify!(b));
assert_eq!(a, 5);

Note

A magic function called __coerce_mut is used here, don't name your functions that and you are good!