Skip to main content

cgp-examples

cgp-examples is a repository of five small programs built with Context-Generic Programming (CGP), a language extension for Rust with pluggable trait implementations at compile-time. Each crate shows one area of CGP working in code, and between them they cover a wide range of CGP's design patterns. They share a workspace and nothing else, so each has a section of its own.

The crates are demonstrations, not libraries. None is published, and nobody adds them as a dependency: the way to use one is to clone the repository and run it, or read it. The code these pages show uses current CGP idioms throughout, so it is safe to copy for the patterns it shows.

The crates​

CrateWhat it showsHow it runs
expressionan arithmetic interpreter that gains operators and operations without editing old code: the extensible visitor patternthree unit tests, and a test to add for each of the other contexts
web-appone social-media backend wired four ways, from one trait per domain to namespace defaultsa check of its wiring; every provider body is todo!()
builderan application assembled from per-subsystem builders that never name the struct they build: the extensible builder patterncompile-time checks of its builders
transfera balance-and-transfer HTTP service whose every domain type and operation is a component, organized with a namespacea server binary
greeta greeting written as a CGP function, as a component with two providers, and over an abstract typethree binaries

The pages for builder, transfer, and greet are still being written. Until they are, each crate is in its own directory of the repository, and transfer has a README of its own.

Where to start​

  • To see the extensible visitor pattern, start with expression, whose four examples build on each other.
  • To see how wiring changes as an application grows, read the four stages of web-app in order.
  • To learn CGP from the beginning, start with the Hello World tutorial instead; these pages assume it rather than teach it.

Running the crates​

Clone the repository and work from its root. The workspace builds on stable Rust 1.90 or later, with the 2024 edition, and each crate is a package of its own, such as cgp-example-expression, so cargo test -p cgp-example-expression runs one crate's tests. Each crate's pages give the command for its programs.

Where to go next​

  • expression: the extensible visitor pattern, in four contexts.
  • web-app: one application's wiring at four scales.
  • Projects: the other projects on this site, and a table from each CGP pattern to an example that shows it.
  • Extensible data types, part 1 and part 2: the posts that developed the builder and expression crates. Their code predates the current design.

An AI agent wrote this page using the CGP knowledge base. Its content was verified against the project's source. See How AI is used in this project.