wick_packet/
inherent.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Serialize, Deserialize, PartialEq)]
4#[allow(missing_copy_implementations)]
5/// Data inherent to an invocation. Meant to be supplied by a runtime, not a user.
6#[must_use]
7#[non_exhaustive]
8pub struct InherentData {
9  /// The seed to associate with an invocation.
10  pub seed: u64,
11  /// The timestamp to associate with an invocation.
12  pub timestamp: u64,
13}
14
15impl InherentData {
16  /// Constructor for [InherentData]
17  pub const fn new(seed: u64, timestamp: u64) -> Self {
18    Self { seed, timestamp }
19  }
20
21  #[cfg(all(feature = "rng", not(target_family = "wasm")))]
22  pub fn next(&self) -> Self {
23    Self {
24      seed: seeded_random::Random::from_seed(seeded_random::Seed::unsafe_new(self.seed)).gen(),
25      timestamp: std::time::SystemTime::now()
26        .duration_since(std::time::UNIX_EPOCH)
27        .unwrap()
28        .as_millis()
29        .try_into()
30        .unwrap(),
31    }
32  }
33
34  /// Create a new [InherentData] with the current time and a random seed.
35  ///
36  /// This is not "unsafe" in the Rust sense. It is unsafe because it should
37  /// only be used if you are sure you know what you're doing. If you don't know why this is unsafe, don't use it.
38  #[cfg(all(feature = "rng", feature = "std", not(target_family = "wasm")))]
39  pub fn unsafe_default() -> Self {
40    Self {
41      seed: seeded_random::Random::new().gen(),
42      timestamp: std::time::SystemTime::now()
43        .duration_since(std::time::UNIX_EPOCH)
44        .unwrap()
45        .as_millis()
46        .try_into()
47        .unwrap(),
48    }
49  }
50
51  /// Clone the [InherentData] struct.
52  ///
53  /// This is not "unsafe" in the Rust sense. It is unsafe because it should
54  /// only be used if you are sure you know what you're doing. If you don't know why this is unsafe, don't use it.
55  pub const fn unsafe_clone(&self) -> Self {
56    Self {
57      seed: self.seed,
58      timestamp: self.timestamp,
59    }
60  }
61}
62
63#[cfg(test)]
64mod tests {}