-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See the changelog for more details.
- Loading branch information
Showing
7 changed files
with
75 additions
and
12 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
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
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
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//! This test checks that you cannot have multiple entries from a `StateMap` | ||
//! alive at the same time. | ||
//! | ||
//! When compiling it, the borrow-checker is supposed to throw an error. | ||
use concordium_std::*; | ||
|
||
pub fn main() { | ||
let mut state_builder = StateBuilder::open(ExternStateApi::open()); | ||
let mut map: StateMap<u8, u8, _> = state_builder.new_map(); | ||
// Get two entries. | ||
let e1 = map.entry(0u8); | ||
let e2 = map.entry(1u8); | ||
// Use them, so we are certain that their lifetimes overlap. | ||
e1.or_insert(1); | ||
e2.or_insert(2); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error[E0499]: cannot borrow `map` as mutable more than once at a time | ||
--> tests/state/map-multiple-entries.rs:12:14 | ||
| | ||
11 | let e1 = map.entry(0u8); | ||
| -------------- first mutable borrow occurs here | ||
12 | let e2 = map.entry(1u8); | ||
| ^^^^^^^^^^^^^^ second mutable borrow occurs here | ||
13 | // Use them, so we are certain that their lifetimes overlap. | ||
14 | e1.or_insert(1); | ||
| -- first borrow later used here |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//! This test checks that you cannot have multiple `StateRefMut` from a | ||
//! `StateMap` alive at the same time. | ||
//! | ||
//! When compiling it, the borrow-checker is supposed to throw an error. | ||
use concordium_std::*; | ||
|
||
pub fn main() { | ||
let mut state_builder = StateBuilder::open(ExternStateApi::open()); | ||
let mut map: StateMap<u8, u8, _> = state_builder.new_map(); | ||
map.insert(0, 1); | ||
map.insert(1, 2); | ||
// Get two mutable references and unwrap the options. | ||
let e1 = map.get_mut(&0u8).unwrap(); | ||
let e2 = map.get_mut(&1u8).unwrap(); | ||
// Use them, so we are certain that their lifetimes overlap. | ||
assert_eq!(*e1, *e2); | ||
} |
10 changes: 10 additions & 0 deletions
10
concordium-std/tests/state/map-multiple-state-ref-mut.stderr
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error[E0499]: cannot borrow `map` as mutable more than once at a time | ||
--> tests/state/map-multiple-state-ref-mut.rs:14:14 | ||
| | ||
13 | let e1 = map.get_mut(&0u8).unwrap(); | ||
| ----------------- first mutable borrow occurs here | ||
14 | let e2 = map.get_mut(&1u8).unwrap(); | ||
| ^^^^^^^^^^^^^^^^^ second mutable borrow occurs here | ||
15 | // Use them, so we are certain that their lifetimes overlap. | ||
16 | assert_eq!(*e1, *e2); | ||
| -- first borrow later used here |