| Crates.io | capture |
| lib.rs | capture |
| version | 0.1.0 |
| created_at | 2014-11-28 13:43:40.390167+00 |
| updated_at | 2015-12-11 23:57:49.028558+00 |
| description | A macro for adding explicit capture clauses to a (closure-) expression. |
| homepage | |
| repository | https://github.com/Kimundi/capture.rs |
| max_upload_size | |
| id | 418 |
| size | 6,657 |
A macro for adding explicit capture clauses to a (closure-) expression.
Using this macro, it becomes possible to be explicit about what variables a closure captures, and by which capture mode it does so.
capture!($([move IDENT,]
[ref IDENT,]
[IDENT IDENT,])*
in EXPR)
The macro will expand to nested block expressions with a let binding per capture clause:
move x clause rebinds a name by itself, which is effectively a no-op.ref x clause rebinds a name by a shared reference to it.ref mut x clause rebinds a name by a mutable reference to it.IDENT x clause rebinds a name to the result of calling .IDENT() method on it.
This can for example be used to capture by clone, which would look like this: clone x.These bindings will be in scope for the final EXPR expression, which will usually be a
by-value capturing closure.
Using the macro:
#![feature(phase)]
#![feature(unboxed_closures)]
#[phase(plugin)]
extern crate capture;
fn main() {
let (x, y, z) = (1u32, 2u32, 3u32);
let g = capture!(move x, ref y, clone z in move |:| x + *y + z);
assert_eq!(g(), 6);
}