Coder Social home page Coder Social logo

closure's Introduction

Looking For Maintainers

This crate is no longer maintained. Feel free to contact the author if you'd like to continue developing it.

closure! - A macro for individually capturing variables

Latest version Documentation License

This crate provides a macro which lets you write closures that can capture individually either by moving, referencing, mutably referencing of cloning.

Usage

Start by adding an entry to your Cargo.toml:

[dependencies]
closure = "0.3.0"

Then you can write closures like so:

use closure::closure;

let string = "move".to_string();
let x = 10;
let mut y = 20;
let rc = Rc::new(5);

let closure = closure!(move string, ref x, ref mut y, clone rc, |arg: i32| {
    ...
});

closure's People

Contributors

oliver-giersch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

3gx technic

closure's Issues

Suggestion: generalize clone capture mode

The clone capture mode can be easily generalized, like in this old unmaintained crate:

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.

It would be useful because it would enable things like to_owned, to_string, to_vec etc., in addition to clone.
I think it would just require changing this:

closure/src/lib.rs

Lines 192 to 196 in 92fa655

// Capture by cloning
(@inner clone $var:ident $($tail:tt)*) => {
let $var = $var.clone();
closure!(@inner $($tail)*)
};

to this:

    // Capture by .$method()
    (@inner $method:ident $var:ident $($tail:tt)*) => {
        let $var = $var.$method();
        closure!(@inner $($tail)*)
    };

New capture syntax with better IDE support

Hi,
currently the body of the closure is captured by $($tail:tt)*, i.e. as tokens. This doesn't play well with IDE (e.g. rust-analyzer).
If we capture the body as $expr or $block then IDE knows all types and able to autocomplete.
It is possible to parse captures list with the $tt rule and avoid conflict with the rest of the macro if it is enclosed in the delimiters ([], () or {}).

So the new syntax will be

closure!([ref a, ref mut b] |x| {
   *b += 10;
    x + a
})

You can see that it works here
image

I can make a PR for it, but we need to decide how to introduce this breaking change. As a fork, as a new major version, as a new macro with alternative name?

Suggestion: Support capturing for members

Currently, this doesn't work: closure!(clone self.foo, || { ... })
It would be very useful to support this (for all capturing modes)!

(Also in general, not just for self, but for any bar.foo and also bar.baz.foo (any nesting level), using the last path segment as the new name, i.e. clone bar.baz.foo expanding to let foo = bar.baz.foo.clone();.)

Support weak references

Types that should be downgraded into a weak reference, and then the contents should not be executed if the reference cannot be upgraded back to a strong reference (but providing a way to also handle the case where such a weak reference is optional).

Feature: Add Support for Async Blocks

Async blocks suffer from similar problems as closures. I have some code similar to the following (the foo function here emulates a library-supplied function)

use core::future::Future;

async fn foo(f: impl Future<Output = ()> + 'static) {
    // stuff
}

fn main() {
    let no_copy = "abcdef".to_string();

    // error[E0373]: async block may outlive the current function,
    // but it borrows `no_copy`, which is owned by the current function
    foo(async {
        println!("{}", no_copy);
    });
}

The solution is to clone no_copy, like you would for a closure. I would like the macro to support this usecase, something like:

closure!(clone no_copy, async {
    ...
});

Is this possible?

Thanks

Suggestion: Still allow `move` keyword before closure

Usually the way I use this macro is: I already have a move |..| {..} closure and then start using this macro for it, and often forget to remove the move keyword, leading to frustration & a wasted compile-cycle.
I would prefer if it would still accept it, or even require it (like the crate I was previously using that was abandoned).
With the current situation that requires me to remove move, it's often requires more typing / cursor-moving than manually writing the code to clone the variables that it should capture..
Also the move keyword still serves as important reminder that the closure is actually a move closure, so also for that reason I'd prefer to keep it!
Could you please allow it? :)

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.