#macro #attributes #hack #assignment #compound #first #assign

rhs_first_assign

用于黑客复合赋值的属性宏

1 个不稳定版本

0.1.0 2020年2月17日

#5#assign

MIT/Apache

8KB
53 代码行

rhs_first_assign

CI codecov dependency status Crates.io License

用于黑客复合赋值的属性宏。

动机

use std::num::Wrapping;

fn main() {
    let mut xs = vec![Wrapping(1), Wrapping(2)];

    // OK
    xs[1] = xs[0] + xs[1];

    // Error
    xs[1] += xs[0];
}
error[E0502]: cannot borrow `xs` as immutable because it is also borrowed as mutable
  --> src/main.rs:10:14
   |
10 |     xs[1] += xs[0];
   |     ---------^^---
   |     |        |
   |     |        immutable borrow occurs here
   |     mutable borrow occurs here
   |     mutable borrow later used here

用法

use rhs_first_assign::rhs_first_assign;

use std::num::Wrapping;

#[rhs_first_assign]
fn main() {
    let mut xs = vec![Wrapping(1), Wrapping(2)];

    xs[1] = xs[0] + xs[1];

    xs[1] += xs[0];
}

use std::num::Wrapping;

fn main() {
    let mut xs = vec![Wrapping(1), Wrapping(2)];

    xs[1] = xs[0] + xs[1];

    {
        let __rhs_first_assign_rhs_l11_c10 = xs[0];
        xs[1] += __rhs_first_assign_rhs_l11_c10;
    };
}

许可证

根据 MIT OR Apache-2.0 许可。

依赖

~1.5MB
~34K SLoC