/
inventory.rs
47 lines (38 loc) · 1.25 KB
/
inventory.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT
// ANCHOR: inventory_lib
use std::num::NonZeroU32;
use vector_map::VecMap;
pub type ProductId = u32;
pub struct Inventory {
/// Every product in inventory must have a non-zero quantity
pub inner: VecMap<ProductId, NonZeroU32>,
}
impl Inventory {
pub fn update(&mut self, id: ProductId, new_quantity: NonZeroU32) {
self.inner.insert(id, new_quantity);
}
pub fn get(&self, id: &ProductId) -> Option<NonZeroU32> {
self.inner.get(id).cloned()
}
}
// ANCHOR_END: inventory_lib
#[cfg(kani)]
mod verification {
use super::*;
// ANCHOR: safe_update
#[kani::proof]
#[kani::unwind(3)]
pub fn safe_update() {
// Empty to start
let mut inventory = Inventory { inner: VecMap::new() };
// Create non-deterministic variables for id and quantity.
let id: ProductId = kani::any();
let quantity: NonZeroU32 = kani::any();
assert!(quantity.get() != 0, "NonZeroU32 is internally a u32 but it should never be 0.");
// Update the inventory and check the result.
inventory.update(id, quantity);
assert!(inventory.get(&id).unwrap() == quantity);
}
// ANCHOR_END: safe_update
}