Simulation

Trait Simulation 

Source
pub trait Simulation {
    type StateLoadingError;
    type AccessData: ?Sized;
    type LoadData;
    type Event: Copy + Ord;
    type EventContainer<'a>: Iterator<Item = Self::Event>
       where Self: 'a;

    // Required methods
    fn data(&self) -> &Self::AccessData;
    fn reload(
        &mut self,
        data: Self::LoadData,
    ) -> Result<(), Self::StateLoadingError>;
    fn callables(&self) -> Self::EventContainer<'_>;
    fn revertables(&self) -> Self::EventContainer<'_>;
    fn callable(&self, event: Self::Event) -> bool;
    fn revertable(&self, event: Self::Event) -> bool;
    unsafe fn call(&mut self, event: Self::Event);
    unsafe fn revert(&mut self, event: Self::Event);

    // Provided methods
    fn try_call(&mut self, event: Self::Event) -> bool { ... }
    fn try_revert(&mut self, event: Self::Event) -> bool { ... }
    fn prepare_call(&mut self) -> CallState<'_, Self, Call> { ... }
    fn prepare_revert(&mut self) -> CallState<'_, Self, Revert> { ... }
}
Expand description

The Simulation trait provides an interface for interacting with a simulation.

Required Associated Types§

Source

type StateLoadingError

The error type returned when loading the simulation state fails.

Source

type AccessData: ?Sized

The type used to access the current state.

Source

type LoadData

The type of data used to load the simulation state.

Source

type Event: Copy + Ord

The type of events that can be called or reverted in the simulation.

Source

type EventContainer<'a>: Iterator<Item = Self::Event> where Self: 'a

The type of container used to access the available events.

Required Methods§

Source

fn data(&self) -> &Self::AccessData

Returns a reference to the data which repsesents the current state.

Source

fn reload( &mut self, data: Self::LoadData, ) -> Result<(), Self::StateLoadingError>

Reloads the simulation state from the provided data.

Source

fn callables(&self) -> Self::EventContainer<'_>

Returns the events that can currently be called.

Source

fn revertables(&self) -> Self::EventContainer<'_>

Returns the events that can currently be reverted.

Source

fn callable(&self, event: Self::Event) -> bool

Checks if the provided event can be called.

Source

fn revertable(&self, event: Self::Event) -> bool

Checks if the provided event can be reverted.

Source

unsafe fn call(&mut self, event: Self::Event)

Calls the provided event.

§Safety

The caller must ensure that the event is valid and can be called in the current simulation state.

Source

unsafe fn revert(&mut self, event: Self::Event)

Reverts the provided event.

§Safety

The caller must ensure that the event is valid and can be reverted in the current simulation state.

Provided Methods§

Source

fn try_call(&mut self, event: Self::Event) -> bool

Tries to call the provided event and returns if it was successful.

Source

fn try_revert(&mut self, event: Self::Event) -> bool

Tries to revert the provided event and returns if it was successful.

Source

fn prepare_call(&mut self) -> CallState<'_, Self, Call>

Prepares a safe helper to list callable elements and choose one to call.

Examples found in repository?
examples/example.rs (line 132)
13fn main() -> ExitCode {
14    let mut save = false;
15    let mut load = false;
16
17    let mut args = std::env::args();
18    args.next();
19    for arg in args {
20        match arg.as_ref() {
21            "save" => save = true,
22            "load" => load = true,
23            _ => eprintln!("Ignore invalid argument '{arg}'"),
24        }
25    }
26
27    let Ok(net) = Net::load("examples/example.pn".as_ref()) else {
28        eprintln!("Failed to load example net");
29        return ExitCode::FAILURE;
30    };
31    let mut net = SimulatedNet::new(net);
32
33    let mut names = HashMap::new();
34    let Ok(file) = File::open("examples/example.pnk") else {
35        eprintln!("Failed to load example keys");
36        return ExitCode::FAILURE;
37    };
38    for (tid, line) in net.transition_ids().zip(BufReader::new(file).lines()) {
39        let Ok(line) = line else {
40            eprintln!("Failed to read key");
41            return ExitCode::FAILURE;
42        };
43        names.insert(tid, line);
44    }
45
46    if save && net.save("examples/example_copy.pns".as_ref()).is_err() {
47        eprintln!("Failed to save example net");
48    }
49
50    if load {
51        fn read_values(filename: &Path) -> std::io::Result<Vec<u32>> {
52            let size = (std::fs::metadata(filename)?.len() + 3) / 4;
53
54            let mut file = File::open(filename)?;
55
56            (0..size)
57                .map(|_| {
58                    let mut value = [0; 4];
59                    file.read_exact(&mut value)?;
60                    Ok(u32::from_le_bytes(value))
61                })
62                .collect()
63        }
64
65        let Ok(data) = read_values("examples/example.pns".as_ref()) else {
66            eprintln!("Reading state data failed");
67            return ExitCode::FAILURE;
68        };
69
70        if net.add_simulation_from_data(data).is_err() {
71            eprintln!("State initialization failed");
72            return ExitCode::FAILURE;
73        }
74    } else {
75        net.add_simulation();
76    }
77
78    enum StepAction {
79        Continue,
80        Reverse,
81        Save,
82        Quit,
83    }
84
85    let mut forward = true;
86    for mut simulation in &mut net {
87        fn step<D: FireDirection>(
88            fire: FireState<SimulationStateMut, D>,
89            names: &HashMap<Tid, String>,
90        ) -> StepAction {
91            if fire.callables.is_empty() {
92                return StepAction::Reverse;
93            }
94
95            for (i, tid) in fire.callables.iter().enumerate() {
96                println!("{}: {}", i + 1, names[tid]);
97            }
98            let stdin = io::stdin();
99            let mut string = String::new();
100            let Ok(size) = stdin.read_line(&mut string) else {
101                eprintln!("Input error");
102                return StepAction::Continue;
103            };
104            if size == 0 {
105                return StepAction::Continue;
106            }
107            match unsafe { string.chars().next().unwrap_unchecked() } {
108                'r' => return StepAction::Reverse,
109                'q' => return StepAction::Quit,
110                's' => return StepAction::Save,
111                _ => (),
112            }
113            match usize::from_str(&string[..(string.len() - 1)]) {
114                Ok(num) if num != 0 && num <= fire.callables.len() => {
115                    fire.call(num - 1);
116                }
117                _ => {
118                    println!(
119                        "You have to input a valid number from 1 to {}",
120                        fire.callables.len()
121                    );
122                    println!("You can also quit by writing \"q\", save the current state by writing \"s\" or reverse by writing \"r\"");
123                    return StepAction::Continue;
124                }
125            }
126
127            StepAction::Continue
128        }
129
130        loop {
131            let step_action = if forward {
132                step(simulation.prepare_call(), &names)
133            } else {
134                step(simulation.prepare_revert(), &names)
135            };
136
137            use StepAction::*;
138            match step_action {
139                Continue => (),
140                Reverse => {
141                    println!("Reverse play direction!");
142                    forward = !forward;
143                }
144                Save => {
145                    fn save(data: &[usize], filename: &Path) -> std::io::Result<()> {
146                        let mut file = File::create(filename)?;
147                        for &count in data {
148                            file.write_all(&(count as u32).to_le_bytes())?;
149                        }
150
151                        Ok(())
152                    }
153
154                    let data = simulation.data();
155
156                    println!(
157                        "{}",
158                        if save(data, "examples/example.pns".as_ref()).is_ok() {
159                            "Saving successful"
160                        } else {
161                            "Saving failed"
162                        }
163                    );
164                }
165                Quit => break,
166            }
167        }
168    }
169
170    ExitCode::SUCCESS
171}
Source

fn prepare_revert(&mut self) -> CallState<'_, Self, Revert>

Prepares a safe helper to list revertable elements and choose one to revert.

