Something went wrong. Try again.
forked from verus-lang/verus
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455use vstd::prelude::*;use vstd::invariant::*;use vstd::cell::pcell_maybe_uninit::*;use vstd::cell::CellId;use vstd::simple_pptr::MemContents;
verus!{
ghost struct EvenCell { }
impl InvariantPredicate<CellId, PointsTo<u8>> for EvenCell { open spec fn inv(cell_id: CellId, points_to: PointsTo<u8>) -> bool { points_to.id() == cell_id && (match points_to.mem_contents() { MemContents::Uninit => false, MemContents::Init(x) => x % 2 == 0, }) }}
fn add_2(cell: &PCell<u8>, Tracked(inv): Tracked<&LocalInvariant<CellId, PointsTo<u8>, EvenCell>>) requires inv.constant() == cell.id(),{ open_local_invariant!(inv => points_to => { assert(points_to.is_init()); assert(points_to.value() % 2 == 0);
let x = cell.take(Tracked(&mut points_to)); assert(x % 2 == 0);
// Add 2 (wrap around if necessary) let x_plus_2 = if x == 254 { 0 } else { x + 2 };
cell.put(Tracked(&mut points_to), x_plus_2);
assert(points_to.is_init()); assert(points_to.value() % 2 == 0); });}
fn main() { let (cell, Tracked(points_to)) = PCell::new(4);
let tracked inv = LocalInvariant::new( cell.id(), points_to, 1337 /* arbitrary namespace */);
add_2(&cell, Tracked(&inv)); add_2(&cell, Tracked(&inv)); add_2(&cell, Tracked(&inv));}
}