Skip to content

Instantly share code, notes, and snippets.

@matthewhammer
Created May 6, 2017 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewhammer/4e3e576edea5cea2c425d0acf95417df to your computer and use it in GitHub Desktop.
Save matthewhammer/4e3e576edea5cea2c425d0acf95417df to your computer and use it in GitHub Desktop.
Adapton avoids divide by zero
#[macro_use]
extern crate adapton;
use adapton::macros::*;
use adapton::engine::*;
fn main() {
manage::init_dcg();
let nom = cell!(42);
let den = cell!(2);
// In Rust, cloning is explicit:
let den2 = den.clone(); // here, we clone the _global reference_ to the cell.
let den3 = den.clone(); // here, we clone the _global reference_ to the cell, again.
let div = thunk![ get!(nom) / get!(den) ];
let check = thunk![ if get!(den2) == 0 { None } else { Some(get!(div)) } ];
assert_eq!(get!(check), Some(21));
set(&den3, 0); assert_eq!(get!(check), None);
set(&den3, 2); assert_eq!(get!(check), Some(21));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment