Skip to content

make interface types more like closures #1567

Closed
@nikomatsakis

Description

@nikomatsakis

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.

Metadata

Metadata

Assignees

Labels

A-lifetimesArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions