Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support domain specific variable type #11

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

schell
Copy link

@schell schell commented Oct 19, 2019

  • 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 schell marked this pull request as ready for review August 21, 2022 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants