-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test with by value type containing mut ref.
- Loading branch information
1 parent
3549845
commit 9eac75c
Showing
1 changed file
with
9 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |