/ console / program / src / data / future / to_bits.rs
to_bits.rs
 1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
 2  // This file is part of the alphavm 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  
16  use super::*;
17  
18  impl<N: Network> ToBits for Future<N> {
19      /// Returns the future as a list of **little-endian** bits.
20      #[inline]
21      fn write_bits_le(&self, vec: &mut Vec<bool>) {
22          // Write the bits for the program ID.
23          let program_id_bits = self.program_id.to_bits_le();
24          u16::try_from(program_id_bits.len()).or_halt_with::<N>("Program ID exceeds u16::MAX bits").write_bits_le(vec);
25          vec.extend_from_slice(&program_id_bits);
26  
27          // Write the bits for the function name.
28          let function_name_bits = self.function_name.to_bits_le();
29          u16::try_from(function_name_bits.len())
30              .or_halt_with::<N>("Function name exceeds u16::MAX bits")
31              .write_bits_le(vec);
32          vec.extend_from_slice(&function_name_bits);
33  
34          // Write the number of arguments.
35          u8::try_from(self.arguments.len()).or_halt_with::<N>("arguments exceed u8::MAX").write_bits_le(vec);
36  
37          // Write the arguments.
38          for argument in &self.arguments {
39              let argument_bits = argument.to_bits_le();
40  
41              // Write the size of the argument.
42              u16::try_from(argument_bits.len()).or_halt_with::<N>("argument exceeds u16::MAX bits").write_bits_le(vec);
43  
44              // Write the argument.
45              vec.extend_from_slice(&argument_bits);
46          }
47      }
48  
49      /// Returns the future as a list of **big-endian** bits.
50      #[inline]
51      fn write_bits_be(&self, vec: &mut Vec<bool>) {
52          // Write the bits for the program ID.
53          let program_id_bits = self.program_id.to_bits_be();
54          u16::try_from(program_id_bits.len()).or_halt_with::<N>("Program ID exceeds u16::MAX bits").write_bits_be(vec);
55          vec.extend_from_slice(&program_id_bits);
56  
57          // Write the bits for the function name.
58          let function_name_bits = self.function_name.to_bits_be();
59          u16::try_from(function_name_bits.len())
60              .or_halt_with::<N>("Function name exceeds u16::MAX bits")
61              .write_bits_be(vec);
62          vec.extend_from_slice(&function_name_bits);
63  
64          // Write the number of arguments.
65          u8::try_from(self.arguments.len()).or_halt_with::<N>("arguments exceed u8::MAX").write_bits_be(vec);
66  
67          // Write the arguments.
68          for argument in &self.arguments {
69              let argument_bits = argument.to_bits_be();
70  
71              // Write the size of the argument.
72              u16::try_from(argument_bits.len()).or_halt_with::<N>("argument exceeds u16::MAX bits").write_bits_be(vec);
73  
74              // Write the argument.
75              vec.extend_from_slice(&argument_bits);
76          }
77      }
78  }