1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use std::ops::{Deref, DerefMut};

use super::fvm_shared_latest::randomness::Randomness as Randomness_latest;
use fvm_shared2::randomness::Randomness as Randomness_v2;
use fvm_shared3::randomness::Randomness as Randomness_v3;
use fvm_shared4::randomness::Randomness as Randomness_v4;
use serde::{Deserialize, Serialize};

/// Represents a shim over `Randomness` from `fvm_shared` with convenience
/// methods to convert to an older version of the type
///
/// # Examples
/// ```
/// # use forest_filecoin::doctest_private::Randomness;
///
/// // Create FVM2 Randomness normally
/// let fvm2_rand = fvm_shared2::randomness::Randomness(vec![]);
///
/// // Create a correspndoning FVM3 Randomness
/// let fvm3_rand = fvm_shared3::randomness::Randomness(vec![]);
///
/// // Create a correspndoning FVM4 Randomness
/// let fvm4_rand = fvm_shared4::randomness::Randomness(vec![]);
///
/// // Create a shim Randomness, ensure conversions are correct
/// let rand_shim = Randomness::new(vec![]);
/// assert_eq!(fvm4_rand, *rand_shim);
/// assert_eq!(fvm3_rand, rand_shim.clone().into());
/// assert_eq!(fvm2_rand, rand_shim.into());
/// ```
#[derive(PartialEq, Eq, Default, Clone, Debug, Deserialize, Serialize)]
#[serde(transparent)]
pub struct Randomness(Randomness_latest);

impl Randomness {
    pub fn new(rand: Vec<u8>) -> Self {
        Randomness(Randomness_latest(rand))
    }
}

impl Deref for Randomness {
    type Target = Randomness_latest;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Randomness {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl From<Randomness_v4> for Randomness {
    fn from(other: Randomness_v4) -> Self {
        Randomness(other)
    }
}

impl From<Randomness_v3> for Randomness {
    fn from(other: Randomness_v3) -> Self {
        Randomness(Randomness_latest(other.0))
    }
}

impl From<Randomness_v2> for Randomness {
    fn from(other: Randomness_v2) -> Self {
        Randomness(Randomness_latest(other.0))
    }
}

impl From<Randomness> for Randomness_v4 {
    fn from(other: Randomness) -> Self {
        other.0
    }
}

impl From<Randomness> for Randomness_v3 {
    fn from(other: Randomness) -> Self {
        Self(other.0 .0)
    }
}

impl From<Randomness> for Randomness_v2 {
    fn from(other: Randomness) -> Self {
        Self(other.0 .0)
    }
}