Skip to main content

forest/shim/
randomness.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use super::fvm_shared_latest::randomness::Randomness as Randomness_latest;
5use fvm_shared2::randomness::Randomness as Randomness_v2;
6use fvm_shared3::randomness::Randomness as Randomness_v3;
7use serde::{Deserialize, Serialize};
8
9/// Represents a shim over `Randomness` from `fvm_shared` with convenience
10/// methods to convert to an older version of the type
11///
12/// # Examples
13/// ```
14/// # use forest::doctest_private::Randomness;
15///
16/// // Create FVM2 Randomness normally
17/// let fvm2_rand = fvm_shared2::randomness::Randomness(vec![]);
18///
19/// // Create a correspndoning FVM3 Randomness
20/// let fvm3_rand = fvm_shared3::randomness::Randomness(vec![]);
21///
22/// // Create a correspndoning FVM4 Randomness
23/// let fvm4_rand = fvm_shared4::randomness::Randomness(vec![]);
24///
25/// // Create a shim Randomness, ensure conversions are correct
26/// let rand_shim = Randomness::new(vec![]);
27/// assert_eq!(fvm4_rand, *rand_shim);
28/// assert_eq!(fvm3_rand, rand_shim.clone().into());
29/// assert_eq!(fvm2_rand, rand_shim.into());
30/// ```
31#[derive(
32    PartialEq,
33    Eq,
34    Default,
35    Clone,
36    Debug,
37    Deserialize,
38    Serialize,
39    derive_more::Deref,
40    derive_more::DerefMut,
41    derive_more::From,
42    derive_more::Into,
43)]
44#[serde(transparent)]
45pub struct Randomness(Randomness_latest);
46
47impl Randomness {
48    pub fn new(rand: Vec<u8>) -> Self {
49        Randomness(Randomness_latest(rand))
50    }
51}
52
53impl From<Randomness_v3> for Randomness {
54    fn from(other: Randomness_v3) -> Self {
55        Randomness(Randomness_latest(other.0))
56    }
57}
58
59impl From<Randomness_v2> for Randomness {
60    fn from(other: Randomness_v2) -> Self {
61        Randomness(Randomness_latest(other.0))
62    }
63}
64
65impl From<Randomness> for Randomness_v3 {
66    fn from(other: Randomness) -> Self {
67        Self(other.0.0)
68    }
69}
70
71impl From<Randomness> for Randomness_v2 {
72    fn from(other: Randomness) -> Self {
73        Self(other.0.0)
74    }
75}