Skip to main content

geop_lib_constraints/
lib.rs

1//! Constraints on a part's state, and the solver that moves the state until
2//! they hold.
3//!
4//! A program's steps say what they require of the state ([`Constraint`]s,
5//! [`constraint::define_constraints`]) and which values of it a solve may
6//! move ([`unknown::Unknown`]s, [`unknown::declare_unknowns`]). Each of the
7//! two is one target of the part, built up by the steps one revision after
8//! another ([`Constraints`], [`unknown::Unknowns`]); [`solve`] reads them
9//! from the part the program builds at every state it tries.
10
11// Library code reports what goes wrong as an error, never by panicking;
12// tests may unwrap.
13#![cfg_attr(
14    not(test),
15    deny(
16        clippy::unwrap_used,
17        clippy::expect_used,
18        clippy::panic,
19        clippy::indexing_slicing,
20        clippy::todo,
21        clippy::unimplemented,
22        clippy::unreachable
23    )
24)]
25
26pub mod constraint;
27pub mod solve;
28pub mod unknown;
29
30pub use constraint::{Bound, Constraint, Constraints};
31
32use geop_core_program::part::Make;
33
34/// A step's arguments with the generator that makes what it contributes
35/// from them: the arguments of the target that runs it, since a generator
36/// captures nothing. Two are equal where both are, so a version made by the
37/// same generator from equal arguments is reused.
38struct Generated<Args, Out> {
39    args: Args,
40    generator: Make<Args, Out>,
41}
42
43// By hand, since a derive would ask the same of `Out`.
44impl<Args: Clone, Out> Clone for Generated<Args, Out> {
45    fn clone(&self) -> Self {
46        Self {
47            args: self.args.clone(),
48            generator: self.generator,
49        }
50    }
51}
52
53/// The generator by its address, which is this process's own: what read a
54/// generated target is made again in another process (see
55/// `geop_core_program::kept`), never taken for what another generator made.
56impl<Args: std::fmt::Debug, Out> std::fmt::Debug for Generated<Args, Out> {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        let generator = self.generator as *const ();
59        write!(f, "Generated({:?}, {generator:p})", self.args)
60    }
61}
62
63impl<Args: PartialEq, Out> PartialEq for Generated<Args, Out> {
64    fn eq(&self, other: &Self) -> bool {
65        self.args == other.args && std::ptr::fn_addr_eq(self.generator, other.generator)
66    }
67}
68
69#[cfg(test)]
70mod test;