Skip to content

Instantly share code, notes, and snippets.

@Tobba
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tobba/1dacf1060a68aca54941 to your computer and use it in GitHub Desktop.
Save Tobba/1dacf1060a68aca54941 to your computer and use it in GitHub Desktop.
  • Start Date: (fill me in with today's date, YYYY-MM-DD)
  • RFC PR: (leave this empty)
  • Rust Issue: (leave this empty)

Summary

Currently, each unboxed closure definition generates its own unique type, functions however do not exhibit the same behavior. This RFC proposes the addition of a unique compiler-generated zero-sized anonymous type for each function definition.

Motivation

This allows a normal function to be used in place where any unboxed closure would, with the exact same generated code as if the function was an unboxed closure with no captures. As it stands the function would instead be passed as a pointer, which leads to pointless indirection and missed optimizations.

As this causes a function to be equivalent (bar the coercions to fn() and calling syntax) to a struct with no fields implementing Fn, FnMut and FnOnce, it may lead to further simplification of the language and the compiler.

Detailed design

Each function shall have its own anonymous, zero-sized type, henceforth referred to as a "function type". Each function type should implement Fn, FnMut and FnOnce. Any reference of a function as a literal should result in the instantiation of that functions associated type. Every function type should also be coercible to an appropriate fn() type.

Drawbacks

The superfluous type information may lead to slowdowns during compilation. This is also introduces extra coercion rules which may be undesirable.

Alternatives

None currently proposed.

Unresolved questions

None.

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