#closure #serialization #serde #distributed

nightly serde_closure

Serializable and debuggable closures. This library provides macros that wrap closures to make them serializable and debuggable

17 releases

✓ Uses Rust 2018 edition

0.2.10 Feb 5, 2020
0.2.9 Nov 22, 2019
0.2.5 Oct 19, 2019
0.1.5 Jul 25, 2019
0.1.2 Jul 30, 2018

#59 in Encoding

Download history 124/week @ 2019-11-01 148/week @ 2019-11-08 137/week @ 2019-11-15 258/week @ 2019-11-22 100/week @ 2019-11-29 78/week @ 2019-12-06 771/week @ 2019-12-13 27/week @ 2019-12-20 89/week @ 2019-12-27 35/week @ 2020-01-03 30/week @ 2020-01-10 302/week @ 2020-01-17 229/week @ 2020-01-24 132/week @ 2020-01-31 431/week @ 2020-02-07

762 downloads per month
Used in 10 crates (9 directly)

MIT/Apache

22KB
265 lines

serde_closure

Crates.io MIT / Apache 2.0 licensed Build Status

Docs

Serializable and debuggable closures.

This library provides macros that wrap closures to make them serializable and debuggable.

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).

  • There are three macros, FnOnce, FnMut and 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:

|a| a+1
FnMut!(|a| a+1)

Annotated, non-capturing closure:

|a: String| -> String { a.to_uppercase() }
FnMut!(|a: String| -> String { a.to_uppercase() })

Inferred closure, capturing num:

let mut num = 0;
|a| num += a
let mut num = 0;
FnMut!(|a| num += a)

move closure, capturing hello and world:

let hello = String::from("hello");
let mut world = String::new();
move |name| {
    world += (hello.to_uppercase() + name).as_str();
}
let hello = String::from("hello");
let mut world = String::new();
FnMut!(move |name| {
    world += (hello.to_uppercase() + name).as_str();
})

Limitations

There are currently some minor limitations:

  • Use of types that start with a lowercase letter need might need to be disambiguated from variables. If you see an error like the following, fix the case of the type, or append it with my_struct::<> to disambiguate.
error[E0308]: mismatched types
   --> tests/test.rs:450:4
    |
449 |       FnOnce!(move || {
    |  _____-
450 | |         my_struct;
    | |         ^^^^^^^^^ expected struct `serde_closure::internal::a_variable`, found struct `my_struct`
451 | |     });
    | |______- in this macro invocation
    |
    = note: expected type `serde_closure::internal::a_variable`
               found type `my_struct`
  • Use of variables that start with an uppercase letter might need to be disambiguated from types. If you see an error like the following, fix the case of the variable, or wrap it with (MyVariable) to disambiguate.
error: imports cannot refer to local variables
   --> tests/test.rs:422:3
    |
417 |       FnOnce!(move || {
    |  _____-
418 | |         MyVariable;
    | |         ^^^^^^^^^^
419 | |     });
    | |______- in this macro invocation
    |
  • Functions and closures called inside the closure might need to be disambiguated. This can be done the same as above with function::<> for functions and (closure) for closures.

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.

The 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 would help you to send serializable closures between them. This can be done by upcasting the closure to a Box<dyn serde_traitobject::Fn()>, which is automatically serializable and deserializable with serde.

License

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.7–1.1MB
~25K SLoC