forgetful

Crates.ioforgetful
lib.rsforgetful
version0.1.0
sourcesrc
created_at2023-07-15 21:30:19.681769
updated_at2023-07-15 21:30:19.681769
descriptionTrack and forget values within a specific scope, enabling detection of repeated values.
homepage
repositoryhttps://github.com/cwaldren/forgetful-observer/
max_upload_size
id917451
size8,837
Casey Waldren (cwaldren)

documentation

README

forgetful observer

This crate allows you to track items seen during execution of an algorithm using RAII.

An observation of a particular item is represented by an Obervation<T>. When this object falls out of scope, the item is forgotten.

This might be useful when implementing a recursive algorithm on a graph that must detect cycles.

Here's an example:

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());

The Observer can track any item that is Eq + Hash. For example:

observer.notice(&42);

Internally, Observer stores references to the items it notices in a HashSet.

Upon the Observation's destruction, the item reference is removed from the set.

Commit count: 5

cargo fmt