1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
//! Scala's Placeholder Syntax for Rust. //! //! ``` //! # let xs = vec![1, 2, 3, 4, 5]; //! use placeholder_closure::λ; //! //! let xs = xs.into_iter().map(λ!($ + 1)).collect::<Vec<_>>(); //! ``` //! //! # How it works //! //! The [`λ!`] (or [`lambda!`]) macro replaces 0 or more `$` characters with closure arguments. //! //! ``` //! # use placeholder_closure::λ; //! let f = λ!($ + 1); //! # let _: fn(i32) -> i32 = f; //! ``` //! //! will be: //! //! ``` //! # use placeholder_closure::λ; //! let f = |__0| __0 + 1; //! # let _: fn(i32) -> i32 = f; //! ``` //! //! ## Constructing `move` closures //! //! You can also construct `move` closures. //! //! ``` //! # use placeholder_closure::λ; //! fn dot<F: FnOnce(Y) -> Z, G: FnOnce(X) -> Y, X, Y, Z>(f: F, g: G) -> impl FnOnce(X) -> Z { //! λ!(move { f(g($)) }) //! } //! ``` //! //! [`λ!`]: ./macro.λ.html //! [`lambda!`]: ./macro.lambda.html #![forbid(unsafe_code)] #![warn(rust_2018_idioms, missing_docs)] #[allow(unused_extern_crates)] extern crate proc_macro; mod lambda; /// An alias for [`λ!`]. /// /// [`λ!`]: ./macro.λ.html #[proc_macro] pub fn lambda(input: proc_macro::TokenStream) -> proc_macro::TokenStream { lambda::lambda(input.into()).into() } /// Scala's Placeholder Syntax for Rust. /// /// Available since Rust 1.53. /// /// See the [crate level documentation] for details. /// /// [crate level documentation]: ./index.html #[rustversion::since(1.53)] #[proc_macro] pub fn λ(input: proc_macro::TokenStream) -> proc_macro::TokenStream { lambda::lambda(input.into()).into() }