snarkvm_console_program/data/future/
argument.rs

1// Copyright 2024 Aleo Network Foundation
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::*;
17
18/// An argument passed into a future.
19#[derive(Clone)]
20pub enum Argument<N: Network> {
21    /// A plaintext value.
22    Plaintext(Plaintext<N>),
23    /// A future.
24    Future(Future<N>),
25}
26
27impl<N: Network> Equal<Self> for Argument<N> {
28    type Output = Boolean<N>;
29
30    /// Returns `true` if `self` and `other` are equal.
31    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    /// Returns `true` if `self` and `other` are *not* equal.
40    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        // Read the index.
55        let index = u8::read_le(&mut reader)?;
56        // Read the argument.
57        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    /// Returns the argument as a list of **little-endian** bits.
83    #[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    /// Returns the argument as a list of **big-endian** bits.
98    #[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}