nimble_participant_steps/
lib.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/nimble-rust/nimble
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5use flood_rs::{Deserialize, ReadOctetStream};
6use nimble_participant::ParticipantId;
7use nimble_steps::Step;
8use std::collections::HashMap;
9use std::io;
10
11/// `ParticipantSteps` stores the steps for all participants for a single tick of a simulation.
12///
13/// This struct is generic over `T`, where `T` can be any type that represents a step in the simulation.
14/// The steps are stored in a `HashMap` where each participant is identified by a unique `ParticipantId`,
15/// and the corresponding value is the step of type `T` that the participant will take during that tick.
16///
17/// # Usage
18/// This struct is typically used in the context of a game or simulation engine where each tick
19/// (a discrete time step) requires recording or processing the actions or movements of multiple participants.
20///
21/// # Examples
22/// ```
23/// use std::collections::HashMap;
24/// use nimble_participant::ParticipantId;
25/// use nimble_participant_steps::ParticipantSteps;
26/// use nimble_steps::Step;
27///
28/// struct ExampleStep {
29///     action: String
30/// }
31///
32/// let mut steps = HashMap::new();
33/// steps.insert(ParticipantId(1), ExampleStep { action: "move".to_string() });
34/// steps.insert(ParticipantId(2), ExampleStep { action: "jump".to_string() });
35///
36/// let participant_steps = ParticipantSteps::<Step<String>>::new();
37/// ```
38///
39/// In this example, `ParticipantSteps` is used to track the actions of two participants in a single tick.
40/// Each participant has a unique `ParticipantId` and a `Step` that describes their action for the tick.
41#[derive(Clone)]
42pub struct ParticipantSteps<T> {
43    pub steps: HashMap<ParticipantId, Step<T>>,
44}
45
46impl<T> Default for ParticipantSteps<T> {
47    fn default() -> Self {
48        Self::new()
49    }
50}
51
52impl<T> ParticipantSteps<T> {
53    pub fn new() -> Self {
54        Self {
55            steps: HashMap::new(),
56        }
57    }
58
59    pub fn insert(&mut self, participant_id: ParticipantId, step: Step<T>) {
60        self.steps.insert(participant_id, step);
61    }
62
63    pub fn len(&self) -> usize {
64        self.steps.len()
65    }
66
67    pub fn is_empty(&self) -> bool {
68        self.steps.is_empty()
69    }
70}
71
72impl<T: Clone> Deserialize for ParticipantSteps<T> {
73    fn deserialize(_: &mut impl ReadOctetStream) -> io::Result<Self>
74    where
75        Self: Sized,
76    {
77        todo!()
78    }
79}