Skip to content

Commit

Permalink
Rename simulation mod to sim, improve SimResult
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Varlakov <[email protected]>
  • Loading branch information
survived committed Nov 29, 2024
1 parent 599cc69 commit f3b576a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 18 deletions.
4 changes: 2 additions & 2 deletions examples/random-generation-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ mod tests {

let n: u16 = 5;

let randomness = round_based::simulation::run_with_setup(
let randomness = round_based::sim::run_with_setup(
core::iter::repeat_with(|| rng.fork()).take(n.into()),
|i, party, rng| protocol_of_random_generation(party, i, n, rng),
)
Expand All @@ -201,7 +201,7 @@ mod tests {

let n: u16 = 5;

let randomness = round_based::simulation::async_env::run_with_setup(
let randomness = round_based::sim::async_env::run_with_setup(
core::iter::repeat_with(|| rng.fork()).take(n.into()),
|i, party, rng| protocol_of_random_generation(party, i, n, rng),
)
Expand Down
15 changes: 8 additions & 7 deletions round-based/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
## v0.4.0
* Improve ergonomics of protocol simulation, which is used for writing tests [#14]
* Remove `dev` feature, it's replaced with `sim` and `sim-async`
* `round_based::Simulation` is renamed and moved to `round_based::simulation::async_env::Network`
* Other async simulated network related types are moved to `round_based::simulation::async_env`
* Added convenient `round_based::simulation::{run, run_with_setup}` which make simulation very ergonomic
* Simulation outputs `round_based::simulation::SimResult`, which has convenient most-common methods:
* `round_based::simulation` module is renamed into `round_based::sim`
* `round_based::simulation::Simulation` is renamed and moved to `round_based::sim::async_env::Network`
* Other async simulated network related types are moved to `round_based::sim::async_env`
* Added convenient `round_based::sim::{run, run_with_setup}` which make simulation very ergonomic
* Simulation outputs `round_based::sim::SimResult`, which has convenient most-common methods:
* `.expect_ok()` that unwraps all results, and if any party returned an error, panics with a verbose
error message
* `.expect_eq()` that checks that all outputs are equally the same
* When `sim-async` feature is enabled, you can use `round_based::simulation::async_env::{run, run_with_setup, ...}`,
but typically you don't want to use it
* `round_based::simulation::SimulationSync` has been renamed to `round_based::simulation::Simulation`
* When `sim-async` feature is enabled, you can use `round_based::sim::async_env::{run, run_with_setup, ...}`,
but typically you don't want to use them
* `round_based::simulation::SimulationSync` has been renamed to `round_based::sim::Simulation`

Migration guidelines:
* Replace `dev` feature with `sim`
Expand Down
2 changes: 1 addition & 1 deletion round-based/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub mod runtime;
pub mod state_machine;

#[cfg(feature = "sim")]
pub mod simulation;
pub mod sim;

pub use self::delivery::*;
#[doc(no_inline)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
//!
//! let n = 3;
//!
//! let output = round_based::simulation::async_env::run(
//! let output = round_based::sim::async_env::run(
//! n,
//! |i, party| protocol_of_random_generation(party, i, n),
//! )
Expand Down Expand Up @@ -276,7 +276,7 @@ impl NextMessageId {
///
/// let n = 3;
///
/// let output = round_based::simulation::async_env::run(
/// let output = round_based::sim::async_env::run(
/// n,
/// |i, party| protocol_of_random_generation(party, i, n),
/// )
Expand Down Expand Up @@ -354,7 +354,7 @@ where
///
/// let mut rng = rand_dev::DevRng::new();
/// let n = 3;
/// let output = round_based::simulation::async_env::run_with_setup(
/// let output = round_based::sim::async_env::run_with_setup(
/// core::iter::repeat_with(|| rng.fork()).take(n.into()),
/// |i, party, rng| protocol_of_random_generation(rng, party, i, n),
/// )
Expand Down
25 changes: 20 additions & 5 deletions round-based/src/simulation/mod.rs → round-based/src/sim/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
//!
//! let n = 3;
//!
//! let output = round_based::simulation::run(
//! let output = round_based::sim::run(
//! n,
//! |i, party| protocol_of_random_generation(party, i, n),
//! )
Expand Down Expand Up @@ -187,6 +187,21 @@ impl<T> SimResult<T> {
}
}

impl<T> IntoIterator for SimResult<T> {
type Item = T;
type IntoIter = alloc::vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl<T> core::ops::Deref for SimResult<T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> From<Vec<T>> for SimResult<T> {
fn from(list: Vec<T>) -> Self {
Self(list)
Expand Down Expand Up @@ -472,7 +487,7 @@ impl<M: Clone> MessagesQueue<M> {
///
/// let n = 3;
///
/// let output = round_based::simulation::run(
/// let output = round_based::sim::run(
/// n,
/// |i, party| protocol_of_random_generation(party, i, n),
/// )
Expand Down Expand Up @@ -527,7 +542,7 @@ where
///
/// let mut rng = rand_dev::DevRng::new();
/// let n = 3;
/// let output = round_based::simulation::run_with_setup(
/// let output = round_based::sim::run_with_setup(
/// core::iter::repeat_with(|| rng.fork()).take(n.into()),
/// |i, party, rng| protocol_of_random_generation(rng, party, i, n),
/// )
Expand Down Expand Up @@ -560,7 +575,7 @@ where
#[cfg(test)]
mod tests {
mod expect_eq {
use crate::simulation::SimResult;
use crate::sim::SimResult;

#[test]
fn all_eq() {
Expand Down Expand Up @@ -590,7 +605,7 @@ mod tests {
}

mod expect_ok {
use crate::simulation::SimResult;
use crate::sim::SimResult;

#[test]
fn all_ok() {
Expand Down
File renamed without changes.

0 comments on commit f3b576a

Please sign in to comment.