#逻辑 #黑暗 #黑色 #魔法 #可怕

召唤

一个逻辑引擎,可以神奇地为你提供你想要的东西

4 个版本 (2 个重大变更)

0.3.1 2020年4月24日
0.3.0 2020年4月24日
0.2.0 2020年4月24日
0.1.0 2020年4月24日

#10#黑色

MIT 许可证

15KB
179

召唤

Crates.io MIT/Apache docs.rs LoC Tests Lints

一个逻辑引擎,可以神奇地为你提供你想要的东西

由于代码执行了一些可疑操作,需要夜版本。

你想学习黑暗魔法吗?如果是的话,那么请阅读代码,但要注意!

示例

#![feature(const_type_id)]
use summon::{Tome, circle};

#[derive(Clone)]
struct ConstantAcceleration(f64);
#[derive(Clone)]
struct InitialVelocity(f64);
#[derive(Clone)]
struct InitialPosition(f64);
#[derive(Clone)]
struct Time(f64);

#[derive(Debug)]
struct Distance(f64);

// The tome is where all the logic and conversions are written in your code.
let mut tome = Tome::new();

// You can use ether() to give types as givens.
tome.ether(ConstantAcceleration(3.0));
tome.ether(InitialVelocity(5.0));
tome.ether(InitialPosition(6.0));
tome.ether(Time(4.0));

// Inscribe is used to describe a conversion between types.
// Several macros are provided for convenience.
// This one lets you destructure and type construct freely.
tome.inscribe(
    circle!(ConstantAcceleration(a), InitialVelocity(v), InitialPosition(p), Time(t) => Distance(0.5 * a * t.powi(2) + v * t + p))
);

// So long as it is possible to produce the result with the given inscriptions, it will be produced.
let summoned = tome.summon::<Distance>().unwrap().0;
assert_eq!(
    0.5 * 3.0 * 4.0f64.powi(2) + 5.0 * 4.0 + 6.0,
    summoned,
);

代码免责声明

Rust 本意不是做这个的。现在回头还来得及!如果读者是软件工程师,这段代码可能会引起阅读者的不适。请谨慎行事。如果阅读代码导致面部痉挛和/或死亡,我概不负责。读者必须自行承担风险。已经警告过了!

依赖项

~11KB