mozilla/rust — Issue #1567: make interface types more like closures
Author: nikomatsakis
Created: 2012-01-19 16:41:40+00:00
State: open
Labels: rfc

Interface types are really very similar conceptually and in practice to closures. I think we should make them resemble closures more closely both in the language and implementation as follows:

- Make interface instance two words: `(vtable, data)` (analogous to a closure's (fptr, data))
- Model a vtable with one entry as a single function pointer
- Use type bounds after the iface name to indicate how the data is stored (& vs @ vs ~)

Therefore, for an iface `X`, the type:

- `X` indicates any interface instance Such a closure cannot be copied, just as `fn` cannot be copied
- `X&`indicates an interface instance whose data is located on the stack.
- `X@` indicates an interface instance whose data is boxed
- `X~` indicates an interface instance whose data is sendable and stored via unique ptr

Advantages:

- More analogous.
- `X~` instances can be sent
- `X&` instances are particularly cheap to construct
- Closures are a special case of an interface instance (one with a single method), in the impl at least.