Trait

1

trait: From Function Pointer Tables to Behavior Abstraction

5 minute

C has no language-level interface, but C projects have always had interfaces.

Driver tables, callback tables, vtables, opaque pointers, and void *ctx plus function pointers all express the same idea: an object can perform a group of operations.

Rust trait also expresses behavior. The difference is that Rust puts “this type must provide these methods” into the type system instead of relying only on struct fields and calling conventions.

This article covers the first use of traits: migrating C function pointer tables into Rust behavior abstraction.

Read More