2 releases
0.1.1 | Jan 10, 2024 |
---|---|
0.1.0 | Jan 8, 2024 |
#27 in #closure
Used in heartless_tk
14KB
177 lines
This crate provides a proc macro to generate "let bindings" automatically,
usually cloning values into an expression(usually a closure). Inspired by
crate enclose
.
Syntax
bind!( ( comma_separated_list_of_var_bindings ) the_expr_that_uses_the_vars )
comma_separated_list_of_var_bindings
is in the form of
var_binding, another var_binding, ...
.
var_binding
is in the form of:
-
id
, generatinglet id = id.clone();
-
mut id
, generatinglet mut id = id.clone();
-
new_id = id
, generatinglet new_id = id.clone();
-
mut new_id = id
, generatinglet mut new_id = id.clone();
-
id = expr
, generatinglet id = expr;
-
mut id = expr
, generatinglet mut id = expr;
-
expr
, generatinglet the_only_id_in_the_expr = expr;
, e.g.bind!( (s.to_owned()) .. )
generateslet s = s.to_owned()
. -
mut expr
, generatinglet mut the_only_id_in_the_expr = expr;
e.g.bind!( (mut s.to_owned()) .. )
generateslet mut s = s.to_owned()
.
lib.rs
:
Why This Project
Sometimes we are forced to write some boring code like:
let foo2 = foo.clone();
let bar2 = *bar;
let baz2 = baz.to_owned();
let f = move |args| {
// access to foo2, bar2 and baz2
};
It's quite annoying, messing up the source code and the readers can't focus
on business logic. Some crates have been published to dealing with this,
and the bind crate is yet another one, inspired by crate enclose
, which
provides a convenient declarative macro. Since crate bind is a proc_macro
,
it can do more than macro_rules
.
Example
let f = bind!( ( foo,*bar,baz.to_owned() )
move |args| {
// access to foo, bar and baz
}
);
Dependencies
~235–680KB
~16K SLoC