snarkvm_console_program/data/future/
argument.rs1use super::*;
17
18#[derive(Clone)]
20pub enum Argument<N: Network> {
21 Plaintext(Plaintext<N>),
23 Future(Future<N>),
25}
26
27impl<N: Network> Equal<Self> for Argument<N> {
28 type Output = Boolean<N>;
29
30 fn is_equal(&self, other: &Self) -> Self::Output {
32 match (self, other) {
33 (Self::Plaintext(plaintext_a), Self::Plaintext(plaintext_b)) => plaintext_a.is_equal(plaintext_b),
34 (Self::Future(future_a), Self::Future(future_b)) => future_a.is_equal(future_b),
35 (Self::Plaintext(..), _) | (Self::Future(..), _) => Boolean::new(false),
36 }
37 }
38
39 fn is_not_equal(&self, other: &Self) -> Self::Output {
41 match (self, other) {
42 (Self::Plaintext(plaintext_a), Self::Plaintext(plaintext_b)) => plaintext_a.is_not_equal(plaintext_b),
43 (Self::Future(future_a), Self::Future(future_b)) => future_a.is_not_equal(future_b),
44 (Self::Plaintext(..), _) | (Self::Future(..), _) => Boolean::new(true),
45 }
46 }
47}
48
49impl<N: Network> FromBytes for Argument<N> {
50 fn read_le<R: Read>(mut reader: R) -> IoResult<Self>
51 where
52 Self: Sized,
53 {
54 let index = u8::read_le(&mut reader)?;
56 let argument = match index {
58 0 => Self::Plaintext(Plaintext::read_le(&mut reader)?),
59 1 => Self::Future(Future::read_le(&mut reader)?),
60 2.. => return Err(error(format!("Failed to decode future argument {index}"))),
61 };
62 Ok(argument)
63 }
64}
65
66impl<N: Network> ToBytes for Argument<N> {
67 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
68 match self {
69 Self::Plaintext(plaintext) => {
70 0u8.write_le(&mut writer)?;
71 plaintext.write_le(&mut writer)
72 }
73 Self::Future(future) => {
74 1u8.write_le(&mut writer)?;
75 future.write_le(&mut writer)
76 }
77 }
78 }
79}
80
81impl<N: Network> ToBits for Argument<N> {
82 #[inline]
84 fn write_bits_le(&self, vec: &mut Vec<bool>) {
85 match self {
86 Self::Plaintext(plaintext) => {
87 vec.push(false);
88 plaintext.write_bits_le(vec);
89 }
90 Self::Future(future) => {
91 vec.push(true);
92 future.write_bits_le(vec);
93 }
94 }
95 }
96
97 #[inline]
99 fn write_bits_be(&self, vec: &mut Vec<bool>) {
100 match self {
101 Self::Plaintext(plaintext) => {
102 vec.push(false);
103 plaintext.write_bits_be(vec);
104 }
105 Self::Future(future) => {
106 vec.push(true);
107 future.write_bits_be(vec);
108 }
109 }
110 }
111}