https://github.com/mintlu8/mutify
Coerse `mut var: T` and `var: &mut T` into `&mut T`
https://github.com/mintlu8/mutify
Last synced: about 1 month ago
JSON representation
Coerse `mut var: T` and `var: &mut T` into `&mut T`
- Host: GitHub
- URL: https://github.com/mintlu8/mutify
- Owner: mintlu8
- License: apache-2.0
- Created: 2024-01-11T06:16:31.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-11T06:18:48.000Z (over 2 years ago)
- Last Synced: 2025-10-30T08:56:46.338Z (8 months ago)
- Language: Rust
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# Mutify
[](https://crates.io/crates/mutify)
[](https://docs.rs/mutify/latest/mutify/)
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.
```rust
let func = |v: &mut i32| *v += 1;
let mut b = 0;
let a = &mut b;
// `a` is not mutable.
func(&mut a);
```
## Example
```rust
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!