Skip to content

Commit

Permalink
Add test with by value type containing mut ref.
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbolliger committed Jun 22, 2021
1 parent 3549845 commit 9eac75c
Showing 1 changed file with 9 additions and 44 deletions.
53 changes: 9 additions & 44 deletions stainless_frontend/tests/pass/mut_ref_borrow_11.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,16 @@
extern crate stainless;

/// This benchmark stems from [RustVis Specs](http://erik.vestera.as/rustvis/specs)
/// and was adapted to work with stainless.

struct Container<K, V> {
pair: Option<(K, V)>,
}

impl<K, V> Container<K, V> {
pub fn new() -> Self {
Container { pair: None }
}

pub fn insert(&mut self, k: K, v: V) {
self.pair = Some((k, v))
}
}

impl<V> Container<String, V> {
pub fn get_mut(&mut self, key: &String) -> Option<&mut V> {
match &mut self.pair {
Some((k, v)) if *k == *key => Some(v),
_ => None,
}
#[allow(unused_mut, unused_assignments)]
fn change(mut opt: Option<&mut i32>) {
if let Some(x) = opt {
*x = 456;
}
opt = None;
}

pub fn main() {
let mut target = Container::new();
let key = "foo".to_string();

let ref1 = &mut target;
match ref1.get_mut(&key) {
Some(value) => *value = 123,
_ => {
target.insert(key.clone(), 5);
match target.get_mut(&key) {
Some(v) => *v = 123,
_ => panic!("no value"),
}
}
}

assert!(matches!(
target,
Container {
pair: Some((key, 123))
} if key == "foo"
))
let mut x = 123;
change(None);
change(Some(&mut x));
assert!(x == 456)
}

0 comments on commit 9eac75c

Please sign in to comment.