2个版本
| 0.1.1 | 2024年1月10日 |
|---|---|
| 0.1.0 | 2024年1月8日 |
#13 in #closure
用于 heartless_tk
14KB
177 行
此crate提供了一个过程宏,用于自动生成“let绑定”,通常将值克隆到表达式(通常是闭包)中。受crate enclose的启发。
语法
bind!( (逗号分隔的变量绑定列表)使用变量的表达式)
comma_separated_list_of_var_bindings的形式为var_binding, another var_binding, ...。
var_binding的形式为
-
id,生成let id = id.clone(); -
mut id,生成let mut id = id.clone(); -
new_id = id,生成let new_id = id.clone(); -
mut new_id = id,生成let mut new_id = id.clone(); -
id = expr,生成let id = expr; -
mut id = expr,生成let mut id = expr; -
expr,生成let the_only_id_in_the_expr = expr;,例如bind!( (s.to_owned()) .. )生成let s = s.to_owned()。 -
mut expr,生成let mut the_only_id_in_the_expr = expr;例如bind!( (mut s.to_owned()) .. )生成let mut s = s.to_owned()。
lib.rs:
为什么这个项目
有时我们被迫编写一些无聊的代码,比如
let foo2 = foo.clone();
let bar2 = *bar;
let baz2 = baz.to_owned();
let f = move |args| {
// access to foo2, bar2 and baz2
};
这相当令人讨厌,搞乱了源代码,读者无法专注于业务逻辑。已经发布了一些crate来处理这个问题,bind crate 就是其中之一,灵感来自 crate enclose,它提供了一个方便的声明式宏。由于crate bind 是一个 proc_macro,它可以做比 macro_rules 更多的事情。
示例
let f = bind!( ( foo,*bar,baz.to_owned() )
move |args| {
// access to foo, bar and baz
}
);
依赖项
~285–740KB
~18K SLoC