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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
//! Serializable and debuggable closures. //! //! **[Crates.io](https://crates.io/crates/serde_closure) │ //! [Repo](https://github.com/alecmocatta/serde_closure)** //! //! This library provides macros that wrap closures to make them serializable //! and debuggable. //! //! ``` //! # use serde_closure::Fn; //! let one = 1; //! let plus_one = Fn!(|x: i32| x + one); //! //! assert_eq!(2, plus_one(1)); //! println!("{:#?}", plus_one); //! //! // prints: //! // Fn<main::{{closure}} at main.rs:6:15> { //! // one: 1, //! // source: "| x : i32 | x + one", //! // } //! ``` //! //! This library aims to work in as simple and safe a way as possible. It //! currently requires nightly Rust for the `unboxed_closures` and `fn_traits` //! features (rust issue //! [#29625](https://github.com/rust-lang/rust/issues/29625)). //! //! * There are three macros, [`FnOnce`](macro@FnOnce), [`FnMut`](macro@FnMut) //! and [`Fn`](macro@Fn), corresponding to the three types of Rust closure. //! * Wrap your closure with one of the macros and it will now implement //! `Copy`, `Clone`, `PartialEq`, `Eq`, `Hash`, `PartialOrd`, `Ord`, //! `Serialize`, `Deserialize` and `Debug`. //! * There are some minor syntax limitations, which are documented below. //! * This crate has one unavoidable but documented and sound usage of //! `unsafe`. //! //! # Examples of wrapped closures //! **Inferred, non-capturing closure:** //! ``` //! # #[macro_use] extern crate serde_closure; //! # ( //! |a| a+1 //! # )(0i32); //! ``` //! ``` //! # #[macro_use] extern crate serde_closure; //! # ( //! FnMut!(|a| a+1) //! # )(0i32); //! ``` //! //! **Annotated, non-capturing closure:** //! ``` //! # #[macro_use] extern crate serde_closure; //! # ( //! |a: String| -> String { a.to_uppercase() } //! # )(String::from("abc")); //! ``` //! ``` //! # #[macro_use] extern crate serde_closure; //! # ( //! FnMut!(|a: String| -> String { a.to_uppercase() }) //! # )(String::from("abc")); //! ``` //! //! **Inferred closure, capturing `num`:** //! ``` //! # #[macro_use] extern crate serde_closure; //! let mut num = 0; //! # ( //! |a| num += a //! # )(1i32); //! ``` //! ``` //! # #[macro_use] extern crate serde_closure; //! let mut num = 0; //! # ( //! FnMut!(|a| num += a) //! # )(1i32); //! ``` //! //! **`move` closure, capturing `hello` and `world`:** //! ``` //! # #[macro_use] extern crate serde_closure; //! let hello = String::from("hello"); //! let mut world = String::new(); //! # ( //! move |name| { //! world += (hello.to_uppercase() + name).as_str(); //! } //! # )("abc"); //! ``` //! ``` //! # #[macro_use] extern crate serde_closure; //! let hello = String::from("hello"); //! let mut world = String::new(); //! # ( //! FnMut!(move |name| { //! world += (hello.to_uppercase() + name).as_str(); //! }) //! # )("abc"); //! ``` //! //! # Limitations //! There are currently some minor limitations: //! * Captured variables with an uppercase first letter need to be explicitly //! captured. If you see a panic like the following, fix the case of the //! variable. //! ```text //! thread 'main' panicked at 'A variable with an upper case first letter was implicitly captured. //! Unfortunately due to current limitations it must be captured explicitly. //! Please refer to the README.', tests/test.rs:205:10 //! note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace. //! ``` //! * Functions called inside the closure might need to be disambiguated. This //! also affects enum unit and tuple variants with a lowercase first letter. //! If you see an error like either of the following, qualify `my_function` //! as `self::my_function` and `my_enum_variant` as //! `MyEnum::my_enum_variant`. //! ```text //! error[E0277]: the trait bound `fn(usize) -> std::option::Option<usize> {my_function::<usize>}: fnref::_IMPL_DESERIALIZE_FOR_Fn::_serde::Serialize` is not satisfied //! --> tests/test.rs:327:10 //! | //! 314 | fn unfold<A, St, F>(initial_state: St, f: F) -> Unfold<St, F> //! | ------ //! 315 | where //! 316 | F: Fn(&mut St) -> Option<A> + Serialize, //! | --------- required by this bound in `fnref::unfold` //! ... //! 327 | let _ = unfold(0_usize, Fn!(|acc: &mut _| my_function(*acc))); //! | ^^^^^^ the trait `fnref::_IMPL_DESERIALIZE_FOR_Fn::_serde::Serialize` is not implemented for `fn(usize) -> std::option::Option<usize> {my_function::<usize>}` //! ``` //! ```text //! error[E0530]: function parameters cannot shadow tuple variants //! --> tests/test.rs:173:47 //! | //! 173 | FnMut!(|acc: &mut _| my_enum_variant(*acc)) //! | ---------------------^^^^^^^^^^^^^^^------- //! | | | //! | | cannot be named the same as a tuple variant //! | in this macro invocation //! ``` //! //! # Serializing between processes //! //! Closures created by this crate are unnameable – i.e. just like normal //! closures, there is no Rust syntax available with which to write the type. //! What this means is that to deserialize a closure, you either need to specify //! the precise type you're deserializing without naming it (which is possible //! but not particularly practical), or *erase* the type by storing it in a //! [trait object](https://doc.rust-lang.org/beta/book/ch17-02-trait-objects.html). //! //! The [`serde_traitobject`](https://github.com/alecmocatta/serde_traitobject) //! crate enables trait objects to be safely serialized and sent between other //! processes running the same binary. //! //! For example, if you have multiple forks of a process, or the same binary //! running on each of a cluster of machines, //! [`serde_traitobject`](https://github.com/alecmocatta/serde_traitobject) //! would help you to send serializable closures between them. #![doc(html_root_url = "https://docs.rs/serde_closure/0.2.5")] #![feature(unboxed_closures, fn_traits)] #![warn( missing_copy_implementations, missing_debug_implementations, missing_docs, trivial_casts, trivial_numeric_casts, unused_import_braces, unused_qualifications, unused_results, clippy::pedantic )] // from https://github.com/rust-unofficial/patterns/blob/master/anti_patterns/deny-warnings.md #![allow(clippy::inline_always)] use proc_macro_hack::proc_macro_hack; /// Macro that wraps a closure, evaluating to a [`FnOnce`](structs::FnOnce) /// struct that implements [`std::ops::FnOnce`], [`Debug`](std::fmt::Debug), /// [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize), and /// various convenience traits. /// /// See the [readme](self) for examples. #[proc_macro_hack(fake_call_site)] pub use serde_closure_derive::FnOnce; /// Macro that wraps a closure, evaluating to a [`FnMut`](structs::FnMut) struct /// that implements [`std::ops::FnMut`], [`Debug`](std::fmt::Debug), /// [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize), and /// various convenience traits. /// /// See the [readme](self) for examples. #[proc_macro_hack(fake_call_site)] pub use serde_closure_derive::FnMut; /// Macro that wraps a closure, evaluating to a [`Fn`](structs::Fn) struct that /// implements [`std::ops::Fn`], [`Debug`](std::fmt::Debug), /// [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize), and /// various convenience traits. /// /// See the [readme](self) for examples. #[proc_macro_hack(fake_call_site)] pub use serde_closure_derive::Fn; #[doc(hidden)] pub mod internal { pub use core; pub use serde; pub use std; use std::marker::PhantomData; pub trait FnOnce<Args> { type Output; fn call_once(self, args: Args) -> Self::Output; } pub trait FnMut<Args>: FnOnce<Args> { fn call_mut(&mut self, args: Args) -> Self::Output; } pub trait Fn<Args>: FnMut<Args> { fn call(&self, args: Args) -> Self::Output; } #[inline(always)] pub fn to_phantom<T>(_t: &T) -> PhantomData<fn(T)> { PhantomData } #[inline(always)] pub fn is_phantom<T>(_t: &T, _marker: PhantomData<fn(T)>) {} #[allow(missing_copy_implementations, missing_debug_implementations)] pub struct ZeroSizedAssertion; } pub mod structs { //! Structs representing a serializable closure, created by the //! [`FnOnce`](macro@FnOnce), [`FnMut`](macro@FnMut) and [`Fn`](macro@Fn) //! macros. They Implement [`std::ops::FnOnce`], [`std::ops::FnMut`] and //! [`std::ops::Fn`] respectively, as well as [`Debug`](std::fmt::Debug), //! [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize), //! and various convenience traits. //! //! See the [readme](self) for examples. use serde::{Deserialize, Serialize}; use std::{ fmt::{self, Debug}, ops }; use super::internal; /// A struct representing a serializable closure, created by the /// [`FnOnce`](macro@FnOnce) macro. Implements [`std::ops::FnOnce`], /// [`Debug`], [`Serialize`] and [`Deserialize`], and various convenience /// traits. /// /// See the [readme](self) for examples. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] #[serde(transparent)] pub struct FnOnce<F> { f: F, } impl<F> FnOnce<F> { /// Internal method #[doc(hidden)] #[inline(always)] pub fn internal_new<I>(f: F) -> Self where F: internal::FnOnce<I>, { Self { f } } } impl<F, I> ops::FnOnce<I> for FnOnce<F> where F: internal::FnOnce<I>, { type Output = F::Output; #[inline(always)] extern "rust-call" fn call_once(self, args: I) -> Self::Output { self.f.call_once(args) } } impl<F> Debug for FnOnce<F> where F: Debug, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { Debug::fmt(&self.f, f) } } /// A struct representing a serializable closure, created by the /// [`FnMut`](macro@FnMut) macro. Implements [`std::ops::FnMut`], /// [`Debug`], [`Serialize`] and [`Deserialize`], and various convenience /// traits. /// /// See the [readme](self) for examples. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] #[serde(transparent)] pub struct FnMut<F> { f: F, } impl<F> FnMut<F> { /// Internal method #[doc(hidden)] #[inline(always)] pub fn internal_new<I>(f: F) -> Self where F: internal::FnMut<I>, { Self { f } } } impl<F, I> ops::FnOnce<I> for FnMut<F> where F: internal::FnOnce<I>, { type Output = F::Output; #[inline(always)] extern "rust-call" fn call_once(self, args: I) -> Self::Output { self.f.call_once(args) } } impl<F, I> ops::FnMut<I> for FnMut<F> where F: internal::FnMut<I>, { #[inline(always)] extern "rust-call" fn call_mut(&mut self, args: I) -> Self::Output { self.f.call_mut(args) } } impl<F> Debug for FnMut<F> where F: Debug, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { Debug::fmt(&self.f, f) } } /// A struct representing a serializable closure, created by the /// [`Fn`](macro@Fn) macro. Implements [`std::ops::Fn`], [`Debug`], /// [`Serialize`] and [`Deserialize`], and various convenience traits. /// /// See the [readme](self) for examples. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] #[serde(transparent)] pub struct Fn<F> { f: F, } impl<F> Fn<F> { /// Internal method #[doc(hidden)] #[inline(always)] pub fn internal_new<I>(f: F) -> Self where F: internal::Fn<I>, { Self { f } } } impl<F, I> ops::FnOnce<I> for Fn<F> where F: internal::FnOnce<I>, { type Output = F::Output; #[inline(always)] extern "rust-call" fn call_once(self, args: I) -> Self::Output { self.f.call_once(args) } } impl<F, I> ops::FnMut<I> for Fn<F> where F: internal::FnMut<I>, { #[inline(always)] extern "rust-call" fn call_mut(&mut self, args: I) -> Self::Output { self.f.call_mut(args) } } impl<F, I> ops::Fn<I> for Fn<F> where F: internal::Fn<I>, { #[inline(always)] extern "rust-call" fn call(&self, args: I) -> Self::Output { self.f.call(args) } } impl<F> Debug for Fn<F> where F: Debug, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { Debug::fmt(&self.f, f) } } }