cassowary-rs icon indicating copy to clipboard operation
cassowary-rs copied to clipboard

Support domain specific variable type

Open schell opened this issue 4 years ago • 0 comments

  • adds a type parameter to many types, representing a solver variable type
  • removes Variable in favor of tools for writing your own domain specific variable types
  • adds some chain/builder-style functions for creating constraints
  • moves some other things around where they found better homes
  • moves a bunch of trait implementations into a macro
  • removes all instances of Arc, Rc and RefCell in favor of OrderedFloat and Option - Solver can now safely be passed between threads with Send and Sync
  • generalizes Solver::optimise and removes Solver.artificial as its no longer needed
  • bumps the version to 0.4

This provides library users with the ability to specify their own variables. All the structs associated with constructing constraint syntax now take a type variable that represents the domain specific variable type. Variable itself is removed as its trivial to derive it. The tests provide an example. There's a new module derive_syntax for automatically deriving the necessary machinery to write constraints and expressions using the domain specific variable type.

This has two good effects:

  1. There's no need for maintaining a HashMap<MyType, Variable> mapping from meaning to cassowary variable.
  2. It's now very easy to run n separate solvers - one for each dimension - and not have the types mix. For example:
pub struct Entity {...}
pub enum XVariable {
  Left(Entity), Width(Entity), Right(Entity)
}
derive_syntax_for!(XVariable);

pub enum YVariable {
  Top(Entity), Height(Entity), Bottom(Entity)
}
derive_syntax_for!(YVariable);

type XSolver = Solver<XVariable>;
type YSolver = Solver<YVariable>;

^ Here you can't mix constraints from the two axes without triggering a compile time error, which I think I like. Anyway - there's only a couple tests really, so YMMV.

This is currently a draft PR until I update the documentation.

schell avatar Oct 19 '19 14:10 schell