Giter VIP home page Giter VIP logo

capture.rs's Introduction

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

capture.rs's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

alekratz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.