Introduction

This book covers the design patterns for context-generic programming (CGP), a new programming paradigm for Rust that allows strongly-typed components to be implemented and composed in a modular, generic, and type-safe way.

What is Context-Generic Programming

A high level overview of CGP is available on the project website. This section contains a summarized version of the overview.

At its core, CGP makes use of Rust's trait system to build generic component interfaces that decouple code that consumes an interface from code that implements an interface. Through this decoupling, code can be written to be generic over any context, and then be wired to be used on a concrete context by writing few lines of code. CGP makes use of Rust's strong type system to help ensure that any such wiring is type-safe, catching any unsatisfied dependencies as compile-time errors.

CGP shares some similarities with other modular programming patterns, such as OCaml modules, Scala implicits, mixins, and dependency injection. Compared to these other patterns, CGP has a unique advantage that it enables high modularity while also being type-safe and concise. With Rust as its host language, CGP also allows high-performance and low-level code to be written in a modular way, without requiring complex runtime support.

CGP is designed to solve a wide range of common problems in programming, including error handling, logging, encoding, and modular dispatch. In particular, it allows writing static-typed Rust programs to be almost as expressive as writing dynamic-typed programs, but with the additional type safety guarantees. If you ever feel that Rust's type system is restricting your ability to reuse code, or be forced to duplicate code through copy-pasting or macros, then CGP may be of help to solve your problem.

That said, programming in CGP is as expressive, but not as easy, as dynamic-typed programming. There may be a steep learning curve in learning how to program in a generic way, and this book aims to help make that learning process more approachable. Thoughout this book, we will slowly understand how CGP works, and learn about useful design patterns that can be used in any programming situation.

The State of This Book

This book was last revised for CGP v0.4.0, and the library has moved on since. What that means for you depends on which chapter you are reading.

The Terminology and Core Concepts sections, up to and including Provider Traits, explain the ideas using ordinary Rust traits and implementations. They do not depend on a particular CGP release, and they remain accurate.

The chapters after those show CGP's macros and its wiring, and they predate several changes to that syntax. Read them for the ideas rather than for the code, and check anything you intend to write against the documentation on the project website, which tracks the current release.

The entries listed in the table of contents without a link were never written, and most of what they planned has since been written on the website instead. That is the place to look for the current explanations and reference material.

Who This Book Is For

This book takes a deep dive into how CGP works, building and explaining its core constructs from the ground up.

However, it's worth noting that many of the concepts explained here are not immediately essential for simply writing working CGP code, particularly when you are just starting out. This is because CGP provides a high level of abstraction, allowing developers to write highly productive code without needing to understand all the underlying details.

If your primary goal is to start writing CGP code and become productive as quickly as possible, this book might not be the most direct path.

On the other hand, this book is primarily intended for those who wish to gain a better understanding of what happens "behind the macros" after they have gained some experience using CGP.

It will also be a valuable resource for users encountering CGP-related error messages, providing the knowledge needed to understand the root cause and fix issues more effectively.

For beginners looking for a faster start, the tutorials on the project website are the shorter path.

Chapter Outlines

The first section of this book, Terminology, will introduce common terms to be used to understand CGP. We will learn about what is a context, and what are consumer and provider traits.

In the next section, Core Concepts, we will cover the core concepts and the Rust-specific design patterns that we use to enable context-generic programming.

Following that, Design Patterns will introduce general design patterns that are built on top of the foundation of context-generic programming.

The section Domain-Specific Patterns will cover use-case-specific design patterns, such as error handling and logging.

Finally, the secion Related Concepts will compare context-generic programming with related concepts, such as the similarity and differences of context-generic programming as compared to object-oriented programming.

Contribution

This book is open sourced under the MIT license on GitHub.

Anyone is welcome to contribute by submitting pull requests for grammatical correction, content improvement, or adding new design patterns.

A GitHub Discussions forum is available for readers to ask questions or have discussions for topics covered in this book.