1 个不稳定版本
0.1.0 | 2023年7月15日 |
---|
#1681 在 数据结构
7KB
138 行
遗忘观察者
此包允许您使用 RAII 跟踪算法执行期间看到的项。
特定项的观察由一个 Obervation<T>
表示。当此对象超出作用域时,项就会被遗忘。
当在必须检测循环的图上实现递归算法时,这可能是很有用的。
以下是一个示例
let observer = Observer::new();
{
let observation = observer.notice("foo").expect("never seen before");
// While 'observation' is in scope, subsequent calls to notice return None.
assert!(observer.notice("foo").is_none());
}
// Now that 'observation' is out of scope, this will return Some(Observation).
assert!(observer.notice("foo").is_some());
Observer
可以跟踪任何满足 Eq + Hash
条件的项。例如
observer.notice(&42);
内部,Observer
使用 HashSet
存储它注意到的项的引用。
在 Observation
被销毁后,项引用将从集合中删除。