Skip to content

Serializable Lambda expression #1022

@hyunsik

Description

@hyunsik

Rust basically is very strong for parallel programming. If Rust supports Serializable Lambda expression, it would be very strong even for distributed programming.

Here was a little discussion.
rust-lang/rust#23759

Python and Java 8 already have similar concepts as follows:

Activity

kmcallister

kmcallister commented on Dec 30, 2015

@kmcallister
Contributor

See also #668.

Rufflewind

Rufflewind commented on Mar 13, 2017

@Rufflewind

One could do add some compiler magic to support arbitrary object serialization in Rust, with the caveat that such an object can only be deserialized by the same program. This would be like Any with persistence. The Haskell recently added support for these kinds of “static pointers”.

But the downside of this is that the user has no control over the serialization protocol, and it would require making a serializer that works over arbitrarily complicated types, even though much of it could be done outside of the standard library.

A more flexible approach would be to expose two new mechanisms to Rust:

  • The ability to have an extensible lookup table for a selection of types, a “global type registry” if you will. This I think is the main missing ingredient for Encodable trait objects #668.

  • The ability to construct and deconstruct closures into a tuple of its upvars (Closure::Data). Typically, Closure::Data would be serializable through a #[derive] mechanism.

    pub trait Closure {
        type Data;
        fn construct(data: Self::Data) -> Self;
        fn deconstruct(self) -> Self::Data;
    }
    
    impl Closure for /* all unboxed closures */ {
        type Data = (/* upvars ... */);
        /* ... */
    }
    
    impl Closure for /* all fn(_) -> _ */ {
        type Data = ();
        /* ... */
    }

Edit: no need store a separate function pointer since the closure type itself would suffice.

added
T-langRelevant to the language team, which will review and decide on the RFC.
T-libs-apiRelevant to the library API team, which will review and decide on the RFC.
on Feb 23, 2018
ariesdevil

ariesdevil commented on Jun 20, 2018

@ariesdevil

Any updates on it? With write spark like system in rust, Serializable Lambda expression is needed.

habnabit

habnabit commented on Jun 26, 2018

@habnabit

(python's pickle does not serialize functions' code to a wire format; it only serializes the name of a function.)

ariesdevil

ariesdevil commented on Jun 26, 2018

@ariesdevil
alecmocatta

alecmocatta commented on Nov 2, 2019

@alecmocatta

I created a crate serde_closure that makes closures serializable by wrapping them with the macro Fn!(...).

If the resulting serializable closure is upcast to Box<dyn serde_traitobject::Fn()> it can then be serialized and sent between identical distributed processes using serde_traitobject, though this currently requires nightly.

Rufflewind

Rufflewind commented on Nov 2, 2019

@Rufflewind

Interesting. I had tried a similar idea a while back but deemed it too questionable for production use.

alecmocatta

alecmocatta commented on Nov 5, 2019

@alecmocatta

@Rufflewind Yes, thanks for your crate, it was good inspiration for serde_traitobject.

I just made a rust PR that would make serde_traitobject safe: rust-lang/rust#66113. Keen to hear if you have any thoughts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @habnabit@steveklabnik@hyunsik@kmcallister@Centril

        Issue actions

          Serializable Lambda expression · Issue #1022 · rust-lang/rfcs