Examples found in repository?
examples/example.rs (line 134)
13fn main() -> ExitCode {
14    let mut save = false;
15    let mut load = false;
16
17    let mut args = std::env::args();
18    args.next();
19    for arg in args {
20        match arg.as_ref() {
21            "save" => save = true,
22            "load" => load = true,
23            _ => eprintln!("Ignore invalid argument '{arg}'"),
24        }
25    }
26
27    let Ok(net) = Net::load("examples/example.pn".as_ref()) else {
28        eprintln!("Failed to load example net");
29        return ExitCode::FAILURE;
30    };
31    let mut net = SimulatedNet::new(net);
32
33    let mut names = HashMap::new();
34    let Ok(file) = File::open("examples/example.pnk") else {
35        eprintln!("Failed to load example keys");
36        return ExitCode::FAILURE;
37    };
38    for (tid, line) in net.transition_ids().zip(BufReader::new(file).lines()) {
39        let Ok(line) = line else {
40            eprintln!("Failed to read key");
41            return ExitCode::FAILURE;
42        };
43        names.insert(tid, line);
44    }
45
46    if save && net.save("examples/example_copy.pns".as_ref()).is_err() {
47        eprintln!("Failed to save example net");
48    }
49
50    if load {
51        fn read_values(filename: &Path) -> std::io::Result<Vec<u32>> {
52            let size = (std::fs::metadata(filename)?.len() + 3) / 4;
53
54            let mut file = File::open(filename)?;
55
56            (0..size)
57                .map(|_| {
58                    let mut value = [0; 4];
59                    file.read_exact(&mut value)?;
60                    Ok(u32::from_le_bytes(value))
61                })
62                .collect()
63        }
64
65        let Ok(data) = read_values("examples/example.pns".as_ref()) else {
66            eprintln!("Reading state data failed");
67            return ExitCode::FAILURE;
68        };
69
70        if net.add_simulation_from_data(data).is_err() {
71            eprintln!("State initialization failed");
72            return ExitCode::FAILURE;
73        }
74    } else {
75        net.add_simulation();
76    }
77
78    enum StepAction {
79        Continue,
80        Reverse,
81        Save,
82        Quit,
83    }
84
85    let mut forward = true;
86    for mut simulation in &mut net {
87        fn step<D: FireDirection>(
88            fire: FireState<SimulationStateMut, D>,
89            names: &HashMap<Tid, String>,
90        ) -> StepAction {
91            if fire.callables.is_empty() {
92                return StepAction::Reverse;
93            }
94
95            for (i, tid) in fire.callables.iter().enumerate() {
96                println!("{}: {}", i + 1, names[tid]);
97            }
98            let stdin = io::stdin();
99            let mut string = String::new();
100            let Ok(size) = stdin.read_line(&mut string) else {
101                eprintln!("Input error");
102                return StepAction::Continue;
103            };
104            if size == 0 {
105                return StepAction::Continue;
106            }
107            match unsafe { string.chars().next().unwrap_unchecked() } {
108                'r' => return StepAction::Reverse,
109                'q' => return StepAction::Quit,
110                's' => return StepAction::Save,
111                _ => (),
112            }
113            match usize::from_str(&string[..(string.len() - 1)]) {
114                Ok(num) if num != 0 && num <= fire.callables.len() => {
115                    fire.call(num - 1);
116                }
117                _ => {
118                    println!(
119                        "You have to input a valid number from 1 to {}",
120                        fire.callables.len()
121                    );
122                    println!("You can also quit by writing \"q\", save the current state by writing \"s\" or reverse by writing \"r\"");
123                    return StepAction::Continue;
124                }
125            }
126
127            StepAction::Continue
128        }
129
130        loop {
131            let step_action = if forward {
132                step(simulation.prepare_call(), &names)
133            } else {
134                step(simulation.prepare_revert(), &names)
135            };
136
137            use StepAction::*;
138            match step_action {
139                Continue => (),
140                Reverse => {
141                    println!("Reverse play direction!");
142                    forward = !forward;
143                }
144                Save => {
145                    fn save(data: &[usize], filename: &Path) -> std::io::Result<()> {
146                        let mut file = File::create(filename)?;
147                        for &count in data {
148                            file.write_all(&(count as u32).to_le_bytes())?;
149                        }
150
151                        Ok(())
152                    }
153
154                    let data = simulation.data();
155
156                    println!(
157                        "{}",
158                        if save(data, "examples/example.pns".as_ref()).is_ok() {
159                            "Saving successful"
160                        } else {
161                            "Saving failed"
162                        }
163                    );
164                }
165                Quit => break,
166            }
167        }
168    }
169
170    ExitCode::SUCCESS
171}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§