GitXplorerGitXplorer
K

capture.rs

public
8 stars
1 forks
0 issues

Commits

List of commits on branch master.
Unverified
01a319700633b3526f07ea5f0184cfc682e146c3

Fixed travis config

KKimundi committed 10 years ago
Unverified
2faf8b5030568b9b8bf03802d83a40383747fcc5

Fixed travis config

KKimundi committed 10 years ago
Unverified
dc5d7d74b5f15a3f4c8fc0f61554ed6dbdae023f

Added travis config

KKimundi committed 10 years ago
Unverified
796bb16a81dc449a1dd0f1c279b08ddd2020f11c

Initial fixups

KKimundi committed 10 years ago
Unverified
7a9519c3494bd091f29759c17d3e4d25e54d83e5

initial commit

KKimundi committed 10 years ago

README

The README file for this repository.

capture.rs

Travis-CI Status

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.

Syntax

capture!($([move IDENT,]
           [ref IDENT,]
           [IDENT IDENT,])*
         in EXPR)

Semantic

The macro will expand to nested block expressions with a let binding per capture clause:

  • The move x clause rebinds a name by itself, which is effectively a no-op.
  • The ref x clause rebinds a name by a shared reference to it.
  • The ref mut x clause rebinds a name by a mutable reference to it.
  • The 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.

Current limitations and future changes

  • The macro syntax makes it somewhat verbose. A future version of this package might make use of expression-attributes instead to become more lightweight.
  • The macro might become more specialized to always produce a by-value capturing closure, rather than allowing arbitrary expressions.

Example

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);
}