6 个版本

0.2.2 2024年5月6日
0.2.1 2024年2月16日
0.2.0 2023年11月14日
0.1.2 2023年5月8日
0.0.0-0.0.0-0.0.0 2022年11月7日

#133 in 算法

Download history 207/week @ 2024-05-04 48/week @ 2024-05-11 61/week @ 2024-05-18 68/week @ 2024-05-25 96/week @ 2024-06-01 97/week @ 2024-06-08 105/week @ 2024-06-15 114/week @ 2024-06-22 85/week @ 2024-06-29 126/week @ 2024-07-06 96/week @ 2024-07-13 162/week @ 2024-07-20 211/week @ 2024-07-27 426/week @ 2024-08-03

923 每月下载量
用于 incremental-map

MIT 许可证

245KB
5.5K SLoC

incremental-rs

将 Jane Street 的 Incremental 库移植过来。

  • 基本 Incr 功能的相当严谨的实现,测试和基准测试良好,性能与原始版本非常相似。
  • 部分实现 incremental-map

安装

cargo add incremental

示例

基本用法

use incremental::IncrState;

let state = IncrState::new();
let variable = state.var(5);
let times_10 = variable.map(|num| num * 10);
let observer = times_10.observe();

// stabilise will propagate any changes
state.stabilise();
let value = observer.value();
assert_eq!(value, 50);

// now mutate
variable.set(10);
state.stabilise();

// watch as var was propagated through the tree, and reached our observer
assert_eq!(observer.value(), 100);

订阅,以及当节点产生与上次相同的值时传播停止的说明

use incremental::{IncrState, Update};

// A little system to compute the absolute value of an input
// Note that the input could change (e.g. 5 to -5), but the
// output may stay the same (5 both times).
let state = IncrState::new();
let variable = state.var(5i32);
let absolute = variable.map(|num| num.abs());
let observer = absolute.observe();

// set up a subscription.
use std::{cell::RefCell, rc::Rc};
let latest = Rc::new(RefCell::new(None));
let latest_clone = latest.clone();
let subscription_token = observer.subscribe(move |update| {
     *latest_clone.borrow_mut() = Some(update.cloned());
});

// initial stabilisation
state.stabilise();
assert_eq!(observer.value(), 5);
assert_eq!(latest.borrow().clone(), Some(Update::Initialised(5)));

// now mutate, but such that the output of abs() won't change
variable.set(-5);
state.stabilise();
// The subscription function was not called, because the `absolute` node
// produced the same value as last time we stabilised.
assert_eq!(latest.borrow().clone(), Some(Update::Initialised(5)));
assert_eq!(observer.value(), 5);

// now mutate such that the output changes too
variable.set(-10);
state.stabilise();
// The observer did get a new value, and did call the subscription function
assert_eq!(latest.borrow().clone(), Some(Update::Changed(10)));
assert_eq!(observer.value(), 10);

// now unsubscribe. this also implicitly happens if you drop the observer,
// but you can individually unsubscribe particular subscriptions if you wish.
observer.unsubscribe(subscription_token);
// dropping the observer also unloads any part of the computation graph
// that was only running for the purposes of this particular observer
drop(observer);

// now that the observer is dead, we can mutate the variable and nothing will
// happen, like, at all. The absolute value will not be computed.
variable.set(100000000);
let recomputed = state.stats().recomputed;
state.stabilise();
assert_eq!(recomputed, state.stats().recomputed);

依赖

~375–780KB
~14K SLoC