Open
Description
Collection of design patterns and the corresponding issues if not contained in the repository already (in reference to Design patterns (by Gamma, Helm, Johnson, Vlissides)). Some patterns are probably not applicable to/realizable with Rust as they stem from OOP, but we'll sort that out on the way.
But in general I think it is also nice to have design patterns
listed somewhere on a page that are not applicable to Rust and stated why and with an added workaround if any.
Checkmark = already existing in repository (link to file)
No checkmark = Link to corresponding issue
No checkmark and no link = Check if applicable for Rust
Coverage
Patterns
Creational Patterns
Structural Patterns
Behavioural Patterns
- Interpreter
- Strategy
- Visitor
- Command
- Observer
- Chain of Responsibility
- Iterator
- State
- Mediator
- Memento
- Template Method
Still Uncategorized
- [WIP] Typestates/Session types
- Entry API
- Late bound bounds
- Graphs
- Platform-specific sub-modules
- Destructor bombs (ensure linear typing dynamically
- Convertible to Foo trait for more generic generics
- Iterators (to safely avoid bounds checks)
- Closures and lifetimes (coupling to lifetime)
- Module organisation
by looking at examples such as Rustslibstd
, and how it integrated into the Rusts source code, lessons can be learned about ergonomic project management and API design. Closely assosciated with platform-specific sub-modules - Extension traits
- 'Shadow' borrowed version of struct - e.g., double buffering, Niko's parser generator
- Composition of structs to please the borrow checker
-
Error
traits andResult
forwarding
Idioms
- Trait to separate visibility of methods from visibility of data
- Stability for extensibility
- Leak amplification
"Vec::drain sets the Vec's len to 0 prematurely so that mem::forgetting Drain "only" mem::forgets more stuff. instead of exposing uninitialized memory or having to update the len on every iteration" - Interior mutability - UnsafeCell, Cell, RefCell
Anti-patterns
- [WIP] Thread + catch_panic for exceptions
- [WIP] Clone to satisfy the borrow checker
- Matching all fields of a struct (back compat)
- Wildcard matches
- Taking an enum rather than having multiple functions
-
Unwrap()
ing everyResult
instead of forwarding it
Functional Programming
- Currying (from Add introduction #117)
- Higher-order functions (from Add introduction #117)
- Collections (from Add introduction #117)
Refactoring
- Refactoring from
unwrap
toResult
General Unsorted
- Public type aliases
- Use conversion traits
- Laziness: Use Iterators
- Laziness: Take closures
- Custom traits for input parameters
- Extension traits
Notes
- Inspiration for more patterns #35: I recently started collecting some API design patterns here. I'm not sure if these match what this repo is all about (or which ones you already cover), but I just wanted to say: Feel free to take anything you like from that post! :)
The markdown source of that post is here and I hereby relicense the content as MPL2 (in addition to CC-BY) and allow you to use it in this repo.