|
| 1 | +field_name: "a" |
| 2 | +flatten: false |
| 3 | +field_name: "b" |
| 4 | +flatten: false |
| 5 | +flatten_field_exists false |
| 6 | +field_name: "a" |
| 7 | +flatten: true |
| 8 | +field_name: "b" |
| 9 | +flatten: false |
| 10 | +flatten_field_exists true |
| 11 | +hasher_impl impl :: light_hasher :: DataHasher for StructFlatten |
| 12 | +{ |
| 13 | + fn hash < H > (& self) -> :: std :: result :: Result < [u8; 32], :: |
| 14 | + light_hasher :: HasherError > where H : :: light_hasher :: Hasher |
| 15 | + { |
| 16 | + use :: light_hasher :: DataHasher; use :: light_hasher :: Hasher; use |
| 17 | + :: light_hasher :: to_byte_array :: ToByteArray; let field_array = |
| 18 | + self.to_byte_arrays() ? ; let mut slices = Vec :: |
| 19 | + with_capacity(Self :: NUM_FIELDS as usize); for i in 0 .. Self :: |
| 20 | + NUM_FIELDS as usize { slices.push(field_array [i].as_slice()); } |
| 21 | + println! ("slices: {:?}", slices); H :: hashv(slices.as_slice()) |
| 22 | + } |
| 23 | +} |
| 24 | +code: { |
| 25 | + let flattened_arrays = < StructInnerF as :: light_hasher :: to_byte_array |
| 26 | + :: ToByteArray > :: to_byte_arrays :: < { StructInnerF :: NUM_FIELDS } > |
| 27 | + (& self.a) ? ; for element in flattened_arrays.iter() |
| 28 | + { |
| 29 | + field_array [num_flattned_fields] = * element; num_flattned_fields += |
| 30 | + 1; |
| 31 | + } |
| 32 | +} |
| 33 | +code: field_array [num_flattned_fields] = self.b.to_byte_array() ? ; |
| 34 | +num_flattned_fields += 1; |
| 35 | +#![feature(prelude_import)] |
| 36 | +#[prelude_import] |
| 37 | +use std::prelude::rust_2021::*; |
| 38 | +#[macro_use] |
| 39 | +extern crate std; |
| 40 | +use light_hasher::{DataHasher, Hasher, Poseidon}; |
| 41 | +use light_sdk_macros::LightHasher; |
| 42 | +use light_zero_copy::{ |
| 43 | + borsh::Deserialize, borsh_mut::DeserializeMut, ZeroCopy, ZeroCopyEq, |
| 44 | +}; |
| 45 | +extern crate test; |
| 46 | +#[cfg(test)] |
| 47 | +#[rustc_test_marker = "flatten"] |
| 48 | +#[doc(hidden)] |
| 49 | +pub const flatten: test::TestDescAndFn = test::TestDescAndFn { |
| 50 | + desc: test::TestDesc { |
| 51 | + name: test::StaticTestName("flatten"), |
| 52 | + ignore: false, |
| 53 | + ignore_message: ::core::option::Option::None, |
| 54 | + source_file: "sdk-libs/macros/tests/flatten.rs", |
| 55 | + start_line: 6usize, |
| 56 | + start_col: 4usize, |
| 57 | + end_line: 6usize, |
| 58 | + end_col: 11usize, |
| 59 | + compile_fail: false, |
| 60 | + no_run: false, |
| 61 | + should_panic: test::ShouldPanic::No, |
| 62 | + test_type: test::TestType::IntegrationTest, |
| 63 | + }, |
| 64 | + testfn: test::StaticTestFn(#[coverage(off)] || test::assert_test_result(flatten())), |
| 65 | +}; |
| 66 | +fn flatten() { |
| 67 | + #[repr(C)] |
| 68 | + pub struct StructInnerF { |
| 69 | + pub a: u8, |
| 70 | + pub b: u16, |
| 71 | + } |
| 72 | + impl ::light_hasher::to_byte_array::ToByteArray for StructInnerF { |
| 73 | + const NUM_FIELDS: usize = 2usize; |
| 74 | + fn to_byte_array( |
| 75 | + &self, |
| 76 | + ) -> ::std::result::Result<[u8; 32], ::light_hasher::HasherError> { |
| 77 | + ::light_hasher::DataHasher::hash::<::light_hasher::Poseidon>(self) |
| 78 | + } |
| 79 | + fn to_byte_arrays<const NUM_FIELDS: usize>( |
| 80 | + &self, |
| 81 | + ) -> ::std::result::Result<[[u8; 32]; NUM_FIELDS], ::light_hasher::HasherError> { |
| 82 | + if Self::NUM_FIELDS != NUM_FIELDS { |
| 83 | + return Err(::light_hasher::HasherError::InvalidNumFields); |
| 84 | + } |
| 85 | + let mut arrays = [[0u8; 32]; NUM_FIELDS]; |
| 86 | + arrays[0usize] = self.a.to_byte_array()?; |
| 87 | + arrays[1usize] = self.b.to_byte_array()?; |
| 88 | + Ok(arrays) |
| 89 | + } |
| 90 | + } |
| 91 | + impl ::light_hasher::DataHasher for StructInnerF { |
| 92 | + fn hash<H>(&self) -> ::std::result::Result<[u8; 32], ::light_hasher::HasherError> |
| 93 | + where |
| 94 | + H: ::light_hasher::Hasher, |
| 95 | + { |
| 96 | + use ::light_hasher::DataHasher; |
| 97 | + use ::light_hasher::Hasher; |
| 98 | + use ::light_hasher::to_byte_array::ToByteArray; |
| 99 | + let vec: Vec<[u8; 32]> = <[_]>::into_vec( |
| 100 | + #[rustc_box] |
| 101 | + ::alloc::boxed::Box::new([ |
| 102 | + self.a.to_byte_array()?, |
| 103 | + self.b.to_byte_array()?, |
| 104 | + ]), |
| 105 | + ); |
| 106 | + { |
| 107 | + ::std::io::_print(format_args!("slices: {0:?}\n", vec)); |
| 108 | + }; |
| 109 | + H::hashv( |
| 110 | + &[self.a.to_byte_array()?.as_slice(), self.b.to_byte_array()?.as_slice()], |
| 111 | + ) |
| 112 | + } |
| 113 | + } |
| 114 | + #[automatically_derived] |
| 115 | + impl ::core::fmt::Debug for StructInnerF { |
| 116 | + #[inline] |
| 117 | + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { |
| 118 | + ::core::fmt::Formatter::debug_struct_field2_finish( |
| 119 | + f, |
| 120 | + "StructInnerF", |
| 121 | + "a", |
| 122 | + &self.a, |
| 123 | + "b", |
| 124 | + &&self.b, |
| 125 | + ) |
| 126 | + } |
| 127 | + } |
| 128 | + #[automatically_derived] |
| 129 | + impl ::core::marker::StructuralPartialEq for StructInnerF {} |
| 130 | + #[automatically_derived] |
| 131 | + impl ::core::cmp::PartialEq for StructInnerF { |
| 132 | + #[inline] |
| 133 | + fn eq(&self, other: &StructInnerF) -> bool { |
| 134 | + self.a == other.a && self.b == other.b |
| 135 | + } |
| 136 | + } |
| 137 | + #[repr(C)] |
| 138 | + pub struct StructFlatten { |
| 139 | + #[flatten] |
| 140 | + pub a: StructInnerF, |
| 141 | + pub b: StructInnerF, |
| 142 | + } |
| 143 | + impl ::light_hasher::to_byte_array::ToByteArray for StructFlatten { |
| 144 | + const NUM_FIELDS: usize = 0 + StructInnerF::NUM_FIELDS + 1; |
| 145 | + fn to_byte_array( |
| 146 | + &self, |
| 147 | + ) -> ::std::result::Result<[u8; 32], ::light_hasher::HasherError> { |
| 148 | + ::light_hasher::DataHasher::hash::<::light_hasher::Poseidon>(self) |
| 149 | + } |
| 150 | + fn to_byte_arrays<const NUM_FIELDS: usize>( |
| 151 | + &self, |
| 152 | + ) -> ::std::result::Result<[[u8; 32]; NUM_FIELDS], ::light_hasher::HasherError> { |
| 153 | + if Self::NUM_FIELDS != NUM_FIELDS { |
| 154 | + return Err(::light_hasher::HasherError::InvalidNumFields); |
| 155 | + } |
| 156 | + let mut num_flattned_fields = 0; |
| 157 | + let mut field_array = [[0u8; 32]; NUM_FIELDS]; |
| 158 | + { |
| 159 | + let flattened_arrays = <StructInnerF as ::light_hasher::to_byte_array::ToByteArray>::to_byte_arrays::< |
| 160 | + { StructInnerF::NUM_FIELDS }, |
| 161 | + >(&self.a)?; |
| 162 | + for element in flattened_arrays.iter() { |
| 163 | + field_array[num_flattned_fields] = *element; |
| 164 | + num_flattned_fields += 1; |
| 165 | + } |
| 166 | + } |
| 167 | + field_array[num_flattned_fields] = self.b.to_byte_array()?; |
| 168 | + num_flattned_fields += 1; |
| 169 | + Ok(field_array) |
| 170 | + } |
| 171 | + } |
| 172 | + impl ::light_hasher::DataHasher for StructFlatten { |
| 173 | + fn hash<H>(&self) -> ::std::result::Result<[u8; 32], ::light_hasher::HasherError> |
| 174 | + where |
| 175 | + H: ::light_hasher::Hasher, |
| 176 | + { |
| 177 | + use ::light_hasher::DataHasher; |
| 178 | + use ::light_hasher::Hasher; |
| 179 | + use ::light_hasher::to_byte_array::ToByteArray; |
| 180 | + let field_array = self.to_byte_arrays()?; |
| 181 | + let mut slices = Vec::with_capacity(Self::NUM_FIELDS as usize); |
| 182 | + for i in 0..Self::NUM_FIELDS as usize { |
| 183 | + slices.push(field_array[i].as_slice()); |
| 184 | + } |
| 185 | + { |
| 186 | + ::std::io::_print(format_args!("slices: {0:?}\n", slices)); |
| 187 | + }; |
| 188 | + H::hashv(slices.as_slice()) |
| 189 | + } |
| 190 | + } |
| 191 | + #[automatically_derived] |
| 192 | + impl ::core::fmt::Debug for StructFlatten { |
| 193 | + #[inline] |
| 194 | + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { |
| 195 | + ::core::fmt::Formatter::debug_struct_field2_finish( |
| 196 | + f, |
| 197 | + "StructFlatten", |
| 198 | + "a", |
| 199 | + &self.a, |
| 200 | + "b", |
| 201 | + &&self.b, |
| 202 | + ) |
| 203 | + } |
| 204 | + } |
| 205 | + #[automatically_derived] |
| 206 | + impl ::core::marker::StructuralPartialEq for StructFlatten {} |
| 207 | + #[automatically_derived] |
| 208 | + impl ::core::cmp::PartialEq for StructFlatten { |
| 209 | + #[inline] |
| 210 | + fn eq(&self, other: &StructFlatten) -> bool { |
| 211 | + self.a == other.a && self.b == other.b |
| 212 | + } |
| 213 | + } |
| 214 | + let test = StructFlatten { |
| 215 | + a: StructInnerF { a: 1, b: 2 }, |
| 216 | + b: StructInnerF { a: 3, b: 4 }, |
| 217 | + }; |
| 218 | + let one = { |
| 219 | + let mut array = [0u8; 32]; |
| 220 | + array[31] = 1; |
| 221 | + array |
| 222 | + }; |
| 223 | + let two = { |
| 224 | + let mut array = [0u8; 32]; |
| 225 | + array[31] = 2; |
| 226 | + array |
| 227 | + }; |
| 228 | + let three = { |
| 229 | + let mut array = [0u8; 32]; |
| 230 | + array[31] = 3; |
| 231 | + array |
| 232 | + }; |
| 233 | + let four = { |
| 234 | + let mut array = [0u8; 32]; |
| 235 | + array[31] = 4; |
| 236 | + array |
| 237 | + }; |
| 238 | + let hash = Poseidon::hashv(&[three.as_slice(), four.as_slice()]).unwrap(); |
| 239 | + let manual_slices = [one.as_ref(), two.as_ref(), hash.as_ref()]; |
| 240 | + { |
| 241 | + ::std::io::_print(format_args!("manual_slices {0:?}\n", manual_slices)); |
| 242 | + }; |
| 243 | + let manual_hash = Poseidon::hashv(&manual_slices).unwrap(); |
| 244 | + match (&test.hash::<Poseidon>().unwrap(), &manual_hash) { |
| 245 | + (left_val, right_val) => { |
| 246 | + if !(*left_val == *right_val) { |
| 247 | + let kind = ::core::panicking::AssertKind::Eq; |
| 248 | + ::core::panicking::assert_failed( |
| 249 | + kind, |
| 250 | + &*left_val, |
| 251 | + &*right_val, |
| 252 | + ::core::option::Option::None, |
| 253 | + ); |
| 254 | + } |
| 255 | + } |
| 256 | + }; |
| 257 | +} |
| 258 | +#[rustc_main] |
| 259 | +#[coverage(off)] |
| 260 | +#[doc(hidden)] |
| 261 | +pub fn main() -> () { |
| 262 | + extern crate test; |
| 263 | + test::test_main_static(&[&flatten]) |
| 264 | +} |
0 commit comments