rust-unofficial/patterns

Umbrella: Repository Content (Scope)/ TODOs

simonsan opened this issue · 4 comments

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

Still Uncategorized

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

Functional Programming

  • Currying (from #117)
  • Higher-order functions (from #117)
  • Collections (from #117)

Refactoring

  • Refactoring from unwrap to Result

General Unsorted

  • Public type aliases
  • Use conversion traits
  • Laziness: Use Iterators
  • Laziness: Take closures
  • Custom traits for input parameters
  • Extension traits

Notes

  • #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

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
Strategy pattern: https://medium.com/swlh/strategy-design-pattern-in-rust-5f5486cd294c
I can add it here.

Nice, for sure! 💯

Today I discovered https://github.com/udoprog/patterns where game engine patterns are 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 at udoprog/patterns: udoprog/patterns#1

Something new to addon for cell if anyone work on cell. https://plv.mpi-sws.org/rustbelt/ghostcell/