Skip to content

Commit 569278a

Browse files
committed
stash
1 parent e3028e6 commit 569278a

File tree

4 files changed

+342
-64
lines changed

4 files changed

+342
-64
lines changed

sdk-libs/macros/expand.rs

+264
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
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+
}

sdk-libs/macros/src/hasher.rs

+40-27
Original file line numberDiff line numberDiff line change
@@ -247,22 +247,6 @@ pub(crate) fn hasher(input: ItemStruct) -> Result<TokenStream> {
247247
});
248248
println!("flatten_field_exists {}", flatten_field_exists);
249249
let hasher_impl = if flatten_field_exists {
250-
// Insert in front of all other flattening code
251-
// Do it here so that we have collected all flattned_fields_added.
252-
code.insert(
253-
0,
254-
quote! {
255-
let mut num_flattned_fields = 0;
256-
let mut field_array = [[0u8; 32]; #(#flattned_fields_added)*];
257-
let mut slices: [&[u8]; #(#flattned_fields_added)*] = [&[]; #(#flattned_fields_added)*];
258-
},
259-
);
260-
code.push(quote! {
261-
// Set all slices properly for both flattened and non-flattened fields
262-
for i in 0..num_flattned_fields {
263-
slices[i] = field_array[i].as_slice();
264-
}
265-
});
266250
quote! {
267251
impl #impl_gen ::light_hasher::DataHasher for #struct_name #type_gen #where_clause {
268252
fn hash<H>(&self) -> ::std::result::Result<[u8; 32], ::light_hasher::HasherError>
@@ -273,9 +257,11 @@ pub(crate) fn hasher(input: ItemStruct) -> Result<TokenStream> {
273257
use ::light_hasher::Hasher;
274258
use ::light_hasher::to_byte_array::ToByteArray;
275259

276-
#(#truncate_code)*
277-
#(#code)*
278-
println!("slices: {:?}", slices);
260+
let field_array: [[u8; 32]; Self::NUM_FIELDS ] = self.to_byte_arrays()?;
261+
let mut slices: [&[u8]; Self::NUM_FIELDS as usize] = [&[]; Self::NUM_FIELDS as usize];
262+
for i in 0..Self::NUM_FIELDS as usize {
263+
slices[i] = field_array[i].as_slice();
264+
}
279265
H::hashv(slices.as_slice())
280266
}
281267
}}
@@ -353,25 +339,52 @@ pub(crate) fn hasher(input: ItemStruct) -> Result<TokenStream> {
353339
quote! { #field_count }
354340
};
355341

356-
Ok(quote! {
357-
impl #impl_gen ::light_hasher::to_byte_array::ToByteArray for #struct_name #type_gen #where_clause {
358-
const NUM_FIELDS: usize = #total_field_count;
359-
360-
fn to_byte_array(&self) -> ::std::result::Result<[u8; 32], ::light_hasher::HasherError> {
361-
#to_byte_array
342+
let to_byte_arrays_code = if flatten_field_exists {
343+
// Insert in front of all other flattening code
344+
// Do it here so that we have collected all flattned_fields_added.
345+
code.insert(
346+
0,
347+
quote! {
348+
let mut num_flattned_fields = 0;
349+
let mut field_array = [[0u8; 32]; NUM_FIELDS];
350+
},
351+
);
352+
quote! {
353+
fn to_byte_arrays<const NUM_FIELDS: usize>(&self) -> ::std::result::Result<[[u8; 32]; NUM_FIELDS], ::light_hasher::HasherError> {
354+
if Self::NUM_FIELDS != NUM_FIELDS {
355+
return Err(::light_hasher::HasherError::InvalidNumFields);
356+
}
357+
#(#truncate_code)*
358+
#(#code)*
359+
Ok(field_array)
362360
}
363-
361+
}
362+
} else {
363+
quote! {
364364
fn to_byte_arrays<const NUM_FIELDS: usize>(&self) -> ::std::result::Result<[[u8; 32]; NUM_FIELDS], ::light_hasher::HasherError> {
365365
if Self::NUM_FIELDS != NUM_FIELDS {
366366
return Err(::light_hasher::HasherError::InvalidNumFields);
367367
}
368368
#(#truncate_code)*
369-
let mut arrays = [[0u8; 32]; NUM_FIELDS];
369+
let mut arrays = [[0u8; 32]; NUM_FIELDS ];
370370

371371
#(#to_byte_arrays_fields)*
372372
Ok(arrays)
373373
}
374374
}
375+
};
376+
377+
Ok(quote! {
378+
impl #impl_gen ::light_hasher::to_byte_array::ToByteArray for #struct_name #type_gen #where_clause {
379+
const NUM_FIELDS: usize = #total_field_count;
380+
381+
fn to_byte_array(&self) -> ::std::result::Result<[u8; 32], ::light_hasher::HasherError> {
382+
#to_byte_array
383+
}
384+
385+
#to_byte_arrays_code
386+
387+
}
375388

376389
#hasher_impl
377390
})

0 commit comments

Comments
 (0)